-
-
Notifications
You must be signed in to change notification settings - Fork 194
[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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
eb177e6
Solve : Best Time to Buy And Sell Stock
printjin-gmailcom 5c8085e
Merge branch 'DaleStudy:main' into main
printjin-gmailcom 92240cd
Solve : Group Anagrams
printjin-gmailcom 649197a
Solve : Group Anagram 2
printjin-gmailcom 9bdaebb
Solve : Encode and Decode Strings
printjin-gmailcom 4f69ed0
Merge branch 'main' of https://github.com/printjin-gmailcom/Dalle_Cod…
printjin-gmailcom 13aa8e6
Solve : Implement Trie Prefix Tree
printjin-gmailcom 1384fbf
Solve : Word Break
printjin-gmailcom 8d49797
Solve : Word Break 2
printjin-gmailcom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
for c in word: | ||
count[ord(c) - ord('a')] += 1 | ||
anagrams[tuple(count)].append(word) | ||
return list(anagrams.values()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오.. 문자 개수 기반으로 그룹화 하는 부분이 인상깊네요.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
시간 복잡도만 보면 카운트 방식이 더 빠를 거라고 생각했는데, 리트코드에서는 문자열을 정렬하는 방법이 더 빠르더라고요. 아마 테스트에 사용된 단어들이 짧아서 그런 것 같습니다만 효율적인 방법 공유해보고자 최종코드로 넣었어요 :)