-
-
Notifications
You must be signed in to change notification settings - Fork 363
[hoonjichoi1] WEEK 03 solutions #2716
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 @@ | ||
| /* | ||
| Time Complexity : O(c^t) | ||
| Space Complexity : O(t) | ||
| */ | ||
|
|
||
| class Solution { | ||
|
|
||
| public List<List<Integer>> combinationSum(int[] candidates, int target) { | ||
| List<List<Integer>> output = new ArrayList<>(); | ||
| Stack<Integer> nums = new Stack<>(); | ||
| dfs (candidates, output, target, nums, 0, 0); | ||
| return output; | ||
| } | ||
|
|
||
| private void dfs(int[] candidates, List<List<Integer>> output, int target, Stack<Integer> nums, int start, int total) { | ||
| // base case : pass | ||
| if (target == total) { | ||
| output.add(new ArrayList<>(nums)); | ||
| return; | ||
| } | ||
| // base case : fail | ||
| if (target < total) { | ||
| return; | ||
| } | ||
|
|
||
| for (int i = start ; i < candidates.length ; i++) { | ||
| int num = candidates[i]; | ||
| nums.push(num); | ||
| dfs(candidates, output, target, nums, i, total + num); | ||
| nums.pop(); | ||
| } | ||
|
|
||
|
|
||
| } | ||
| } |
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 루프는 비트 길이만큼 반복되며, 각 반복에서 나눗셈으로 비트를 확인하는 방식입니다. 개선 제안: 현재 구현은 특정 초기 조건 비효율이 있습니다. 비트를 오른쪽으로 시프트하고 1인 경우 카운트를 증가시키면 더 직관적이고 일반적인 풀이가 됩니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| class Solution { | ||
| public int hammingWeight(int n) { | ||
|
|
||
| if (n == 1) { | ||
| return 1; | ||
| } | ||
|
|
||
| int curr = n, result = 1; | ||
| while (curr > 1) { | ||
| if (curr % 2 == 1) { | ||
| result++; | ||
| } | ||
| curr = curr / 2; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| } | ||
|
|
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 정규식으로 한 번 필터링하고 양방향 탐색으로 대칭 여부를 확인합니다. 개선 제안: 현재 구현의 변수명 오타(conveted) 등 리팩토링으로 가독성을 높이고, 필요한 경우 스트림이나 투포인터 방식으로 불필요한 문자열 생성 없이도 구현할 수 있습니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| class Solution { | ||
| public boolean isPalindrome(String s) { | ||
|
|
||
| // removing all non-alphanumeric characters and convert them to lower case | ||
| String conveted = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase(); | ||
|
|
||
| // early return of the empty string case | ||
| if (conveted.length() == 0) { | ||
| return true; | ||
| } | ||
|
|
||
| // check the symmetry | ||
| int left = 0, right = conveted.length() - 1; | ||
| while (left <= right) { | ||
| if (conveted.charAt(left) != conveted.charAt(right)) { | ||
| return false; | ||
| } | ||
| left++; | ||
| right--; | ||
| } | ||
| 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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 백트래킹 없이 중복 조합 탐색이 이뤄지며, 매 재귀에서 후보를 다시 시도하므로 지수적 시간 복잡도가 예상됩니다.
개선 제안: 고려해볼 만한 대안: 가지치기와 중복 제거를 명시적으로 구현하고, 정렬 후 남은 후보군의 재사용 여부를 제어하면 불필요한 탐색을 줄일 수 있습니다.