-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #862 from Chaedie/main
[Chaedie] Week 5
- Loading branch information
Showing
3 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,40 @@ | ||
""" | ||
Solution: 1) 2์ค ํฌ๋ฌธ์ ๋๋ฉด์ max๊ฐ์ ๊ตฌํ๋ค. | ||
Time: O(n^2) | ||
Space: O(1) | ||
Time Limit Exceeded | ||
""" | ||
|
||
|
||
class Solution: | ||
def maxProfit(self, prices: List[int]) -> int: | ||
result = 0 | ||
for l in range(len(prices) - 1): | ||
for r in range(l + 1, len(prices)): | ||
result = max(result, prices[r] - prices[l]) | ||
|
||
return result | ||
|
||
|
||
""" | ||
Solution: | ||
1) prices๋ฅผ ์ํํ๋ฉด์ max_profit ์ ์ฐพ๋๋ค. | ||
2) profit ์ current price - min_profit๋ก ๊ตฌํ๋ค. | ||
Time: O(n) | ||
Space: O(1) | ||
""" | ||
|
||
|
||
class Solution: | ||
def maxProfit(self, prices: List[int]) -> int: | ||
n = len(prices) | ||
max_profit = 0 | ||
min_price = prices[0] | ||
|
||
for i in range(1, len(prices)): | ||
profit = prices[i] - min_price | ||
max_profit = max(max_profit, profit) | ||
min_price = min(prices[i], min_price) | ||
return max_profit |
This file contains 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,22 @@ | ||
""" | ||
Solution: | ||
1) hash map ์ sorted_word๋ฅผ ํค๋ก, ํด๋น sorted_word ์ ํด๋นํ๋ ์์๋ค์ ๋ฐธ๋ฅ๋ก ๋ฃ์ต๋๋ค. | ||
Time: O(n^2 logn)= O(n) * O(nlogn) | ||
Space: O(n) | ||
""" | ||
|
||
|
||
class Solution: | ||
def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
anagram_words = defaultdict(list) | ||
# dict = {sorted_word: [word, word]} | ||
|
||
for i in range(len(strs)): | ||
word = strs[i] | ||
sorted_word = "".join(sorted(word)) | ||
anagram_words[sorted_word].append(word) | ||
|
||
result = [] | ||
for arr in anagram_words.values(): | ||
result.append(arr) | ||
return result |
This file contains 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,59 @@ | ||
""" | ||
Solution: | ||
Trie ๊ตฌ์กฐ์ ๋ํด ๋ฐฐ์๋ณด์์ต๋๋ค. | ||
TrieNode ๋ children๊ณผ ending์ผ๋ก ์ด๋ฃจ์ด์ ธ์์ต๋๋ค. | ||
1) insert: | ||
1.1) ๋ฌธ์์ด์ ๊ฐ ๋ฌธ์๋ฅผ Trie ๊ตฌ์กฐ์ ๋ฃ์ต๋๋ค. | ||
1.2) ๋ง์ง๋ง ๋ฌธ์์ด์์ ending ์ True ๋ก setํฉ๋๋ค. | ||
2) search: | ||
2.1) ๋ฌธ์์ด์ ๊ฐ ๋ฌธ์๊ฐ node.children ์ ์กด์ฌํ์ง ์์ผ๋ฉด False ๋ฅผ ๋ฐํํฉ๋๋ค. | ||
2.2) ๋ง์ง๋ง ๋ฌธ์๊ฐ ending ์ธ์ง ํ๋ณํฉ๋๋ค. | ||
3) startsWith | ||
3.1) search ์ ๋์ผํ๊ฒ ๋ฌธ์์ด์ ์ํํฉ๋๋ค. | ||
3.2) ending ์ฌ๋ถ์ ๋ฌด๊ดํ๊ฒ True ๋ฅผ ๋ฐํํฉ๋๋ค. | ||
""" | ||
|
||
|
||
class TrieNode: | ||
def __init__(self): | ||
self.children = {} | ||
self.ending = False | ||
|
||
|
||
class Trie: | ||
def __init__(self): | ||
self.root = TrieNode() | ||
|
||
def insert(self, word: str) -> None: | ||
node = self.root | ||
|
||
for char in word: | ||
if char not in node.children: | ||
node.children[char] = TrieNode() | ||
node = node.children[char] | ||
node.ending = True | ||
|
||
def search(self, word: str) -> bool: | ||
node = self.root | ||
|
||
for char in word: | ||
if char not in node.children: | ||
return False | ||
node = node.children[char] | ||
return node.ending | ||
|
||
def startsWith(self, prefix: str) -> bool: | ||
node = self.root | ||
|
||
for char in prefix: | ||
if char not in node.children: | ||
return False | ||
node = node.children[char] | ||
return True | ||
|
||
|
||
# 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) |