-
-
Notifications
You must be signed in to change notification settings - Fork 195
[jungsiroo] Week 5 #860
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
[jungsiroo] Week 5 #860
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,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 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,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 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,47 @@ | ||
""" | ||
Recursive vs Iterative | ||
|
||
재귀 방식의 경우, 만약 문자열의 길이가 굉장히 길다면 그만큼의 콜이 일어나고 이는 성능적으로 느려질 수 있음. | ||
두 가지 모두 시간 복잡도 면에서는 O(m) 임 (m = len(string)) | ||
|
||
Node 클래스를 따로 두어 처리하면 가독성 높게 처리할 수 있다. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네, 덕분에 코드가 읽기 쉽네요 🙌 |
||
""" | ||
|
||
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 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 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) | ||
Comment on lines
+10
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 알고리즘의 성능에 영향을 주는 입력 인자가 2개이니 복잡도 분석도 2개의 변수로 표현해야 하지 않을까요? |
||
""" | ||
|
||
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] | ||
|
Oops, something went wrong.
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.
오, 입력 리스트를 변경하시군요. 신선한 접근입니다!