-
-
Notifications
You must be signed in to change notification settings - Fork 304
[changhyumm] WEEK 02 solutions #2070
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
base: main
Are you sure you want to change the base?
Changes from all commits
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,35 @@ | ||
| class Solution: | ||
| def threeSum(self, nums: List[int]) -> List[List[int]]: | ||
| # ans = set() | ||
| # for i in range(len(n)): | ||
| # seen = {} | ||
| # for j in range(i+1, len(n)): | ||
| # complement = -(nums[i] + nums[j]) | ||
| # if complement in seen: | ||
| # ans.add(tuple(sorted([nums[i], nums[j], complement]))) | ||
| # seen[nums[j]] = j | ||
| # return [list(triplet) for triplet in ans] | ||
| # hash + 이중 loop -> O(n^2) | ||
|
|
||
|
|
||
| ## 투포인터 활용 | ||
| # sort + loop -> O(nlogn) | ||
| ans_set = set() | ||
| nums.sort() | ||
| for i in range(len(nums)): | ||
| l = i + 1 | ||
| r = len(nums) - 1 | ||
| while l < r: | ||
| total = nums[i] + nums[l] + nums[r] | ||
| if total < 0: | ||
| l += 1 | ||
| elif total > 0: | ||
| r -= 1 | ||
| else: | ||
| # 중복제거 | ||
| ans_set.add((nums[i], nums[l], nums[r])) | ||
|
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. set을 활용해 중복 제거를 하셨네요! 저는 포인터 기반으로 루프 내에서 중복을 제거하도록 풀어봤었는데, 이렇게 하면 set을 유지하기 위한 추가적인 공간 복잡도를 최적화 할 수 있더라구요! 한 번 포인터 기반으로도 풀어보시는 걸 추천드립니다~~ 🤩 |
||
| l += 1 | ||
| r -= 1 | ||
| return list(ans_set) | ||
|
|
||
|
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: | ||
| def climbStairs(self, n: int) -> int: | ||
| if n <= 1: | ||
| return 1 | ||
| dp = [0] * n | ||
| print(dp) | ||
| dp[0] = 1 | ||
| dp[1] = 2 | ||
|
|
||
| for i in range(2, n): | ||
| dp[i] = dp[i-2] + dp[i-1] | ||
|
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. 이렇게 DP 테이블의 가장 최근 n개의 원소만 사용하는 경우에는 |
||
| return dp[-1] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| class Solution: | ||
| def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
| # ans = [1] * len(nums) | ||
| # for i in range(len(nums)): | ||
| # for j in range(len(nums)): | ||
| # if i != j: | ||
| # ans[i] = ans[i] * nums[j] | ||
| # return ans | ||
| # 시간복잡도를 줄이기 위해서 이중루프를 안쓰는 방법으로 | ||
| left = [1] * len(nums) | ||
| right = [1] * len(nums) | ||
| for i in range(len(nums) - 1): | ||
| left[i+1] = left[i] * nums[i] | ||
| print(left) | ||
| for j in range(len(nums) - 1, 0, -1): | ||
| right[j-1] = right[j] * nums[j] | ||
| print(right) | ||
|
|
||
| answer = [] | ||
| for k in range(len(left)): | ||
| answer.append(left[k]*right[k]) | ||
| 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. merge를 위해 파일 마지막에 빈 개행 추가 부탁드립니다! |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| class Solution: | ||
| def isAnagram(self, s: str, t: str) -> bool: | ||
| if len(s) != len(t): | ||
| return False | ||
| # sorted_s = sorted(s) | ||
| # sorted_t = sorted(t) | ||
| # for i in range(len(s)): | ||
| # if sorted_s[i] != sorted_t[i]: | ||
| # return False | ||
| # return True | ||
| # 시간복잡도가 O(nlog(n)) | ||
|
|
||
| # Hashmap 사용하는 방법으로 변경 | ||
| counter = {} | ||
| for char in s: | ||
| counter[char] = counter.get(char, 0) + 1 | ||
| for char in t: | ||
| if char in counter and counter[char] != 0: | ||
| counter[char] -= 1 | ||
| elif char in counter and counter[char] == 0: | ||
|
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. if-elif 문에서 |
||
| return False | ||
| else: | ||
| return False | ||
| # 시간복잡도 O(n) | ||
| # 공간복잡도 O(n) | ||
|
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. 저도 다시 풀어보면서 알게 된 사실인데요, 이 문제에서는 입력값이 모두 영어 소문자로만 구성되어 있으므로 공간 복잡도가 O(26) = O(1)이 된다고 합니다! |
||
| return True | ||
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.
sort + 이중 loop이므로 시간 복잡도가
O(n^2)이 될 것 같습니다!