-
-
Notifications
You must be signed in to change notification settings - Fork 362
[freemjstudio] WEEK 03 Solutions #2733
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
865ed8d
1a6de37
9531176
370fe38
db69654
4886e4a
2f3e189
4c68b30
22f9365
dae40c7
067afdb
54d8362
def540d
b33b7b1
fe69c94
d9759bc
c4b3816
b3ecf13
1e6b42a
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 |
|---|---|---|
| @@ -1,29 +1,29 @@ | ||
| class Solution: | ||
| def threeSum(self, nums: list[int]) -> list[list[int]]: | ||
| answer = [] | ||
| nums.sort() | ||
| nums.sort() | ||
|
|
||
| for i in range(len(nums)): | ||
| # 중복된 원소가 있는 경우 똑같이 검사할 필요가 없으므로 건너뛴다. | ||
| if i > 0 and nums[i] == nums[i-1]: | ||
| continue | ||
| continue | ||
|
|
||
| left, right = i+1, len(nums) - 1 | ||
|
|
||
| while left < right: | ||
| while left < right: | ||
| total = nums[i] + nums[left] + nums[right] | ||
| if total == 0: | ||
| answer.append([nums[i], nums[left], nums[right]]) | ||
| # left, right 포인터를 둘다 한칸씩 이동시켜 다음 조합을 검사한다. | ||
| left += 1 | ||
| right -= 1 | ||
|
|
||
| # 중복된 원소가 있는 경우 같은 정답 조합을 방지한다 | ||
| # 중복된 원소가 있는 경우 같은 정답 조합을 방지한다 | ||
| while left < right and nums[left] == nums[left -1]: | ||
| left += 1 | ||
|
|
||
| elif total < 0: | ||
| left += 1 | ||
| else: | ||
| else: | ||
| right -= 1 | ||
| 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 초기 조건 처리와 점화식을 이용해 반복으로 계산한다. 개선 제안: 메모리 사용을 줄이려면 공간을 O(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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: path를 깊은 복사해 저장하고 재귀적으로 백트래킹한다. 개선 제안: 중복 조합 제거를 위한 정렬 및 시작 인덱스 관리가 필요하다면 초기 후보를 정렬하는 것도 고려.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| class Solution: | ||
| def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: | ||
| answer = [] | ||
|
|
||
| def backtracking(start_idx, total, path): | ||
| if target == total: | ||
| answer.append(path[:]) # 새로운 리스트 객체를 만들어서 저장함. | ||
| # 리스트가 mutable 객체이므로 path[:] 로 복사하지 않으면 path 가 바뀔 때 answer 안의 값도 바뀌게 된다. | ||
| return | ||
|
|
||
| if target < total: | ||
| return | ||
|
|
||
| for idx in range(start_idx, len(candidates)): | ||
| new_node = candidates[idx] # 동일한 숫자 재사용 가능함. start_idx 도 포함한다. | ||
| path.append(new_node) | ||
|
|
||
| backtracking(idx, total + new_node, path) | ||
| # 선택 취소 | ||
| path.pop() | ||
|
|
||
| backtracking(0, 0, []) | ||
| 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 리스트 길이와 집합 길이를 비교해 중복 여부를 판단한다. 개선 제안: 추가 최적화가 필요하면 해시 세트를 활용하는 방법은 충분히 효율적이다.
|
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: bin(n) 사용 또는 비트 연산으로 차수에 따라 1의 개수를 카운트한다. 개선 제안: 가독성/성능 측면에서 필요에 따라 비트 탐색 최적화 기법(Kernighan의 알고리즘) 도입 가능.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| class Solution: | ||
| def hammingWeight(self, n: int) -> int: | ||
| binary = bin(n) | ||
| return binary.count("1") | ||
|
|
||
| # bin() 을 못쓰는 경우의 풀이 | ||
| class Solution: | ||
| def hammingWeight(self, n: int) -> int: | ||
| count = 0 | ||
|
|
||
| while n > 0: | ||
| count += n % 2 | ||
| n //=2 | ||
|
|
||
| return count |
|
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 @@ | ||
| class Solution: | ||
| def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
| answer = [1] * len(nums) | ||
|
|
||
| # left -> right | ||
| for i in range(1, len(nums)): | ||
| answer[i] = answer[i-1] * nums[i-1] # 왼쪽에 있는 것들을 곱하자 | ||
|
|
||
| # right -> left | ||
| right_product = 1 | ||
| for i in range(len(nums)-1, -1, -1): | ||
| answer[i] = answer[i] * right_product | ||
| right_product = right_product * nums[i] | ||
|
|
||
|
|
||
| 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 빈도 수를 기준으로 최소 힙을 유지해 메모리 사용을 제한한다. 개선 제안: 더 간단한 방법으로 정렬 기반 접근이나 파이프라인 최적화 고려 가능.
|
|
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,21 @@ | ||
| class Solution: | ||
| def isPalindrome(self, s: str) -> bool: | ||
| characters = "abcdefghijklmnopqrstuvwxyz0123456789" | ||
| s = s.lower() | ||
|
|
||
| filtered = "" | ||
| for ch in s: | ||
| if ch in characters: | ||
| filtered += ch | ||
|
|
||
| n = len(filtered) | ||
| left = 0 | ||
| right = n-1 | ||
| while left <= n//2 and right >= n//2: | ||
| if filtered[left] == filtered[right]: | ||
| left += 1 | ||
| right -= 1 | ||
| else: | ||
| return False | ||
|
|
||
| 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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 정렬 후 중복 제거를 위해 i 루프에서 건너뛰기와 left/right 중복 검사로 중복 결과를 제거한다.
개선 제안: 현재 구현이 적절해 보입니다.