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
13 changes: 13 additions & 0 deletions best-time-to-buy-and-sell-stock/jinah92.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# O(N) times, O(1) spaces
# price[i] 가격으로 팔아서 가장 수익을 많이 내려면, i-1번째까지 중 가장 값이 싼 날짜에서 주식을 사면 된다
class Solution:
def maxProfit(self, prices: List[int]) -> int:
max_profit = 0
min_price = prices[0]

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

return max_profit
24 changes: 24 additions & 0 deletions encode-and-decode-strings/jinah92.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# O(N) times, O(1) spaces
class Solution:
"""
@param: strs: a list of strings
@return: encodes a list of strings to a single string.
"""
def encode(self, strs):
encoded_str = ""
for str in strs:
encoded_str += f"{len(str)}:{str}"
return encoded_str

"""
@param: str: A string
@return: decodes a single string to a list of strings
"""
def decode(self, str):
list_strs, start = [], 0
while start < len(str):
offset = str.find(":", start)
s_length = int(str[start:offset])
list_strs.append(str[offset+1:offset+1+s_length])
start = offset+1+s_length
return list_strs
24 changes: 24 additions & 0 deletions group-anagrams/jinah92.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# O(N * NlogN) times, O(N) spaces
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
anagrams_dict = {}
result = []

for str in strs: # N 시간 소요
str_count = {}
for s in str:
if not s in str_count:
str_count[s] = 1
else:
str_count[s] += 1
anagrams_keys = []
for key, val in sorted(str_count.items()): # 최대 N개의 dict items를 배열하므로 NlogN 시간 소요
anagrams_keys.append(tuple([key, val]))

anagrams_key = tuple(anagrams_keys)
if tuple(anagrams_keys) not in anagrams_dict:
anagrams_dict[tuple(anagrams_keys)] = []
anagrams_dict[tuple(anagrams_keys)].append(str)


return list(anagrams_dict.values())
16 changes: 16 additions & 0 deletions implement-trie-prefix-tree/jinah92.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 시간복잡도
## insert, search는 해시 기반 조회로 인해 O(1), startsWith은 O(n * k) 시간이 소요 (n: 저장된 단어수, k: prefix 길이)
# 공간복잡도
## O(n * m) 공간이 필요 (n: 저장된 단어 수, m : 각 단어의 평균 길이)
class Trie:
def __init__(self):
self.trie = set()

def insert(self, word: str) -> None:
self.trie.add(word)

def search(self, word: str) -> bool:
return word in self.trie

def startsWith(self, prefix: str) -> bool:
return any(item.startswith(prefix) for item in self.trie)
14 changes: 14 additions & 0 deletions word-break/jinah92.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# O(2^s*w) times, O(s) space
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
@cache
def dfs(start):
if start == len(s):
return True
for word in wordDict:
if s[start:start+len(word)] == word:
if dfs(start+len(word)):
return True
return False

return dfs(0)
Loading