Skip to content

[croucs] Week : 5 #863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 12, 2025
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
15 changes: 15 additions & 0 deletions best-time-to-buy-and-sell-stock/heypaprika.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Big-O 예측
# Time : O(n)
# Space : O(1)
class Solution:
def maxProfit(self, prices: List[int]) -> int:
min_price = prices[0]
profit = 0

for price in prices[1:]:
if min_price > price:
min_price = price

profit = max(profit, price - min_price)
return profit

21 changes: 21 additions & 0 deletions encode-and-decode-strings/heypaprika.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Big-O 예측
# Time : O(n)
# Space : O(1)
class Codec:
def encode(self, strs: List[str]) -> str:
"""Encodes a list of strings to a single string.
"""
new_str = "-!@$@#!_".join(strs)
return new_str

def decode(self, s: str) -> List[str]:
"""Decodes a single string to a list of strings.
"""
return s.split("-!@$@#!_")



strs = ["Hello","World"]
codec = Codec()
codec.decode(codec.encode(strs))

23 changes: 23 additions & 0 deletions group-anagrams/heypaprika.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Big-O 예측
# Time : O(k * nlog(n)) (k : strs 배열 수, n : strs 원소의 unique 단어 개수)
# Space : O(k + n)
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
count_dict = {}
num = 0
dicted_list = []
for s in strs:
cur = str(sorted(Counter(s).items()))
dicted_list.append(cur)
if cur in count_dict:
continue
count_dict[cur] = num
num += 1

ans_list = [[] for _ in range(len(count_dict))]
for k, v in count_dict.items():
for i, dicted in enumerate(dicted_list):
if dicted == k:
ans_list[v].append(strs[i])
return ans_list

37 changes: 37 additions & 0 deletions implement-trie-prefix-tree/heypaprika.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Big-O 예측
# Time : O(n) (n : 단어 개수)
# Space : O(n)
class Trie:

def __init__(self):
self.trie_dict = {}
self.trie_dict_list = [{}]

def insert(self, word: str) -> None:
self.trie_dict[word] = 1
for i in range(1, len(word) + 1):
if i > len(self.trie_dict_list) and i > 1:
self.trie_dict_list.append({})
self.trie_dict_list[i-1][word[:i]] = 1

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

def startsWith(self, prefix: str) -> bool:
if len(prefix) > len(self.trie_dict_list):
return False
return prefix in self.trie_dict_list[len(prefix)-1]
Comment on lines +21 to +23
Copy link
Contributor

Choose a reason for hiding this comment

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

길이비교로 예외처리를 먼저 해주는 점 배웠습니다. 👍
return prefix in self.trie_dict_list[len(prefix)-1] 이렇게 처리되는 점 또한 배워갑니다.



# Your Trie object will be instantiated and called as such:
trie = Trie()
trie.insert("apple")
trie.search("apple")
trie.search("app")
trie.startsWith("app")
trie.insert("app")
trie.search("app")

# param_2 = trie.search(word)
# param_3 = trie.startsWith(prefix)

31 changes: 31 additions & 0 deletions word-break/heypaprika.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Big-O 예측
# Time : O(n^2) 최악의 경우 wordDict의 개수의 제곱만큼
# Space : O(n^2) 최악의 경우 a에 wordDict의 개수의 제곱만큼

# 쉽게 접근한 풀이
# 맨 앞의 것을 처리할 수 있는 것을 다 고르기 (startswith)
# 그들을 했을 때의 결과를 가지고 다음 것으로 동일하게 반복 진행하기.

class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
a = [[s]]
i = 1
s_list = None
duplicate_dict = {}
while True:
s_list = a[-1]
a.append([])
for s in s_list:
for word in wordDict:
if s.startswith(word):
surplus_word = s[len(word):]
if surplus_word not in duplicate_dict:
a[-1].append(surplus_word)
duplicate_dict[surplus_word] = 1
# a[-1] = list(set(a[-1]))
if "" in a[-1]:
return True
if not a[-1]:
return False
i += 1

Loading