Skip to content

Commit 05d427a

Browse files
authored
Merge pull request #1415 from sejineer/main
[sejineer] Week 06 solutions
2 parents 1815356 + a8cc3c8 commit 05d427a

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed

container-with-most-water/sejineer.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
시간 복잡도: O(N)
3+
공간 복잡도: O(1)
4+
"""
5+
class Solution:
6+
def maxArea(self, height: List[int]) -> int:
7+
result = 0
8+
start, end = 0, len(height) - 1
9+
10+
while start < end:
11+
area = (end - start) * min(height[start], height[end])
12+
result = max(result, area)
13+
if height[start] < height[end]:
14+
start += 1
15+
else:
16+
end -= 1
17+
return result
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class WordDictionary:
2+
3+
def __init__(self):
4+
self.root = {"$": True}
5+
6+
7+
def addWord(self, word: str) -> None:
8+
node = self.root
9+
for ch in word:
10+
if ch not in node:
11+
node[ch] = {"$": False}
12+
node = node[ch]
13+
node["$"] = True
14+
15+
def search(self, word: str) -> bool:
16+
def dfs(node, idx):
17+
if idx == len(word):
18+
return node["$"]
19+
ch = word[idx]
20+
if ch in node:
21+
return dfs(node[ch], idx + 1)
22+
elif ch == ".":
23+
return any(dfs(node[k], idx + 1) for k in node if k != "$")
24+
else:
25+
return False
26+
return dfs(self.root, 0)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
시간 복잡도: O(Nlog(N))
3+
공간 복잡도: O(N)
4+
"""
5+
from bisect import bisect_left
6+
7+
class Solution:
8+
def lengthOfLIS(self, nums: List[int]) -> int:
9+
sub = []
10+
for num in nums:
11+
index = bisect_left(sub, num)
12+
if index == len(sub):
13+
sub.append(num)
14+
else:
15+
sub[index] = num
16+
return len(sub)

spiral-matrix/sejineer.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
시간 복잡도: O(n * m)
3+
공간 복잡도: O(n * m)
4+
"""
5+
class Solution:
6+
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
7+
n, m = len(matrix), len(matrix[0])
8+
dx = [1, 0, -1, 0]
9+
dy = [0, 1, 0, -1]
10+
state = 0
11+
vis = [[False] * m for _ in range(n)]
12+
13+
result = [matrix[0][0]]
14+
vis[0][0] = True
15+
x, y = 0, 0
16+
17+
while len(result) < n * m:
18+
nx = x + dx[state % 4]
19+
ny = y + dy[state % 4]
20+
21+
if not (0 <= nx < m) or not (0 <= ny < n) or vis[ny][nx]:
22+
state += 1
23+
continue
24+
25+
vis[ny][nx] = True
26+
result.append(matrix[ny][nx])
27+
x, y = nx, ny
28+
29+
return result

valid-parentheses/sejineer.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
시간 복잡도: O(N)
3+
공간 복잡도: O(N)
4+
"""
5+
class Solution:
6+
def isValid(self, s: str) -> bool:
7+
pair = {'(': ')', '{': '}', '[': ']'}
8+
stack = []
9+
for c in s:
10+
if c in ('(', '{', '['):
11+
stack.append(c)
12+
else:
13+
if stack:
14+
cur = stack.pop()
15+
if pair[cur] == c:
16+
continue
17+
else:
18+
return False
19+
else:
20+
return False
21+
return not stack

0 commit comments

Comments
 (0)