Skip to content

Commit 50125ca

Browse files
authored
Merge pull request #2199 from changhyumm/main
[changhyumm] WEEK 06 solutions
2 parents db2f018 + c02b964 commit 50125ca

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def maxProfit(self, prices: List[int]) -> int:
3+
min_price = prices[0]
4+
max_profit = 0
5+
for i in range(0, len(prices) - 1):
6+
min_price = min(prices[i], min_price)
7+
profit = prices[i+1] - min_price
8+
max_profit = max(profit, max_profit)
9+
return max_profit
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def maxArea(self, height: List[int]) -> int:
3+
# 시간복잡도 O(n2)으로 타임아웃
4+
# max_amount = 0
5+
# for i in range(len(height) - 1):
6+
# for j in range(i + 1, len(height)):
7+
# amount = (j-i) * min(height[i], height[j])
8+
# max_amount = max(amount, max_amount)
9+
# return max_amount
10+
11+
# 투포인터 활용
12+
# 시간복잡도 O(n)
13+
max_amount = 0
14+
left = 0
15+
right = len(height) - 1
16+
while left < right:
17+
amount = (right - left) * min(height[left], height[right])
18+
max_amount = max(amount, max_amount)
19+
if height[left] < height[right]:
20+
left += 1
21+
else:
22+
right -= 1
23+
return max_amount
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# prefix 기반으로 탐색할 수 있는 트라이노드 자료구조 활용
2+
class TrieNode:
3+
def __init__(self):
4+
self.children = {}
5+
self.is_ending = False
6+
7+
class WordDictionary:
8+
9+
def __init__(self):
10+
self.root = TrieNode()
11+
12+
def addWord(self, word: str) -> None:
13+
current_node = self.root
14+
for char in word:
15+
if char not in current_node.children:
16+
current_node.children[char] = TrieNode()
17+
current_node = current_node.children[char]
18+
current_node.is_ending = True
19+
20+
def search(self, word):
21+
def dfs(node, index):
22+
# 단어자리수만큼 왔고, 끝단어인경우 찾기 성공
23+
if index == len(word):
24+
return node.is_ending
25+
# 와일드카드인 경우 자식들 전부 탐색
26+
if word[index] == ".":
27+
for child in node.children.values():
28+
if dfs(child, index+1):
29+
return True
30+
# 일반적인 경우 다음 자식 탐색
31+
if word[index] in node.children:
32+
return dfs(node.children[word[index]], index+1)
33+
return False
34+
return dfs(self.root, 0)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def lengthOfLIS(self, nums: List[int]) -> int:
3+
# dp[i] = nums[0]부터 nums[i]까지의 LIS 길이
4+
# 처음은 1
5+
n = len(nums)
6+
dp = [1] * n
7+
8+
for i in range(1, n):
9+
for j in range(i):
10+
if nums[j] < nums[i]:
11+
# nums[i]가 더 큰 경우 길이 + 1
12+
dp[i] = max(dp[i], dp[j] + 1)
13+
14+
return max(dp) # 전체 중 최대

valid-parentheses/changhyumm.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def isValid(self, s: str) -> bool:
3+
brackets = {"(": ")", "{": "}", "[": "]"}
4+
stack = []
5+
# 괄호의 특성상 stack 자료구조 활용
6+
# 시간 복잡도 O(n)
7+
# 공간 복잡도 O(n)
8+
for string in s:
9+
if string not in brackets:
10+
if len(stack) == 0:
11+
return False
12+
else:
13+
target = stack.pop()
14+
if string != target:
15+
return False
16+
else:
17+
stack.append(brackets[string])
18+
if stack:
19+
return False
20+
return True

0 commit comments

Comments
 (0)