-
-
Notifications
You must be signed in to change notification settings - Fork 361
[jaekwang97] WEEK 02 Solutions #2691
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
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,34 @@ | ||
| import java.util.*; | ||
|
|
||
| class Solution { | ||
| public List<List<Integer>> threeSum(int[] nums) { | ||
| int n = nums.length; | ||
| Arrays.sort(nums); | ||
| List<List<Integer>> answers = new ArrayList<>(); | ||
| Set<List<Integer>> answer = new HashSet<>(); | ||
| for(int i = 0 ; i < n ; i++){ | ||
| if (nums[i] > 0) break; | ||
| if (i > 0 && nums[i] == nums[i-1]) continue; | ||
| int left = i + 1; | ||
| int right = n - 1; | ||
| while(left < right){ | ||
| int sum = nums[left] + nums[right] + nums[i]; | ||
|
|
||
|
|
||
| if (sum > 0) right--; | ||
| else if (sum < 0) left++; | ||
| else{ | ||
| answer.add(new ArrayList<>(List.of(nums[i],nums[left], nums[right]))); | ||
| while(left < right && nums[left] == nums[left+1]) left++; | ||
| while(left < right && nums[right] == nums[right-1]) right--; | ||
| left++; | ||
| right--; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| answers = new ArrayList<>(answer); | ||
|
|
||
| return answers; | ||
| } | ||
| } |
|
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 { | ||
| public int climbStairs(int n) { | ||
| if (n <= 2) return n; | ||
|
|
||
| int prev = 1; | ||
| int cur = 2; | ||
|
|
||
| for (int i = 3; i <= n; i++) { | ||
| int next = prev + cur; | ||
| prev = cur; | ||
| cur = next; | ||
| } | ||
|
|
||
| return cur; | ||
| } | ||
| } | ||
|
Comment on lines
+1
to
+16
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 정석적인 풀이대로 잘 하신것 같습니다!
Contributor
Author
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,26 @@ | ||
| import java.util.*; | ||
|
|
||
| class Solution { | ||
| public int[] productExceptSelf(int[] nums) { | ||
| int n = nums.length; | ||
| int[] right = new int[n + 1]; | ||
| int[] left = new int[n + 1]; | ||
|
|
||
| right[0] = 1; | ||
| for (int i = 1; i <= n; i++) { | ||
| right[i] = right[i - 1] * nums[i - 1]; | ||
| } | ||
|
|
||
| left[n] = 1; | ||
| for (int i = n - 1; i >= 0; i--) { | ||
| left[i] = left[i + 1] * nums[i]; | ||
| } | ||
|
|
||
| int[] answer = new int[n]; | ||
| for (int i = 0; i < n; i++) { | ||
| answer[i] = right[i] * left[i + 1]; | ||
| } | ||
|
|
||
| return answer; | ||
| } | ||
| } | ||
|
Comment on lines
+1
to
+26
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
Author
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. 감사합니다! 저도 처음에는 전체 곱을 구해서 나누는 방식을 먼저 떠올렸는데, 문제 조건 때문에 왼쪽 누적 곱과 오른쪽 누적 곱을 따로 계산하는 방식으로 접근했습니다. 이렇게 나누면 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 고정 크기 배열(26)로 문자 빈도를 계산하여 비교합니다. 입력 문자열의 길이에 비례하는 시간과 상수 공간을 가집니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import java.util.*; | ||
|
|
||
| class Solution { | ||
| public boolean isAnagram(String s, String t) { | ||
| int[] count = new int[26]; | ||
|
|
||
| for (char c : s.toCharArray()) { | ||
| count[c - 'a']++; | ||
| } | ||
|
|
||
| for (char c : t.toCharArray()) { | ||
| count[c - 'a']--; | ||
| } | ||
|
|
||
| for (int n : count) { | ||
| if (n != 0) return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } |
|
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,27 @@ | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * public class TreeNode { | ||
| * int val; | ||
| * TreeNode left; | ||
| * TreeNode right; | ||
| * TreeNode() {} | ||
| * TreeNode(int val) { this.val = val; } | ||
| * TreeNode(int val, TreeNode left, TreeNode right) { | ||
| * this.val = val; | ||
| * this.left = left; | ||
| * this.right = right; | ||
| * } | ||
| * } | ||
| */ | ||
| class Solution { | ||
| public boolean isValidBST(TreeNode root) { | ||
| return dfs(root, Long.MIN_VALUE, Long.MAX_VALUE); | ||
| } | ||
|
|
||
| private boolean dfs(TreeNode node, long min, long max){ | ||
| if(node == null) return true; | ||
| if(node.val <= min || node.val >= max) return false; | ||
|
|
||
| return dfs(node.left, min, node.val) && dfs(node.right, node.val, max); | ||
| } | ||
| } |
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 정렬 및 투 포인터를 이용해 모든 3수 합이 0인 경우를 찾고 중복을 제거합니다. 해시 셋을 사용해 중복 제거를 달성하지만 최악의 경우 추가 공간이 필요합니다.
개선 제안: 고려해볼 만한 대안: 중복 제거를 Set 대신 로직으로만 처리해 불필요한 해시 사용을 줄이고, 결과를 즉시 중복 제거된 리스트에 추가하도록 구현해도 됩니다.