Skip to content

Commit 93bc2f3

Browse files
authored
Merge pull request #2164 from doh6077/main
[doh6077] WEEK 05 Solutions
2 parents 3292cc6 + 4970077 commit 93bc2f3

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
# 121. Best Time to Buy and Sell Stock
3+
# O(n) - Two Pointers
4+
class Solution:
5+
def maxProfit(self, prices: list[int]) -> int:
6+
left = 0
7+
right = 1
8+
profit = 0
9+
max_profit = 0
10+
11+
while right < len(prices):
12+
if prices[right] > prices[left]:
13+
profit = prices[right] - prices[left]
14+
max_profit = max(profit, max_profit)
15+
else:
16+
left = right
17+
right += 1
18+
return max_profit
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
# 208. Implement Trie (Prefix Tree)
3+
class Trie:
4+
5+
def __init__(self):
6+
self.list = []
7+
8+
9+
def insert(self, word: str) -> None:
10+
self.list.append(word)
11+
12+
13+
def search(self, word: str) -> bool:
14+
if word in self.list:
15+
return True
16+
else:
17+
return False
18+
19+
20+
def startsWith(self, prefix: str) -> bool:
21+
for i, value in enumerate(self.list):
22+
if value.startswith(prefix):
23+
return True
24+
return False
25+
26+
27+
28+
# Your Trie object will be instantiated and called as such:
29+
# obj = Trie()
30+
# obj.insert(word)
31+
# param_2 = obj.search(word)
32+
# param_3 = obj.startsWith(prefix)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
3+
# 271. Encode and Decode Strings
4+
5+
# integer represents the following word's length
6+
# read until we reach to delimiter
7+
8+
class Solution:
9+
def encode(self, strs):
10+
res = ""
11+
for s in strs:
12+
res += str(len(s)) + "#" + s
13+
return res
14+
15+
def decode(self, str):
16+
res, i = [], 0
17+
18+
while i < len(str):
19+
j = i
20+
while str[j] != "#":
21+
j += 1
22+
length = int(str[i:j])
23+
res.append(str[j + 1 : j + 1 + length])
24+
i = j + 1 + length
25+
26+
return res

group-anagrams/doh6077.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from collections import defaultdict
2+
class Solution:
3+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
4+
# hashmap
5+
# calculate frequency
6+
# 26 alphabet characters
7+
anagrams_dict = defaultdict(list)
8+
for s in strs:
9+
count = [0] * 26
10+
for c in s:
11+
count[ord(c) - ord('a')] += 1
12+
13+
key = tuple(count)
14+
anagrams_dict[key].append(s)
15+
16+
return list(anagrams_dict.values())

word-break/doh6077.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
3+
dp = [True] + [False] * len(s)
4+
5+
for i in range(1, len(s) + 1):
6+
for w in wordDict:
7+
start = i - len(w)
8+
if start >= 0 and dp[start] and s[start:i] == w:
9+
dp[i] = True
10+
break
11+
12+
return dp[-1]

0 commit comments

Comments
 (0)