Skip to content
Merged
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
21 changes: 21 additions & 0 deletions best-time-to-buy-and-sell-stock/gcount85.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
# Approach
지금까지의 최저 가격을 갱신함과 동시에 최선의 이익도 업데이트합니다.

# Complexity
- Time complexity: O(N)

- Space complexity: O(1)
"""


class Solution:
def maxProfit(self, prices: list[int]) -> int:
min_price = float("inf")
answer = 0

for price in prices:
min_price = min(min_price, price)
answer = max(answer, price - min_price)

return answer
21 changes: 21 additions & 0 deletions group-anagrams/gcount85.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
# Approach
strs 배열을 순회하며 문자열을 정규화(정렬)하고,
정규화 값이 같은 원소들끼리 모이도록 딕셔너리에 추가하여 최종 값을 반환한다.

# Complexity
strs의 길이를 N, 문자열의 길이를 K라고 할 때,

- Time complexity: O(N*KlogK)
- Space complexity: O(N*K)
"""

from collections import defaultdict


class Solution:
def groupAnagrams(self, strs: list[str]) -> list[list[str]]:
anagram = defaultdict(list) # normalized str : str list
for s in strs:
anagram["".join(sorted(s))].append(s)
return list(anagram.values())
41 changes: 41 additions & 0 deletions implement-trie-prefix-tree/gcount85.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class TrieNode:
def __init__(self):
self.children = {}
self.is_end = False


class Trie:

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

def insert(self, word: str) -> None:
node = self.root
for ch in word:
if ch not in node.children:
node.children[ch] = TrieNode()
node = node.children[ch]
node.is_end = True

def search(self, word: str) -> bool:
node = self.root
for ch in word:
if ch not in node.children:
return False
node = node.children[ch]
return node.is_end

def startsWith(self, prefix: str) -> bool:
node = self.root
for ch in prefix:
if ch not in node.children:
return False
node = node.children[ch]
return True


# Your Trie object will be instantiated and called as such:
# obj = Trie()
# obj.insert(word)
# param_2 = obj.search(word)
# param_3 = obj.startsWith(prefix)
35 changes: 35 additions & 0 deletions word-break/gcount85.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
# Intuition
wordDict를 word들의 길이로 분류하고,
s의 각 위치까지의 문자열을 완성할 수 있는지 dp 배열로 확인합니다.

# Complexity
wordDict의 길이를 N, s의 길이를 K
- Time complexity: O(N+K)
- Space complexity: O(N+K)
"""

from collections import defaultdict


class Solution:
def wordBreak(self, s: str, wordDict: list[str]) -> bool:
n = len(s)
dp = [False] * (n + 1)
word_dict = defaultdict(set)
for word in wordDict:
word_dict[len(word)].add(word)

dp[0] = True
for i in range(1, n + 1):
for (
k,
v,
) in word_dict.items():
if i + k - 1 > n:
continue
if dp[i - 1] == False:
continue
if s[i - 1 : i + k - 1] in v:
dp[i + k - 1] = True
return dp[n]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오.. 이 문제를 dp로 접근하신 것에 한 번 더 배우고 갑니다!

Loading