-
-
Notifications
You must be signed in to change notification settings - Fork 363
[essaysir] WEEK 03 solutions #2717
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,32 @@ | ||
| import java.util.*; | ||
|
|
||
| class Solution { | ||
| public List<List<Integer>> combinationSum(int[] candidates, int target) { | ||
| // 합이 target 이 되는 경우의 수를 모두 고르시오 | ||
| // target 이 150 이하 | ||
| List<List<Integer>> result = new ArrayList<>(); | ||
| List<Integer> current = new ArrayList<>(); | ||
|
|
||
| backTracking(candidates, target, 0 , result, current); | ||
| return result; | ||
| } | ||
|
|
||
| public void backTracking(int[] candidates, int remain, int start, List<List<Integer>> result, List<Integer> current) { | ||
| if ( remain == 0 ){ | ||
| result.add(new ArrayList<>(current)); | ||
| return; | ||
| } | ||
|
|
||
| if ( remain < 0 ){ | ||
| return; | ||
| } | ||
|
|
||
| for (int i = start; i < candidates.length; i++) { | ||
| current.add(candidates[i]); | ||
|
|
||
| backTracking(candidates, remain - candidates[i], i , result, current); | ||
|
|
||
| current.remove(current.size() - 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
풀이 1:
|
| 복잡도 | |
|---|---|
| Time | O(1) |
| Space | O(1) |
피드백: 주어진 정수의 1비트를 세는 방법으로 간단하고 빠르게 해결된다.
개선 제안: 추가 구현은 필요 없고, 내장 함수 사용이 가장 간단하고 효율적이다.
풀이 2: Solution.hammingWeight — Time: O(b) / Space: O(1)
| 복잡도 | |
|---|---|
| Time | O(b) |
| Space | O(1) |
피드백: 비트 길이에 비례하는 시간이며, 문자열 변환이 비용으로 추가된다.
개선 제안: 권장 구현은 비트카운트 내장 함수를 사용하는 방법이다.
💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| class Solution { | ||
| public int hammingWeight(int n) { | ||
| // 이진수로 바꾸고, 1 이 몇개 있는 지 파악한다. | ||
| return Integer.bitCount(n); | ||
|
|
||
| // 추가 풀이. | ||
| // String ans = Integer.toBinaryString(n).replace("0",""); | ||
| // return ans.length(); | ||
| } | ||
| } | ||
|
|
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 무작정 대소문자 무시 및 비문자 제거를 통해 문제 정의에 맞춘 간단한 풀이이다. 개선 제안: 공간 사용을 줄이고 in-place 처리를 원하면 두 포인터를 사용해 스캔하며 제거된 문자 여부를 판단하는 방법도 가능하다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| class Solution { | ||
| public boolean isPalindrome(String s) { | ||
| StringBuilder sb = new StringBuilder(); | ||
| for (char c : s.toCharArray()) { | ||
| if (Character.isLetterOrDigit(c)) { | ||
| sb.append(Character.toLowerCase(c)); | ||
| } | ||
| } | ||
|
|
||
| String result = sb.toString(); | ||
| char[] x = result.toCharArray(); | ||
| for (int i = 0; i < x.length / 2; i++) { | ||
| if (x[i] != x[x.length - i - 1]) { | ||
| 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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 백트래킹으로 중복 없이 가능한 조합을 생성하며, remain이 0이 되면 현재 경로를 결과에 추가한다.
개선 제안: 현재 구현은 입력 크기에 대해 지수 시간 복잡도를 가지나, 정렬 및 가지치기(예: 남은 합이 남은 최대 가능 합보다 작은 경우)가 적용되면 실무에서 실용적일 수 있다.