Skip to content

Commit c57beac

Browse files
authored
Merge pull request #477 from kjb512/main
[kayden] Week 06 Solutions
2 parents ddd3676 + 97dfab3 commit c57beac

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed

container-with-most-water/kayden.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 시간복잡도: O(N)
2+
# 공간복잡도: O(N)
3+
class Solution:
4+
def maxArea(self, height: List[int]) -> int:
5+
l, r = 0, len(height) - 1
6+
answer = 0
7+
8+
while l < r:
9+
10+
if height[l] < height[r]:
11+
answer = max(answer, (r - l) * height[l])
12+
l += 1
13+
else:
14+
answer = max(answer, (r - l) * height[r])
15+
r -= 1
16+
17+
return answer
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Node:
2+
def __init__(self, ending=False):
3+
self.ending = ending
4+
self.children = {}
5+
6+
7+
class WordDictionary:
8+
9+
def __init__(self):
10+
self.head = Node(True)
11+
12+
# 시간복잡도: O(W)
13+
def addWord(self, word: str) -> None:
14+
node = self.head
15+
16+
for ch in word:
17+
if ch not in node.children:
18+
node.children.setdefault(ch, Node())
19+
node = node.children[ch]
20+
21+
node.ending = True
22+
23+
# 시간복잡도: O(W*N) W: word 길이 N: 자식 노드의 개수
24+
def search(self, word: str) -> bool:
25+
def dfs(idx, node):
26+
if idx == len(word):
27+
return node.ending
28+
29+
if word[idx] == '.':
30+
for n in node.children.values():
31+
if dfs(idx + 1, n):
32+
return True
33+
elif word[idx] in node.children:
34+
return dfs(idx + 1, node.children[word[idx]])
35+
else:
36+
return False
37+
38+
return dfs(0, self.head)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 시간복잡도: O(NlogN)
2+
# 공간복잡도: O(N)
3+
from bisect import bisect_left
4+
5+
class Solution:
6+
def lengthOfLIS(self, nums: List[int]) -> int:
7+
path = []
8+
9+
for num in nums:
10+
idx = bisect_left(path, num)
11+
if len(path) > idx:
12+
path[idx] = num
13+
else:
14+
path.append(num)
15+
16+
return len(path)

spiral-matrix/kayden.py

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

valid-parentheses/kayden.py

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

0 commit comments

Comments
 (0)