mirror of
https://github.com/devenperez/leetcode.git
synced 2026-06-15 09:57:09 +00:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6e90bcdc1f | |||
| ae4b1056dd | |||
| d5e6259f5b | |||
| c6bc37a26c | |||
| a7dc8f50f0 | |||
| fadfb3c332 | |||
| 45404409de | |||
| 71d9f17be7 |
@@ -0,0 +1,84 @@
|
|||||||
|
# """
|
||||||
|
# This is the interface that allows for creating nested lists.
|
||||||
|
# You should not implement it, or speculate about its implementation
|
||||||
|
# """
|
||||||
|
#class NestedInteger:
|
||||||
|
# def __init__(self, value=None):
|
||||||
|
# """
|
||||||
|
# If value is not specified, initializes an empty list.
|
||||||
|
# Otherwise initializes a single integer equal to value.
|
||||||
|
# """
|
||||||
|
#
|
||||||
|
# def isInteger(self):
|
||||||
|
# """
|
||||||
|
# @return True if this NestedInteger holds a single integer, rather than a nested list.
|
||||||
|
# :rtype bool
|
||||||
|
# """
|
||||||
|
#
|
||||||
|
# def add(self, elem):
|
||||||
|
# """
|
||||||
|
# Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
|
||||||
|
# :rtype void
|
||||||
|
# """
|
||||||
|
#
|
||||||
|
# def setInteger(self, value):
|
||||||
|
# """
|
||||||
|
# Set this NestedInteger to hold a single integer equal to value.
|
||||||
|
# :rtype void
|
||||||
|
# """
|
||||||
|
#
|
||||||
|
# def getInteger(self):
|
||||||
|
# """
|
||||||
|
# @return the single integer that this NestedInteger holds, if it holds a single integer
|
||||||
|
# Return None if this NestedInteger holds a nested list
|
||||||
|
# :rtype int
|
||||||
|
# """
|
||||||
|
#
|
||||||
|
# def getList(self):
|
||||||
|
# """
|
||||||
|
# @return the nested list that this NestedInteger holds, if it holds a nested list
|
||||||
|
# Return None if this NestedInteger holds a single integer
|
||||||
|
# :rtype List[NestedInteger]
|
||||||
|
# """
|
||||||
|
|
||||||
|
class Solution:
|
||||||
|
def deserialize(self, s: str) -> NestedInteger:
|
||||||
|
"""
|
||||||
|
Complexities:
|
||||||
|
Time: O(n)
|
||||||
|
Space: O(n)
|
||||||
|
|
||||||
|
where n = len(s)
|
||||||
|
"""
|
||||||
|
|
||||||
|
# base case: number
|
||||||
|
if s[0] != "[":
|
||||||
|
return NestedInteger(int(s))
|
||||||
|
|
||||||
|
# is a list
|
||||||
|
head = NestedInteger()
|
||||||
|
stack = [head]
|
||||||
|
|
||||||
|
i = 1
|
||||||
|
while i < len(s):
|
||||||
|
if s[i] == ",":
|
||||||
|
i += 1
|
||||||
|
elif s[i] == "[":
|
||||||
|
newNestedInteger = NestedInteger()
|
||||||
|
stack[-1].add(newNestedInteger)
|
||||||
|
stack.append(newNestedInteger)
|
||||||
|
i += 1
|
||||||
|
elif s[i] == "]":
|
||||||
|
stack.pop()
|
||||||
|
i += 1
|
||||||
|
else: # Is number or '-'
|
||||||
|
# Find entire number
|
||||||
|
j = i
|
||||||
|
while s[j] not in ",]":
|
||||||
|
j += 1
|
||||||
|
newInt = int(s[i:j])
|
||||||
|
stack[-1].add(NestedInteger(newInt))
|
||||||
|
i = j
|
||||||
|
|
||||||
|
return head
|
||||||
|
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
<h2><a href="https://leetcode.com/problems/mini-parser">385. Mini Parser</a></h2><h3>Medium</h3><hr><p>Given a string s represents the serialization of a nested list, implement a parser to deserialize it and return <em>the deserialized</em> <code>NestedInteger</code>.</p>
|
||||||
|
|
||||||
|
<p>Each element is either an integer or a list whose elements may also be integers or other lists.</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong class="example">Example 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> s = "324"
|
||||||
|
<strong>Output:</strong> 324
|
||||||
|
<strong>Explanation:</strong> You should return a NestedInteger object which contains a single integer 324.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> s = "[123,[456,[789]]]"
|
||||||
|
<strong>Output:</strong> [123,[456,[789]]]
|
||||||
|
<strong>Explanation:</strong> Return a NestedInteger object containing a nested list with 2 elements:
|
||||||
|
1. An integer containing value 123.
|
||||||
|
2. A nested list containing two elements:
|
||||||
|
i. An integer containing value 456.
|
||||||
|
ii. A nested list with one element:
|
||||||
|
a. An integer containing value 789
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong>Constraints:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= s.length <= 5 * 10<sup>4</sup></code></li>
|
||||||
|
<li><code>s</code> consists of digits, square brackets <code>"[]"</code>, negative sign <code>'-'</code>, and commas <code>','</code>.</li>
|
||||||
|
<li><code>s</code> is the serialization of valid <code>NestedInteger</code>.</li>
|
||||||
|
<li>All the values in the input are in the range <code>[-10<sup>6</sup>, 10<sup>6</sup>]</code>.</li>
|
||||||
|
</ul>
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
class Solution:
|
||||||
|
def checkValidString(self, s: str) -> bool:
|
||||||
|
"""
|
||||||
|
Complexities:
|
||||||
|
Time: O(n)
|
||||||
|
Space: O(n)
|
||||||
|
|
||||||
|
where n = len(s)
|
||||||
|
"""
|
||||||
|
|
||||||
|
bracketCount = 0
|
||||||
|
starCount = 0
|
||||||
|
starIndecies = []
|
||||||
|
usedStars = 0
|
||||||
|
|
||||||
|
# Forward pass - allocate as many *s to be (
|
||||||
|
# - when allocating a star, always use the star that appears first
|
||||||
|
for i, l in enumerate(s):
|
||||||
|
if l == '(':
|
||||||
|
bracketCount += 1
|
||||||
|
elif l == '*':
|
||||||
|
starCount += 1
|
||||||
|
starIndecies.append(i)
|
||||||
|
elif bracketCount > 0:
|
||||||
|
bracketCount -= 1
|
||||||
|
elif starCount > 0:
|
||||||
|
starCount -= 1
|
||||||
|
usedStars += 1
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
# There are already matching opening/closing brackets
|
||||||
|
# - all stars are assigned empty string
|
||||||
|
if bracketCount == 0:
|
||||||
|
return True
|
||||||
|
|
||||||
|
# There are more closed brackets needed than the amount of remaining stars
|
||||||
|
if bracketCount > 0 and bracketCount > starCount:
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Backward pass - allocate as many *s to be )
|
||||||
|
# - the first `usedStars` stars are considered used from the forward pass
|
||||||
|
firstViableStar = starIndecies[usedStars]
|
||||||
|
bracketCount = 0
|
||||||
|
starCount = 0
|
||||||
|
for i in range(len(s) - 1, 0, -1):
|
||||||
|
l = s[i]
|
||||||
|
|
||||||
|
if l == ')':
|
||||||
|
bracketCount += 1
|
||||||
|
elif l == '*':
|
||||||
|
starCount += 1 if i >= firstViableStar else 0
|
||||||
|
elif bracketCount > 0:
|
||||||
|
bracketCount -= 1
|
||||||
|
elif starCount > 0:
|
||||||
|
starIndecies.pop(0)
|
||||||
|
starCount -= 1
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
<h2><a href="https://leetcode.com/problems/valid-parenthesis-string">678. Valid Parenthesis String</a></h2><h3>Medium</h3><hr><p>Given a string <code>s</code> containing only three types of characters: <code>'('</code>, <code>')'</code> and <code>'*'</code>, return <code>true</code> <em>if</em> <code>s</code> <em>is <strong>valid</strong></em>.</p>
|
||||||
|
|
||||||
|
<p>The following rules define a <strong>valid</strong> string:</p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>Any left parenthesis <code>'('</code> must have a corresponding right parenthesis <code>')'</code>.</li>
|
||||||
|
<li>Any right parenthesis <code>')'</code> must have a corresponding left parenthesis <code>'('</code>.</li>
|
||||||
|
<li>Left parenthesis <code>'('</code> must go before the corresponding right parenthesis <code>')'</code>.</li>
|
||||||
|
<li><code>'*'</code> could be treated as a single right parenthesis <code>')'</code> or a single left parenthesis <code>'('</code> or an empty string <code>""</code>.</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong class="example">Example 1:</strong></p>
|
||||||
|
<pre><strong>Input:</strong> s = "()"
|
||||||
|
<strong>Output:</strong> true
|
||||||
|
</pre><p><strong class="example">Example 2:</strong></p>
|
||||||
|
<pre><strong>Input:</strong> s = "(*)"
|
||||||
|
<strong>Output:</strong> true
|
||||||
|
</pre><p><strong class="example">Example 3:</strong></p>
|
||||||
|
<pre><strong>Input:</strong> s = "(*))"
|
||||||
|
<strong>Output:</strong> true
|
||||||
|
</pre>
|
||||||
|
<p> </p>
|
||||||
|
<p><strong>Constraints:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= s.length <= 100</code></li>
|
||||||
|
<li><code>s[i]</code> is <code>'('</code>, <code>')'</code> or <code>'*'</code>.</li>
|
||||||
|
</ul>
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
class Solution:
|
||||||
|
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
|
||||||
|
"""
|
||||||
|
Complexities:
|
||||||
|
Time: O(n)
|
||||||
|
Space: O(n)
|
||||||
|
|
||||||
|
where n = len(asteroids)
|
||||||
|
"""
|
||||||
|
|
||||||
|
# iterate left to right
|
||||||
|
# - if right (+ve), keep stack
|
||||||
|
# - if left (-ve), compare/consume top of stack
|
||||||
|
# - - if left consume all of stack, add to output (it will survive)
|
||||||
|
|
||||||
|
stack = []
|
||||||
|
survivedAsteroids = []
|
||||||
|
for i in range(len(asteroids)):
|
||||||
|
if asteroids[i] > 0:
|
||||||
|
stack.append(asteroids[i])
|
||||||
|
else:
|
||||||
|
isDestroyed = False
|
||||||
|
while len(stack) > 0 and abs(asteroids[i]) >= stack[-1]:
|
||||||
|
if abs(asteroids[i]) == stack[-1]:
|
||||||
|
isDestroyed = True
|
||||||
|
stack.pop()
|
||||||
|
break
|
||||||
|
stack.pop()
|
||||||
|
|
||||||
|
if len(stack) == 0 and not isDestroyed:
|
||||||
|
survivedAsteroids.append(asteroids[i])
|
||||||
|
|
||||||
|
|
||||||
|
# also return stack
|
||||||
|
survivedAsteroids += stack
|
||||||
|
|
||||||
|
return survivedAsteroids
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
<h2><a href="https://leetcode.com/problems/asteroid-collision">735. Asteroid Collision</a></h2><h3>Medium</h3><hr><p>We are given an array <code>asteroids</code> of integers representing asteroids in a row. The indices of the asteroid in the array represent their relative position in space.</p>
|
||||||
|
|
||||||
|
<p>For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed.</p>
|
||||||
|
|
||||||
|
<p>Find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong class="example">Example 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> asteroids = [5,10,-5]
|
||||||
|
<strong>Output:</strong> [5,10]
|
||||||
|
<strong>Explanation:</strong> The 10 and -5 collide resulting in 10. The 5 and 10 never collide.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> asteroids = [8,-8]
|
||||||
|
<strong>Output:</strong> []
|
||||||
|
<strong>Explanation:</strong> The 8 and -8 collide exploding each other.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> asteroids = [10,2,-5]
|
||||||
|
<strong>Output:</strong> [10]
|
||||||
|
<strong>Explanation:</strong> The 2 and -5 collide resulting in -5. The 10 and -5 collide resulting in 10.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 4:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> asteroids = [3,5,-6,2,-1,4]
|
||||||
|
<strong>Output:</strong> [-6,2,4]
|
||||||
|
<strong>Explanation:</strong> The asteroid -6 makes the asteroid 3 and 5 explode, and then continues going left. On the other side, the asteroid 2 makes the asteroid -1 explode and then continues going right, without reaching asteroid 4.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong>Constraints:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>2 <= asteroids.length <= 10<sup>4</sup></code></li>
|
||||||
|
<li><code>-1000 <= asteroids[i] <= 1000</code></li>
|
||||||
|
<li><code>asteroids[i] != 0</code></li>
|
||||||
|
</ul>
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
class Solution:
|
||||||
|
def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:
|
||||||
|
"""
|
||||||
|
Complexities:
|
||||||
|
Time: O(n^2)
|
||||||
|
Space: O(n)
|
||||||
|
|
||||||
|
where n = len(deck)
|
||||||
|
"""
|
||||||
|
# run through a sample deck, save ordering
|
||||||
|
sample = [i for i in range(len(deck))]
|
||||||
|
|
||||||
|
revealedCards = []
|
||||||
|
while len(revealedCards) < len(deck):
|
||||||
|
revealedCards.append(sample[0])
|
||||||
|
sample.pop(0)
|
||||||
|
|
||||||
|
if len(sample) == 0:
|
||||||
|
break
|
||||||
|
|
||||||
|
sample.append(sample[0])
|
||||||
|
sample.pop(0)
|
||||||
|
|
||||||
|
# sort deck
|
||||||
|
deck.sort()
|
||||||
|
|
||||||
|
# replace sample with deck values
|
||||||
|
returnedArr = [-1] * len(deck)
|
||||||
|
for i in range(len(revealedCards)):
|
||||||
|
returnedArr[revealedCards[i]] = deck[i]
|
||||||
|
|
||||||
|
return returnedArr
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
<h2><a href="https://leetcode.com/problems/reveal-cards-in-increasing-order">950. Reveal Cards In Increasing Order</a></h2><h3>Medium</h3><hr><p>You are given an integer array <code>deck</code>. There is a deck of cards where every card has a unique integer. The integer on the <code>i<sup>th</sup></code> card is <code>deck[i]</code>.</p>
|
||||||
|
|
||||||
|
<p>You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck.</p>
|
||||||
|
|
||||||
|
<p>You will do the following steps repeatedly until all cards are revealed:</p>
|
||||||
|
|
||||||
|
<ol>
|
||||||
|
<li>Take the top card of the deck, reveal it, and take it out of the deck.</li>
|
||||||
|
<li>If there are still cards in the deck then put the next top card of the deck at the bottom of the deck.</li>
|
||||||
|
<li>If there are still unrevealed cards, go back to step 1. Otherwise, stop.</li>
|
||||||
|
</ol>
|
||||||
|
|
||||||
|
<p>Return <em>an ordering of the deck that would reveal the cards in increasing order</em>.</p>
|
||||||
|
|
||||||
|
<p><strong>Note</strong> that the first entry in the answer is considered to be the top of the deck.</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong class="example">Example 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> deck = [17,13,11,2,3,5,7]
|
||||||
|
<strong>Output:</strong> [2,13,3,11,5,17,7]
|
||||||
|
<strong>Explanation:</strong>
|
||||||
|
We get the deck in the order [17,13,11,2,3,5,7] (this order does not matter), and reorder it.
|
||||||
|
After reordering, the deck starts as [2,13,3,11,5,17,7], where 2 is the top of the deck.
|
||||||
|
We reveal 2, and move 13 to the bottom. The deck is now [3,11,5,17,7,13].
|
||||||
|
We reveal 3, and move 11 to the bottom. The deck is now [5,17,7,13,11].
|
||||||
|
We reveal 5, and move 17 to the bottom. The deck is now [7,13,11,17].
|
||||||
|
We reveal 7, and move 13 to the bottom. The deck is now [11,17,13].
|
||||||
|
We reveal 11, and move 17 to the bottom. The deck is now [13,17].
|
||||||
|
We reveal 13, and move 17 to the bottom. The deck is now [17].
|
||||||
|
We reveal 17.
|
||||||
|
Since all the cards revealed are in increasing order, the answer is correct.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> deck = [1,1000]
|
||||||
|
<strong>Output:</strong> [1,1000]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong>Constraints:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= deck.length <= 1000</code></li>
|
||||||
|
<li><code>1 <= deck[i] <= 10<sup>6</sup></code></li>
|
||||||
|
<li>All the values of <code>deck</code> are <strong>unique</strong>.</li>
|
||||||
|
</ul>
|
||||||
Reference in New Issue
Block a user