Skip to content

Commit 59dbed6

Browse files
authored
Merge pull request #22 from tiationg-kho/update
update
2 parents 622ef53 + 34bb67a commit 59dbed6

14 files changed

+363
-29
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,14 @@ Pattern500 is inspired by Blind 75 and Grind 75's classic problem selection. Pat
312312
| 298 | | [346. Moving Average from Data Stream](https://leetcode.com/problems/moving-average-from-data-stream/) | [I]stack-queue | use queue to simulate | [Python]([I]stack-queue/[I]stack-queue/346-moving-average-from-data-stream.py) |
313313
| 299 | O | [20. Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [I]stack-queue | use stack to store the last states | [Python]([I]stack-queue/[I]stack-queue/20-valid-parentheses.py) |
314314
| 300 | O | [150. Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) | [I]stack-queue | use stack to store the last states | [Python]([I]stack-queue/[I]stack-queue/150-evaluate-reverse-polish-notation.py) |
315-
| 301 | O | [XX]() | [X]XX | XX | [Python]() |
316-
| 302 | O | [XX]() | [X]XX | XX | [Python]() |
317-
| 303 | O | [XX]() | [X]XX | XX | [Python]() |
318-
| 304 | O | [XX]() | [X]XX | XX | [Python]() |
319-
| 305 | O | [XX]() | [X]XX | XX | [Python]() |
320-
| 306 | O | [XX]() | [X]XX | XX | [Python]() |
321-
| 307 | O | [XX]() | [X]XX | XX | [Python]() |
322-
| 308 | O | [XX]() | [X]XX | XX | [Python]() |
315+
| 301 | O | [224. Basic Calculator](https://leetcode.com/problems/basic-calculator/) | [I]stack-queue | use stack to store the last states | [Python]([I]stack-queue/[I]stack-queue/224-basic-calculator.py) |
316+
| 302 | O | [227. Basic Calculator II](https://leetcode.com/problems/basic-calculator-ii/) | [I]stack-queue | use stack to store the last states | [Python]([I]stack-queue/[I]stack-queue/227-basic-calculator-ii.py) |
317+
| 303 | | [772. Basic Calculator III](https://leetcode.com/problems/basic-calculator-iii/) | [I]stack-queue | use stack to store the last states | [Python]([I]stack-queue/[I]stack-queue/772-basic-calculator-iii.py) |
318+
| 304 | O | [735. Asteroid Collision](https://leetcode.com/problems/asteroid-collision/) | [I]stack-queue | use stack to store the last states | [Python]([I]stack-queue/[I]stack-queue/735-asteroid-collision.py) |
319+
| 305 | O | [394. Decode String](https://leetcode.com/problems/decode-string/) | [I]stack-queue | use stack to store the last states | [Python]([I]stack-queue/[I]stack-queue/394-decode-string.py) |
320+
| 306 | | [1209. Remove All Adjacent Duplicates in String II](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/) | [I]stack-queue | use stack to store the last states | [Python]([I]stack-queue/[I]stack-queue/1209-remove-all-adjacent-duplicates-in-string-ii.py) |
321+
| 307 | | [726. Number of Atoms](https://leetcode.com/problems/number-of-atoms/) | [I]stack-queue | use stack to store the last states | [Python]([I]stack-queue/[I]stack-queue/726-number-of-atoms.py) |
322+
| 308 | | [71. Simplify Path](https://leetcode.com/problems/simplify-path/) | [I]stack-queue | use stack to store the last states | [Python]([I]stack-queue/[I]stack-queue/71-simplify-path.py) |
323323
| 309 | O | [XX]() | [X]XX | XX | [Python]() |
324324
| 310 | O | [XX]() | [X]XX | XX | [Python]() |
325325
| 311 | O | [XX]() | [X]XX | XX | [Python]() |

[H]array/array.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,3 +511,4 @@ two pointers opposite direction
511511
- like median
512512
- **use merge sort**
513513
- involve divide and conquer’s idea
514+
- during merging, we can utilize the relationship between left part elements and right part elements
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def removeDuplicates(self, s: str, k: int) -> str:
3+
stack = []
4+
for c in s:
5+
if not stack or stack[- 1][0] != c:
6+
stack.append([c, 1])
7+
else:
8+
stack[- 1][1] += 1
9+
if stack[- 1][1] == k:
10+
stack.pop()
11+
res = ''
12+
while stack:
13+
c, f = stack.pop()
14+
res = c * f + res
15+
return res
16+
17+
# time O(n), n is the string's length
18+
# space O(n), due to stack
19+
# using stack and queue and use stack to store the last states

[I]stack-queue/[I]stack-queue/150-evaluate-reverse-polish-notation.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
class Solution:
22
def evalRPN(self, tokens: List[str]) -> int:
3-
stack = []
3+
num_stack = []
4+
cal_stack = []
45
for t in tokens:
56
if t in '+-*/':
6-
second_num = stack.pop()
7-
first_num = stack.pop()
7+
num2 = num_stack.pop()
8+
num1 = num_stack.pop()
89
if t == '+':
9-
stack.append(first_num + second_num)
10+
num_stack.append(num1 + num2)
1011
elif t == '-':
11-
stack.append(first_num - second_num)
12+
num_stack.append(num1 - num2)
1213
elif t == '*':
13-
stack.append(first_num * second_num)
14+
num_stack.append(num1 * num2)
1415
else:
15-
stack.append(int(first_num / second_num))
16+
num_stack.append(int(num1 / num2))
1617
else:
17-
stack.append(int(t))
18-
19-
return stack[- 1]
18+
num_stack.append(int(t))
19+
return num_stack[- 1]
2020

2121
# time O(n), due to traverse
2222
# space O(n), due to stack
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
class Solution:
22
def isValid(self, s: str) -> bool:
3-
stack = []
43
close_open = {')': '(', '}': '{', ']': '['}
4+
stack = []
55
for c in s:
66
if c not in close_open:
77
stack.append(c)
88
else:
9-
if stack and close_open[c] == stack[- 1]:
10-
stack.pop()
11-
else:
9+
if not stack or stack[- 1] != close_open[c]:
1210
return False
13-
return len(stack) == 0
11+
stack.pop()
12+
return not stack
1413

1514
# time O(n)
1615
# space O(n)
17-
# using stack and queue and use stack to store the last states and stack to check (LIFO) and hashmap
16+
# using stack and queue and use stack to store the last states and hashmap
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution:
2+
def calculate(self, s: str) -> int:
3+
s = '(' + s + ')'
4+
s = s.replace(' ', '')
5+
s = s.replace('(-', '(0-')
6+
7+
num_stack = []
8+
cal_stack = []
9+
10+
def calc():
11+
num2 = num_stack.pop()
12+
num1 = num_stack.pop()
13+
cal = cal_stack.pop()
14+
if cal == '+':
15+
num_stack.append(num1 + num2)
16+
else:
17+
num_stack.append(num1 - num2)
18+
19+
num = ''
20+
for i in range(len(s)):
21+
if s[i] == '(':
22+
cal_stack.append(s[i])
23+
elif s[i] == ')':
24+
while cal_stack and cal_stack[- 1] != '(':
25+
calc()
26+
cal_stack.pop()
27+
elif s[i] in '+-':
28+
while cal_stack and cal_stack[- 1] != '(':
29+
calc()
30+
cal_stack.append(s[i])
31+
else:
32+
num += s[i]
33+
if not s[i + 1].isdigit():
34+
num_stack.append(int(num))
35+
num = ''
36+
37+
return num_stack[- 1]
38+
39+
# time O(n)
40+
# space O(n)
41+
# using stack and queue and use stack to store the last states
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution:
2+
def calculate(self, s: str) -> int:
3+
s = s.replace(' ', '')
4+
5+
num_stack = []
6+
cal_stack = []
7+
8+
def calc():
9+
num2 = num_stack.pop()
10+
num1 = num_stack.pop()
11+
cal = cal_stack.pop()
12+
if cal == '+':
13+
num_stack.append(num1 + num2)
14+
elif cal == '-':
15+
num_stack.append(num1 - num2)
16+
elif cal == '*':
17+
num_stack.append(num1 * num2)
18+
else:
19+
num_stack.append(int(num1 / num2))
20+
21+
cal_weight = {'+': 0, '-': 0, '*': 1, '/': 1}
22+
num = ''
23+
for i in range(len(s)):
24+
if s[i] in '+-*/':
25+
while cal_stack and cal_weight[cal_stack[- 1]] >= cal_weight[s[i]]:
26+
calc()
27+
cal_stack.append(s[i])
28+
else:
29+
num += s[i]
30+
if i == len(s) - 1 or (i + 1 < len(s) and not s[i + 1].isdigit()):
31+
num_stack.append(int(num))
32+
num = ''
33+
34+
while cal_stack:
35+
calc()
36+
37+
return num_stack[- 1]
38+
39+
# time O(n)
40+
# space O(n)
41+
# using stack and queue and use stack to store the last states

[I]stack-queue/[I]stack-queue/346-moving-average-from-data-stream.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
class MovingAverage:
33

44
def __init__(self, size: int):
5+
self.queue = deque([])
56
self.size = size
67
self.total = 0
7-
self.queue = deque([])
88

99
def next(self, val: int) -> float:
1010
if len(self.queue) == self.size:
@@ -19,4 +19,4 @@ def next(self, val: int) -> float:
1919

2020
# time O(1)
2121
# space O(n)
22-
# using stacka and queue and use queue to simulate
22+
# using stack and queue and use queue to simulate
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def decodeString(self, s: str) -> str:
3+
stack = []
4+
5+
cur = ''
6+
num = ''
7+
for i in range(len(s)):
8+
if s[i] == '[':
9+
stack.append((int(num), cur))
10+
num = ''
11+
cur = ''
12+
elif s[i] == ']':
13+
mul, prev = stack.pop()
14+
cur = prev + mul * cur
15+
elif s[i].isdigit():
16+
num += s[i]
17+
else:
18+
cur += s[i]
19+
return cur
20+
21+
# time O(n), not precisely (only true if max freq is small)
22+
# space O(n), not precisely (only true if max freq is small)
23+
# using stack and queue and use stack to store the last states
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def simplifyPath(self, path: str) -> str:
3+
4+
stack = []
5+
path = path.split('/')[1:]
6+
7+
for c in path:
8+
if not c or c == '.':
9+
continue
10+
elif c == '..':
11+
if stack:
12+
stack.pop()
13+
else:
14+
stack.append(c)
15+
16+
return '/' + '/'.join(stack)
17+
18+
# time O(n), due to traverse and split
19+
# space O(n), due to stack and split
20+
# using stack and queue and use stack to store the last states
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from collections import defaultdict
2+
import string
3+
class Solution:
4+
def countOfAtoms(self, formula: str) -> str:
5+
element_freq = defaultdict(int)
6+
mul_stack = []
7+
num = ''
8+
element = ''
9+
mul = 1
10+
for i in range(len(formula) - 1, - 1, - 1):
11+
c = formula[i]
12+
if c == ')':
13+
mul *= int(num) if num else 1
14+
mul_stack.append(int(num) if num else 1)
15+
num = ''
16+
elif c == '(':
17+
mul //= mul_stack.pop()
18+
elif c.isdigit():
19+
num = c + num
20+
else:
21+
element = c + element
22+
if c in string.ascii_uppercase:
23+
element_freq[element] += mul * (int(num) if num else 1)
24+
element = ''
25+
num = ''
26+
27+
res = ''
28+
for e, f in sorted(element_freq.items()):
29+
res += e + (str(f) if f != 1 else '')
30+
return res
31+
32+
# time O(nlogn)
33+
# space O(n), due to stack and hashmap
34+
# using stack and queue and use stack to store the last states and hashmap and sort
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
3+
stack = []
4+
5+
for weight in asteroids:
6+
if not stack:
7+
stack.append(weight)
8+
elif stack[- 1] < 0:
9+
stack.append(weight)
10+
elif stack[- 1] > 0 and weight > 0:
11+
stack.append(weight)
12+
else:
13+
while stack and stack[- 1] > 0 and weight < 0:
14+
if stack[- 1] > abs(weight):
15+
weight = 0
16+
break
17+
elif stack[- 1] == abs(weight):
18+
weight = 0
19+
stack.pop()
20+
break
21+
else:
22+
stack.pop()
23+
if weight:
24+
stack.append(weight)
25+
return stack
26+
27+
# time O(n), each asteroid push and pop at most once
28+
# space O(n), due to stack
29+
# using stack and queue and use stack to store the last states
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution:
2+
def calculate(self, s: str) -> int:
3+
s = '(' + s + ')'
4+
5+
num_stack = []
6+
cal_stack = []
7+
8+
def calc():
9+
num2 = num_stack.pop()
10+
num1 = num_stack.pop()
11+
cal = cal_stack.pop()
12+
if cal == '+':
13+
num_stack.append(num1 + num2)
14+
elif cal == '-':
15+
num_stack.append(num1 - num2)
16+
elif cal == '*':
17+
num_stack.append(num1 * num2)
18+
else:
19+
num_stack.append(int(num1 / num2))
20+
21+
cal_weight = {'+': 0, '-': 0, '*': 1, '/': 1}
22+
num = ''
23+
for i in range(len(s)):
24+
if s[i] == '(':
25+
cal_stack.append(s[i])
26+
elif s[i] == ')':
27+
while cal_stack and cal_stack[- 1] != '(':
28+
calc()
29+
cal_stack.pop()
30+
elif s[i] in '+-*/':
31+
while cal_stack and cal_stack[- 1] != '(' and cal_weight[cal_stack[- 1]] >= cal_weight[s[i]]:
32+
calc()
33+
cal_stack.append(s[i])
34+
else:
35+
num += s[i]
36+
if not s[i + 1].isdigit():
37+
num_stack.append(int(num))
38+
num = ''
39+
return num_stack[- 1]
40+
41+
# time O(n)
42+
# space O(n)
43+
# using stack and queue and use stack to store the last states

0 commit comments

Comments
 (0)