Skip to content

Commit 97b7dc8

Browse files
authored
Merge pull request #1436 from i-mprovising/main
[i-mprovising] Week 06 solutions
2 parents 709e6fa + f333b6b commit 97b7dc8

File tree

5 files changed

+177
-0
lines changed

5 files changed

+177
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
Time complexity O(n)
3+
Space complexity O(1)
4+
5+
Two pointer
6+
"""
7+
8+
class Solution:
9+
def maxArea(self, height: List[int]) -> int:
10+
s, e = 0, len(height) - 1
11+
area = 0 # max area
12+
while s < e:
13+
h = min(height[s], height[e])
14+
tmp = h * (e - s) # area at current iteration
15+
area = max(area, tmp)
16+
# move pointer
17+
if height[s] < height[e]:
18+
s += 1
19+
else:
20+
e -= 1
21+
22+
return area
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
Time complexity O(26^w)
3+
Space complexity O(w)
4+
5+
Trie, DFS
6+
"""
7+
8+
class Node:
9+
def __init__(self, end=False):
10+
self.children = {} # char : node
11+
self.end = end
12+
13+
class WordDictionary:
14+
15+
def __init__(self):
16+
self.root = Node(end=True)
17+
18+
def addWord(self, word: str) -> None:
19+
node = self.root
20+
for ch in word:
21+
if ch not in node.children:
22+
node.children[ch] = Node()
23+
node = node.children[ch]
24+
node.end = True
25+
26+
def search(self, word: str) -> bool:
27+
return self.dfs(self.root, word)
28+
29+
def dfs(self, start, word):
30+
ch = word[0]
31+
if ch == word:
32+
if ch == '.':
33+
return any([node.end for node in start.children.values()])
34+
elif ch in start.children:
35+
if start.children[ch].end:
36+
return True
37+
return False
38+
39+
if ch == '.':
40+
return any([self.dfs(node, word[1:]) for c, node in start.children.items()])
41+
elif ch in start.children:
42+
return self.dfs(start.children[ch], word[1:])
43+
else:
44+
return False
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Time complexity O(n^2)
3+
Space complexity O(n)
4+
5+
Dynamic programming
6+
"""
7+
8+
class Solution:
9+
def lengthOfLIS(self, nums: List[int]) -> int:
10+
11+
dp = [(1, nums[0])] # sequence len, max num in sequence
12+
13+
for i in range(1, len(nums)):
14+
num = nums[i]
15+
max_len = 1
16+
for j in range(i):
17+
x, y = dp[j]
18+
if y < num:
19+
if max_len < x + 1:
20+
max_len = x + 1
21+
dp.append((max_len, num))
22+
23+
# find max len
24+
max_len = 0
25+
for x in dp:
26+
if x[0] > max_len:
27+
max_len = x[0]
28+
29+
return max_len

spiral-matrix/i-mprovising.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
Time complexity O(m*n)
3+
4+
단순구현
5+
"""
6+
7+
class Solution:
8+
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
9+
"""
10+
111
11+
1M1 visited
12+
111
13+
"""
14+
m, n = len(matrix), len(matrix[0])
15+
16+
board = [[0] * (n+2)]
17+
for i in range(m):
18+
tmp = [0] + matrix[i] + [0]
19+
board.append(tmp)
20+
board.append([0] * (n+2))
21+
22+
visited = [[True] * (n+1)]
23+
for _ in range(m):
24+
tmp = [True] + [False] * n + [True]
25+
visited.append(tmp)
26+
visited.append([True] * (n+1))
27+
28+
direction = 0
29+
x, y = 1, 1
30+
numbers = []
31+
32+
for _ in range(m * n):
33+
numbers.append(board[x][y])
34+
visited[x][y] = True
35+
36+
i, j = self.next_idx(direction, x, y)
37+
if visited[i][j]:
38+
direction = self.change_dir(direction)
39+
x, y = self.next_idx(direction, x, y)
40+
else:
41+
x, y = i, j
42+
43+
return numbers
44+
45+
def next_idx(self, dir, x, y):
46+
"""
47+
0 1 2 3 : R D L U
48+
"""
49+
if dir == 0:
50+
y += 1
51+
elif dir == 1:
52+
x += 1
53+
elif dir == 2:
54+
y -= 1
55+
else:
56+
x -= 1
57+
return x, y
58+
59+
def change_dir(self, dir):
60+
return (dir + 1) % 4

valid-parentheses/i-mprovising.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
Time complexity O(n)
3+
4+
stack
5+
"""
6+
class Solution:
7+
def isValid(self, s: str) -> bool:
8+
open_brackets = ["(", "{", "["]
9+
bracket_map = {"(":")", "{":"}", "[":"]"}
10+
stack = []
11+
for char in s:
12+
if char in open_brackets:
13+
stack.append(char)
14+
continue
15+
if not stack:
16+
return False
17+
open_b = stack.pop() # last in open bracket
18+
if bracket_map[open_b] != char:
19+
return False
20+
if stack:
21+
return False
22+
return True

0 commit comments

Comments
 (0)