Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions 3sum/jaekwang97.java

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Two Pointers, Binary Search, Hash Map / Hash Set
  • 설명: 정렬 후 좌우 포인터를 이용한 합 탐색으로 3합을 찾는 대표적 패턴이다. 중복 제거를 위해 해시 세트와 스킵 로직이 사용된다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n^2)
Space O(n)

피드백: 정렬 및 투 포인터를 이용해 모든 3수 합이 0인 경우를 찾고 중복을 제거합니다. 해시 셋을 사용해 중복 제거를 달성하지만 최악의 경우 추가 공간이 필요합니다.

개선 제안: 고려해볼 만한 대안: 중복 제거를 Set 대신 로직으로만 처리해 불필요한 해시 사용을 줄이고, 결과를 즉시 중복 제거된 리스트에 추가하도록 구현해도 됩니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

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;
}
}
16 changes: 16 additions & 0 deletions climbing-stairs/jaekwang97.java

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Dynamic Programming, Greedy
  • 설명: 클라이밍 스텝 문제는 각 단계의 경우의 수를 이전 두 단계의 합으로 구하는 형태로, DP의 점화식을 이용한 전형적인 풀이입니다. 간단한 최적화로 배열 없이 상태만 유지하는 방식도 DP의 한 형태로 해석됩니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(1)

피드백: 초기값을 설정한 뒤 순회하면서 현재 경우의 수를 갱신합니다. 상수 공간으로 구현돼 추가 메모리 사용이 없습니다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DP 정석적인 풀이대로 잘 하신것 같습니다!
다만

  • 몇 번째 항이 초기항이고, 이후 어느 항부터 점화식이 적용되는지 다시 한번 살펴보시는 것도 좋을것 같습니다.
  • 지금 이 풀이에서는 n 이하의 모든 값을 다 저장하고 있는데, 굳이 다 저장할 필요가 있을까요? 저장하지 않는 풀이도 한번 생각해보시는거 어떨까요?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

리뷰 감사합니다!

말씀해주신 것처럼 직전 두 값만 있으면 다음 값을 계산할 수 있어서, 전체 dp 배열을 저장하지 않는 방식으로 수정해봤습니다. 초기항도 n <= 2를 먼저 처리하고, 이후에는 prev, cur 두 변수로 점화식 흐름이 보이도록 정리했습니다.

26 changes: 26 additions & 0 deletions product-of-array-except-self/jaekwang97.java

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Two Pointers, Sliding Window, Monotonic Stack, Hash Map / Hash Set, Greedy, Divide and Conquer, Dynamic Programming, Binary Search, Union Find, Trie, Bit Manipulation, BFS, DFS, Backtracking, Heap / Priority Queue
  • 설명: 두 배열 패턴 없이도 왼쪽/오른쪽 누적 곱을 이용하는 패턴으로, Right-Left 누적 곱 배열을 만들어 정답을 구하는 방식이다. 이는 DP의 부분 문제 재사용과 같은 누적곱(전달 변수) 활용으로 볼 수 있다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(n)

피드백: 왼쪽 누적곱과 오른쪽 누적곱 배열을 만들어 각 인덱스의 결과를 곱으로 구합니다. 추가 배열 사용으로 메모리 사용이 증가합니다.

개선 제안: 현재 구조도 적절하지만 공간 절감을 원하면 두 배열 대신 하나의 결과 배열과 임시 변수로 구현하는 방법을 고려해볼 수 있습니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 처음에 잘못된 풀이(나눗셈을 사용한)로 풀었었는데,
@JAEKWANG97 님 풀이를 보고 제가 틀렸다는걸 알게됐어요.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

감사합니다!

저도 처음에는 전체 곱을 구해서 나누는 방식을 먼저 떠올렸는데, 문제 조건 때문에 왼쪽 누적 곱과 오른쪽 누적 곱을 따로 계산하는 방식으로 접근했습니다. 이렇게 나누면 0이 포함된 케이스도 자연스럽게 처리돼서 좋았던 것 같습니다.

21 changes: 21 additions & 0 deletions valid-anagram/jaekwang97.java

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Hash Map / Hash Set, Bit Manipulation
  • 설명: 두 문자열의 문자 등장 횟수를 배열로 카운트하여 비교하는 방식으로 해시 맵/세트 없이도 등장 여부를 판단한다. 배열 인덱스는 문자 간 차이로 매핑되며, 26 알파벳 고정 크기 카운트를 이용한 차감으로 중복 여부를 확인한다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(1)

피드백: 고정 크기 배열(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;
}
}
27 changes: 27 additions & 0 deletions validate-binary-search-tree/jaekwang97.java

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Binary Search, Depth-First Search, Depth-First Search
  • 설명: BST 유효성 검사에서 각 노드를 범위로 제약하며 좌우 서브트리에 재귀적으로 조건을 확인하는 방식으로 구현되어 있습니다. 트리를 깊이 우선 탐색하며 각 노드의 값이 허용된 범위를 벗어나면 불리언을 반환합니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(h)

피드백: 각 노드에 대해 최소/최대 유효 범위를 유지하고 재귀적으로 검사합니다. 최악의 경우 트리의 높이에 따라 공간 복잡도가 증가합니다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

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);
}
}
Loading