-
-
Notifications
You must be signed in to change notification settings - Fork 361
[njngwn] WEEK 05 Solutions #2775
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
Changes from all commits
b9fedda
b9449de
7c6c464
d68a82c
887666f
245d8e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| class Solution: | ||
| # Time Complexity: O(n), n: len(prices) | ||
| # Space Complexity: O(1) | ||
| def maxProfit(self, prices: List[int]) -> int: | ||
| min_price = prices[0] | ||
| profit = 0 | ||
|
|
||
| for i in range(1, len(prices)): | ||
| min_price = min(min_price, prices[i]) | ||
| profit = max(profit, prices[i] - min_price) | ||
|
|
||
| return profit |
|
Contributor
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. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| class Solution: | ||
| # dynamic programming with bottom-up | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(1) | ||
| def climbStairs(self, n: int) -> int: | ||
| if n <= 2: return n | ||
| prev1, prev2 = 2, 1 | ||
|
|
||
| for i in range(3, n+1): | ||
| prev1, prev2 = prev2 + prev1, prev1 | ||
|
|
||
| return prev1 |
|
Contributor
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. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| class Solution: | ||
|
|
||
| def encode(self, strs: List[str]) -> str: | ||
| encoded = '' | ||
|
|
||
| for s in strs: | ||
| encoded += str(len(s)) + '#' + s | ||
|
|
||
| return encoded | ||
|
|
||
| def decode(self, s: str) -> List[str]: | ||
| decoded = [] | ||
|
|
||
| i = 0 | ||
| while i < len(s): | ||
| j = i # j is a pointer for beginning of string | ||
| # find '#' in string | ||
| while s[j] != '#': | ||
| j += 1 | ||
|
|
||
| length = int(s[i:j]) | ||
| word = s[j + 1:j + length + 1] | ||
| decoded.append(word) | ||
| i = j + length + 1 | ||
|
|
||
| return decoded |
|
Contributor
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. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| class Solution: | ||
| # Time Complexity: O(n), n: len(strs) | ||
| # Space Complexity: O(n), n: len(strs) | ||
| def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
| anagram_groups = dict() # key: string in an alphabetical order, values: strings | ||
|
|
||
| for s in strs: | ||
| word = ''.join(sorted(s)) | ||
| if word in anagram_groups: | ||
| anagram_groups[word].extend([s]) | ||
| else: | ||
| anagram_groups[word] = [s] | ||
|
|
||
| return list(anagram_groups.values()) |
|
Contributor
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. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| from collections import Counter | ||
|
|
||
| class Solution: | ||
| # Time Complexity: O(n), n: max(len(s), len(t)) | ||
| # Space Complexity: O(k), k: number of letters | ||
| def isAnagram(self, s: str, t: str) -> bool: | ||
| return Counter(s) == Counter(t) |
|
Contributor
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. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| from functools import cache | ||
|
|
||
|
|
||
| class Solution: | ||
| def wordBreak(self, s: str, wordDict: List[str]) -> bool: | ||
| @cache | ||
| def check(cur): | ||
| if cur == len(s): | ||
| return True | ||
| for word in wordDict: | ||
| if s[cur: cur + len(word)] == word: | ||
| if check(cur + len(word)): | ||
| return True | ||
| return False | ||
|
|
||
| return check(0) |
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
풀이 1:
Solution.maxProfit— Time: ✅ O(n) → O(n) / Space: ✅ O(1) → O(1)피드백: 최대 이익은 현재 최소가 대비 차이를 비교해 갱신하고, 한 번의 for 루프로 전체 가격을 탐색한다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2:
Solution.climbStairs— Time: O(n) / Space: O(1)피드백: 상수 공간에 두 개의 이전 값을 저장하고 순회하며 계산한다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 3:
Solution.encode— Time: ✅ O(n) → O(n) / Space: O(1)피드백: 각 문자열마다 길이 접두사를 추가하는 방식으로 안전하게 구분한다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 4:
Solution.decode— Time: O(n) / Space: O(1)피드백: 정확하게 파싱하여 문자열 리스트를 재구성한다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 5:
Solution.groupAnagrams— Time: ❌ O(n) → O(n * k log k) / Space: ❌ O(1) → O(n)피드백: 정렬 기반 키 생성으로 아나그램을 그룹화한다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 6:
Solution.isAnagram— Time: ❌ O(n) → O(n + m) / Space: O(1)피드백: Counter를 이용한 간단하고 직관적인 비교이다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 7:
Solution.wordBreak— Time: ❌ O(n) → O(n * average_word_length) / Space: ❌ O(1) → O(n)피드백: 메모이제이션으로 중복 서브 문제를 제거하며 탐색한다.
개선 제안: 현재 구현이 적절해 보입니다.