-
-
Notifications
You must be signed in to change notification settings - Fork 360
[dolphinflow86] WEEK 02 Solutions #2677
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
Open
dolphinflow86
wants to merge
4
commits into
DaleStudy:main
Choose a base branch
from
dolphinflow86:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+65
−0
Open
Changes from all commits
Commits
Show all changes
4 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,20 @@ | ||
| # 1) Recursion with memoization. | ||
| # TC: O(N) where N is the given natural number. | ||
| # SC: O(N) where N is the given natural number. | ||
| class Solution: | ||
| def climb_rec(self, n: int, step: int, memo: Dict[int, int]): | ||
| if step > n: return 0 | ||
| if step in memo: return memo[step] | ||
| if step == n: return 1 | ||
|
|
||
| first_way = self.climb_rec(n, step + 1, memo) | ||
| second_way = self.climb_rec(n, step + 2, memo) | ||
| memo[step] = first_way + second_way | ||
| return memo[step] | ||
|
|
||
|
|
||
| def climbStairs(self, n: int) -> int: | ||
| if n == 1: return 1 | ||
|
|
||
| memo = {} | ||
| return self.climb_rec(n, 1, memo) + self.climb_rec(n, 2, memo) |
|
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. 🏷️ 알고리즘 패턴 분석
|
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 @@ | ||
| # 1) Without using division operator and need to meet linear time complexity, | ||
| # calculate left accumulated product and calculate right accumulated product except iself and then | ||
| # product of left and right to get the result. | ||
| # TC: O(N) where N is the length of nums | ||
| # SC: O(N) where N is the length of nums | ||
| class Solution: | ||
| def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
| n = len(nums) | ||
| answer = [1] * n | ||
| acc = 1 | ||
| for i in range(1, n): | ||
| acc *= nums[i-1] | ||
| answer[i] = acc | ||
|
|
||
| acc = 1 | ||
| for i in range(n-2, -1, -1): | ||
| acc *= nums[i+1] | ||
| answer[i] *= acc | ||
|
|
||
| return answer |
|
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. 🏷️ 알고리즘 패턴 분석
|
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,25 @@ | ||
| # 1) Sort input strings and check if those are the same. | ||
| # TC: O(NlogN) where N is the size of string s and t due to sorting | ||
| # SC: O(N) where N is the length of string s and t to store sorted list. | ||
| class Solution: | ||
| def isAnagram(self, s: str, t: str) -> bool: | ||
| return sorted(s) == sorted(t) | ||
|
|
||
| # 2) Using dict to store count of the first string and decrease back to see if there's negative count. | ||
| # TC: O(N + M) where N is the length of s and M is the length of t. | ||
| # SC: O(N + M) where N is the length of s and M is the length of t. | ||
| class Solution: | ||
| def isAnagram(self, s: str, t: str) -> bool: | ||
| if len(s) != len(t): return False | ||
|
|
||
| char_map: Dict[str, int] = {} | ||
| for ch in s: | ||
| char_map[ch] = char_map.get(ch, 0) + 1 | ||
|
|
||
| for ch in t: | ||
| if ch not in char_map or char_map[ch] == 0: | ||
| return False | ||
|
|
||
| char_map[ch] -= 1 | ||
|
|
||
| return True |
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
풀이 1:
Solution.climbStairs— Time: O(n) / Space: O(n)피드백: 메모화를 사용해 중복 계산을 피하고 재귀 깊이는 n까지 증가한다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2:
Solution.productExceptSelf— Time: O(n) / Space: O(1)피드백: 왼쪽/오른쪽 누적곱을 순회하며 결과 배열에 누적한다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 3:
Solution.isAnagram (전 첫 구현)— Time: ❌ O(N) → O(n log n) / Space: ✅ O(N) → O(n)피드백: 정렬에 의한 시간 복잡도 증가가 존재한다.
개선 제안: 필요 시 카운트(Hashmap) 기반으로 평균 O(n) 가능.
풀이 4:
Solution.isAnagram (두 번째 구현)— Time: O(n) / Space: O(k)피드백: 해시맵으로 선형 시간에 해결 가능하다.
개선 제안: 현재 구현이 적절해 보입니다.