Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions best-time-to-buy-and-sell-stock/changhyumm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def maxProfit(self, prices: List[int]) -> int:
min_price = prices[0]
max_profit = 0
for i in range(0, len(prices) - 1):
min_price = min(prices[i], min_price)
profit = prices[i+1] - min_price
max_profit = max(profit, max_profit)
return max_profit
23 changes: 23 additions & 0 deletions container-with-most-water/changhyumm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution:
def maxArea(self, height: List[int]) -> int:
# 시간복잡도 O(n2)으로 타임아웃
# max_amount = 0
# for i in range(len(height) - 1):
# for j in range(i + 1, len(height)):
# amount = (j-i) * min(height[i], height[j])
# max_amount = max(amount, max_amount)
# return max_amount

# 투포인터 활용
# 시간복잡도 O(n)
max_amount = 0
left = 0
right = len(height) - 1
while left < right:
amount = (right - left) * min(height[left], height[right])
max_amount = max(amount, max_amount)
if height[left] < height[right]:
left += 1
else:
right -= 1
return max_amount
40 changes: 40 additions & 0 deletions design-add-and-search-words-data-structure/changhyumm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# prefix 기반으로 탐색할 수 있는 트라이노드 자료구조 활용
class TrieNode:
def __init__(self):
self.children = {}
self.is_ending = False

class WordDictionary:

def __init__(self):
self.root = TrieNode()

def addWord(self, word: str) -> None:
current_node = self.root
for char in word:
if char not in current_node.children:
current_node.children[char] = TrieNode()
current_node = current_node.children[char]
current_node.is_ending = True

def search(self, word):
def dfs(node, index):
# 단어자리수만큼 왔고, 끝단어인경우 찾기 성공
if index == len(word):
return node.is_ending
# 와일드카드인 경우 자식들 전부 탐색
if word[index] == ".":
for child in node.children.values():
if dfs(child, index+1):
return True
# 일반적인 경우 다음 자식 탐색
if word[index] in node.children:
return dfs(node.children[word[index]], index+1)
return False
return dfs(self.root, 0)


# Your WordDictionary object will be instantiated and called as such:
# obj = WordDictionary()
# obj.addWord(word)
# param_2 = obj.search(word)
14 changes: 14 additions & 0 deletions longest-increasing-subsequence/changhyumm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
# dp[i] = nums[0]부터 nums[i]까지의 LIS 길이
# 처음은 1
n = len(nums)
dp = [1] * n

for i in range(1, n):
for j in range(i):
if nums[j] < nums[i]:
# nums[i]가 더 큰 경우 길이 + 1
dp[i] = max(dp[i], dp[j] + 1)

return max(dp) # 전체 중 최대
20 changes: 20 additions & 0 deletions valid-parentheses/changhyumm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution:
def isValid(self, s: str) -> bool:
brackets = {"(": ")", "{": "}", "[": "]"}
stack = []
# 괄호의 특성상 stack 자료구조 활용
# 시간 복잡도 O(n)
# 공간 복잡도 O(n)
for string in s:
if string not in brackets:
if len(stack) == 0:
return False
else:
target = stack.pop()
if string != target:
return False
else:
stack.append(brackets[string])
if stack:
return False
return True
Loading