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
18 changes: 18 additions & 0 deletions best-time-to-buy-and-sell-stock/doh6077.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

# 121. Best Time to Buy and Sell Stock
# O(n) - Two Pointers
class Solution:
def maxProfit(self, prices: list[int]) -> int:
left = 0
right = 1
profit = 0
max_profit = 0

while right < len(prices):
if prices[right] > prices[left]:
profit = prices[right] - prices[left]
max_profit = max(profit, max_profit)
else:
left = right
right += 1
return max_profit
32 changes: 32 additions & 0 deletions design-add-and-search-words-data-structure/doh6077.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

# 208. Implement Trie (Prefix Tree)
class Trie:

def __init__(self):
self.list = []


def insert(self, word: str) -> None:
self.list.append(word)


def search(self, word: str) -> bool:
if word in self.list:
return True
else:
return False


def startsWith(self, prefix: str) -> bool:
for i, value in enumerate(self.list):
if value.startswith(prefix):
return True
return False
Comment on lines +1 to +24
Copy link
Contributor

Choose a reason for hiding this comment

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

이거 파일을 다른 폴더에 넣으신 것 같습니다!

Copy link
Contributor

Choose a reason for hiding this comment

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

그리고, 통과는 되었지만, Trie 구조와는 거리가 먼 것으로 보여서, 시간이 되시면, 한 번 만들어 보시는 것도 좋을 것 같습니다!




# 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)
26 changes: 26 additions & 0 deletions encode-and-decode-strings/doh6077.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@


# 271. Encode and Decode Strings

# integer represents the following word's length
# read until we reach to delimiter

class Solution:
def encode(self, strs):
res = ""
for s in strs:
res += str(len(s)) + "#" + s
return res

def decode(self, str):
res, i = [], 0

while i < len(str):
j = i
while str[j] != "#":
j += 1
length = int(str[i:j])
res.append(str[j + 1 : j + 1 + length])
i = j + 1 + length

return res
16 changes: 16 additions & 0 deletions group-anagrams/doh6077.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from collections import defaultdict
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
# hashmap
# calculate frequency
# 26 alphabet characters
anagrams_dict = defaultdict(list)
for s in strs:
count = [0] * 26
for c in s:
count[ord(c) - ord('a')] += 1

key = tuple(count)
anagrams_dict[key].append(s)

return list(anagrams_dict.values())
12 changes: 12 additions & 0 deletions word-break/doh6077.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
dp = [True] + [False] * len(s)

for i in range(1, len(s) + 1):
for w in wordDict:
start = i - len(w)
if start >= 0 and dp[start] and s[start:i] == w:
dp[i] = True
break
Copy link
Contributor

Choose a reason for hiding this comment

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

해당 문제를 LIS dp 문제와 유사하게 풀 수 있었군요. 좋은 경험이었습니다.


return dp[-1]