Skip to content

[printjin-gmailcom] Week 05 Solutions #1378

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 9 commits into from
Apr 27, 2025
Merged
10 changes: 10 additions & 0 deletions best-time-to-buy-and-sell-stock/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution:
def maxProfit(self, price):
min_price = float('inf')
max_profit = 0
for price in prices:
if price < min_price:
min_price = price
elif price - min_price > max_profit:
max_profit = price - min_price
return max_profit
18 changes: 18 additions & 0 deletions encode-and-decode-strings/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution:
def encode(self, strs):
res = ''
for s in strs:
res += str(len(s)) + '#' + s
return res

def decode(self, s):
res = []
i = 0
while i < len(s):
j = i
while s[j] != '#':
j += 1
length = int(s[i:j])
res.append(s[j+1:j+1+length])
i = j + 1 + length
return res
11 changes: 11 additions & 0 deletions group-anagrams/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from collections import defaultdict

class Solution:
def groupAnagrams(self, strs):
anagrams = defaultdict(list)
for word in strs:
count = [0] * 26
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 Author

Choose a reason for hiding this comment

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

시간 복잡도만 보면 카운트 방식이 더 빠를 거라고 생각했는데, 리트코드에서는 문자열을 정렬하는 방법이 더 빠르더라고요. 아마 테스트에 사용된 단어들이 짧아서 그런 것 같습니다만 효율적인 방법 공유해보고자 최종코드로 넣었어요 :)

for c in word:
count[ord(c) - ord('a')] += 1
anagrams[tuple(count)].append(word)
return list(anagrams.values())
27 changes: 27 additions & 0 deletions implement-trie-prefix-tree/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Trie:
def __init__(self):
self.trie = {}

def insert(self, word):
node = self.trie
for char in word:
if char not in node:
node[char] = {}
node = node[char]
node['#'] = {}

def search(self, word):
node = self.trie
for char in word:
if char not in node:
return False
node = node[char]
return '#' in node

def startsWith(self, prefix):
node = self.trie
for char in prefix:
if char not in node:
return False
node = node[char]
return True
20 changes: 20 additions & 0 deletions word-break/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution:
def wordBreak(self, s, wordDict):
wordSet = set(wordDict)
memo = {}

def backtrack(start):
if start == len(s):
return True
if start in memo:
return memo[start]

for end in range(start + 1, len(s) + 1):
if s[start:end] in wordSet and backtrack(end):
memo[start] = True
return True

memo[start] = False
return False

return backtrack(0)