-
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 #860 from jungsiroo/main
[jungsiroo] Week 5
- Loading branch information
Showing
4 changed files
with
141 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,23 @@ | ||
class Solution: | ||
def maxProfit(self, prices: List[int]) -> int: | ||
""" | ||
๊ฐ์ฅ ์์ต์ ๋ง์ด ์ป์ ์ ์๋๋ก ์ ์ ์ ๋งค์, ๊ณ ์ ์ ๋งค๋ | ||
๋งค์์ ๋งค๋๋ ์๋ก ๋ค๋ฅธ ๋ | ||
min_price๋ฅผ ๋นผ๊ฐ๋ฉด์ price ์ ๋ฐ์ดํธ | ||
Time Complexity : O(n) | ||
Space Complexity : O(1) | ||
""" | ||
|
||
min_price = max(prices) | ||
days = len(prices) | ||
|
||
for day in range(days): | ||
min_price = min(prices[day], min_price) | ||
prices[day] -= min_price | ||
|
||
return max(prices) | ||
|
||
|
||
|
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,44 @@ | ||
from collections import defaultdict | ||
|
||
class Solution: | ||
def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
# Naive Solution : Sort string | ||
|
||
# ๊ฐ ๋จ์ด๋ง๋ค ๋ชจ๋ ์ ๋ ฌ์ ํ ๋ค ํด๋น ๊ฐ์ hash๋ก ์ฌ์ฉํ์ฌ ๋์ ๋๋ฆฌ์ ์ถ๊ฐํ๋ ๋ฐฉ๋ฒ | ||
# strs[i].length = k | ||
# Time Complexity : O(n*klog(k)) | ||
# Space Complexity : O(n*k) | ||
|
||
""" | ||
n = len(strs) | ||
word_dict = defaultdict(list) | ||
for word in strs: | ||
key = hash(''.join(sorted(word))) | ||
word_dict[key].append(word) | ||
ret = [] | ||
for value in word_dict.values(): | ||
ret.append(value) | ||
return ret | ||
""" | ||
|
||
# Better Solution : Counting | ||
|
||
# anagram ์ ํน์ฑ ์ค ์ํ๋ฒณ ์นด์ดํธ ๊ฐฏ์๊ฐ ๊ฐ๋ค๋ ๊ฒ์ ์ด์ฉ | ||
# ์นด์ดํธ ๊ฐฏ์๋ฅผ ํ์ฉํ์ฌ key ๊ฐ์ผ๋ก ์ฒ๋ฆฌ | ||
# Time Complexity : O(n*k) | ||
# Space Complexity : O(n*k) | ||
word_dict = defaultdict(list) | ||
|
||
for word in strs: | ||
freq = [0]*26 | ||
for char in word: | ||
freq[ord(char) - ord('a')] += 1 | ||
word_dict[tuple(freq)].append(word) | ||
|
||
return list(word_dict.values()) | ||
|
||
|
||
|
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,47 @@ | ||
""" | ||
Recursive vs Iterative | ||
์ฌ๊ท ๋ฐฉ์์ ๊ฒฝ์ฐ, ๋ง์ฝ ๋ฌธ์์ด์ ๊ธธ์ด๊ฐ ๊ต์ฅํ ๊ธธ๋ค๋ฉด ๊ทธ๋งํผ์ ์ฝ์ด ์ผ์ด๋๊ณ ์ด๋ ์ฑ๋ฅ์ ์ผ๋ก ๋๋ ค์ง ์ ์์. | ||
๋ ๊ฐ์ง ๋ชจ๋ ์๊ฐ ๋ณต์ก๋ ๋ฉด์์๋ O(m) ์ (m = len(string)) | ||
Node ํด๋์ค๋ฅผ ๋ฐ๋ก ๋์ด ์ฒ๋ฆฌํ๋ฉด ๊ฐ๋ ์ฑ ๋๊ฒ ์ฒ๋ฆฌํ ์ ์๋ค. | ||
""" | ||
|
||
class Node: | ||
def __init__(self, key=None): | ||
self.key = key | ||
self.children = {} | ||
self.is_end = False | ||
|
||
class Trie: | ||
def __init__(self): | ||
self.head = Node() | ||
|
||
def insert(self, word: str) -> None: | ||
curr = self.head | ||
|
||
for ch in word: | ||
if ch not in curr.children: | ||
curr.children[ch] = Node(ch) | ||
curr = curr.children[ch] | ||
curr.is_end = True | ||
|
||
def search(self, word: str) -> bool: | ||
curr = self.head | ||
|
||
for ch in word: | ||
if ch not in curr.children: | ||
return False | ||
curr = curr.children[ch] | ||
|
||
return curr.is_end | ||
|
||
def startsWith(self, prefix: str) -> bool: | ||
curr = self.head | ||
|
||
for ch in prefix: | ||
if ch not in curr.children: | ||
return False | ||
curr = curr.children[ch] | ||
return True | ||
|
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,27 @@ | ||
class Solution: | ||
def wordBreak(self, s: str, wordDict: List[str]) -> bool: | ||
# DP๋ฅผ ํ์ฉํ ๋ฌธ์ | ||
|
||
""" | ||
KMP๋ Rabin Karp๋ฅผ ์ด์ฉํ์ฌ ํธ๋ ๋ฌธ์ ์ธ ์ค ์์์ผ๋ ์ ํ ์๋ ๋ฌธ์ | ||
neetcode์ ๋์์ ๋ฐ์์ | ||
๊ณ์ ์ํ๋ฅผ ํ๋ฉด์ if dp[j]๋ฅผ ํตํด ๊ธธ์ด๋งํผ ๋์ด์ฃผ๊ณ word_set์ ์๋์ง๋ฅผ ํ์ธํจ | ||
Time Complexity : O(n^2) | ||
Space Complexity : O(n) | ||
""" | ||
|
||
word_set = set(wordDict) | ||
n = len(s) | ||
|
||
dp = [False] * (n + 1) | ||
dp[0] = True | ||
|
||
for i in range(1, n + 1): | ||
for j in range(i): | ||
if dp[j] and s[j:i] in word_set: | ||
dp[i] = True | ||
break | ||
|
||
return dp[n] | ||
|