Skip to content
Open
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
52 changes: 52 additions & 0 deletions 3sum/xeulbn.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, Sorting, Hash Map / Hash Set
  • 설명: 3Sum 문제에서 정렬 후 두 포인터를 활용해 합이 0이 되면 정답을 수집하고, 합이 작으면 왼쪽 포인터를, 크면 오른쪽 포인터를 이동합니다. 중복 제거를 위한 추가 조건도 포함되어 있어 Two Pointers 패턴이 핵심입니다.

📊 시간/공간 복잡도 분석

ℹ️ 이 파일에는 2가지 풀이가 포함되어 있어 각각 분석합니다.

풀이 1: Solution.threeSum — Time: O(n^2) / Space: O(1)
복잡도
Time O(n^2)
Space O(1)

피드백: 정렬과 두 포인터 탐색으로 중복을 제거하고 필요 시 건너뛰기 처리까지 잘 구현되어 있습니다.

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

풀이 2: Solution.threeSum — Time: O(n^2) / Space: O(1)
복잡도
Time O(n^2)
Space O(1)

피드백: 중복 제거 로직이 올바르게 작동하며 시간 복잡도도 최적에 가깝습니다.

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import java.util.*;

//시간복잡도 : O(n^2), 공간복잡도 : O(nlogn)

@namuuCY namuuCY Jul 2, 2026

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.

혹시 이거 왜 공간복잡도가 O(n log n)이 되는지 설명해주실 수 있나요?
sort에 쓰이는 추가 공간복잡도가 logn 정도는 될 것 같은데.. 그것외에는 nlogn까지 올라가는 이유가 코드상으로는 와닿지 않아서 여쭤봅니다!


class Solution {
public List<List<Integer>> threeSum(int[] nums) {

Arrays.sort(nums);

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.

TMI) js는 기본이 문자열 정렬이라 이 풀이를 그대로 js로 옮기면 틀려요 java 좋네요

[10, 2, 1].sort();            // → [1, 10, 2]  😱  비교자 없으면 문자열 정렬
[10, 2, 1].sort((a,b)=>a-b);  // → [1, 2, 10]  이렇게 해야 함

List<List<Integer>> answer = new ArrayList<>();

for(int i=0;i<nums.length-2;i++){
if (i>0 && nums[i] == nums[i-1]) {
continue;
}
int leftIndex = i+1;
int rightIndex = nums.length-1;

while(leftIndex<rightIndex){
int sum =nums[i]+nums[leftIndex]+nums[rightIndex];

if(sum==0){
// 정답 추가
List<Integer> tmpList = new ArrayList<>();
tmpList.add(nums[i]);
tmpList.add(nums[leftIndex]);
tmpList.add(nums[rightIndex]);

answer.add(tmpList);

// left++, right--
leftIndex+=1;
rightIndex-=1;

// 중복 제거
while(leftIndex<rightIndex && nums[leftIndex]==nums[leftIndex-1]){
leftIndex+=1;
}
while(leftIndex<rightIndex && nums[rightIndex]==nums[rightIndex+1]){
rightIndex-=1;
}

}else if(sum<0){
leftIndex+=1;
}else{
rightIndex-=1;
}
}
}

return answer;
}
}
23 changes: 23 additions & 0 deletions climbing-stairs/xeulbn.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로 각 단계의 최적 해를 저장하고 이를 이용해 최종 해를 구한다. 공간 최적화 여지가 있지만 본 코드에서는 배열을 사용한다.

📊 시간/공간 복잡도 분석

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

피드백: 정확한 동작과 적절한 초기값 설정으로 선형 시간/공간 복잡도를 만족합니다.

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.util.*;

//시간복잡도 : O(n), 공간복잡도 : O(n)
//이전 두 값만 필요하기에, 공간복잡도는 O(1)까지 최적화가 가능.
Comment on lines +3 to +4

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.

공간 최적화가 잘 되어서 좋은 것 같습니다!


class Solution {
public int climbStairs(int n) {
if(n<=2){
return n;
}

int[] dp =new int[n+1];
dp[1]=1;
dp[2]=2;

for(int i=3;i<n+1;i++){
dp[i]=dp[i-1]+dp[i-2];
}

return dp[n];

}
}
15 changes: 15 additions & 0 deletions contains-duplicate/xeulbn.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, Greedy
  • 설명: 중복 여부를 확인하기 위해 해시 세트를 사용하여 한 번 방문한 값은 재방문 여부를 빠르게 체크하는 방식으로 중복 탐지를 수행합니다. 전형적인 해시 기반 탐색으로, 시간 복잡도는 O(n), 추가 공간은 O(n) 입니다.

📊 시간/공간 복잡도 분석

ℹ️ 이 파일에는 2가지 풀이가 포함되어 있어 각각 분석합니다.

풀이 1: Solution.containsDuplicate — Time: O(n) / Space: O(n)
복잡도
Time O(n)
Space O(n)

피드백: 해시 세트를 이용한 선형 시간 검사로 최적의 해결

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

풀이 2: Solution.containsDuplicate — Time: O(n) / Space: O(n)
복잡도
Time O(n)
Space O(n)

피드백: 해시 세트를 이용한 선형 시간 검사로 최적의 해결

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import java.util.*;

class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> numberCheck = new HashSet<>();

for(int num : nums){
if(numberCheck.contains(num)){
return true;
}
numberCheck.add(num);
}
return false;
}
}
21 changes: 21 additions & 0 deletions house-robber/xeulbn.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
  • 설명: 배열의 부분 문제로 최대 금액을 구하는 점화식 dp[i] = max(dp[i-1], dp[i-2] + nums[i])를 이용한 전형적인 DP 풀이이며, 중복 계산을 피하기 위한 최적 부분구조를 활용합니다.

📊 시간/공간 복잡도 분석

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

피드백: 연속되게 커지는 값을 피하기 위한 최적 부분구조를 올바르게 활용

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import java.util.*;

class Solution {

public int rob(int[] nums) {
if (nums.length == 1) {
return nums[0];
}

int[] dp = new int[nums.length];

dp[0] = nums[0];
dp[1] = Math.max(nums[0], nums[1]);

for (int i = 2; i < nums.length; i++) {
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
}

return dp[nums.length - 1];
}
}
29 changes: 29 additions & 0 deletions longest-consecutive-sequence/xeulbn.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, Greedy, Two Pointers
  • 설명: 집합에 존재 여부를 빠르게 확인하고, 시작점에서 연속 부분을 확장하는 방식으로 최댓값을 구하므로 해시 세트와 탐색 방향의 확장으로 구성된 그리디/투 포인터 유사 패턴에 해당합니다.

📊 시간/공간 복잡도 분석

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

피드백: 각 수를 한 번씩만 탐색하는 방식으로 최적의 시간 복잡도 확보

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.*;

class Solution {
public int longestConsecutive(int[] nums) {
Set<Integer> set = new HashSet<>();

for (int num : nums) {
set.add(num);
}

int maxLength = 0;

for (int num : set) {
if (!set.contains(num - 1)) {
int currentNum = num;
int currentLength = 1;

while (set.contains(currentNum + 1)) {
currentNum++;
currentLength++;
}

maxLength = Math.max(maxLength, currentLength);
}
}

return maxLength;
}
}
23 changes: 23 additions & 0 deletions product-of-array-except-self/xeulbn.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, Hash Map / Hash Set, Dynamic Programming
  • 설명: 배열의 각 원소를 곱으로 만들되 자기 자신은 제외하는 결과를 구하기 위해, 좌우 누적곱을 각각 미리 계산한 뒤 곱해 최종 값을 얻는 방식으로 진행합니다. 공간을 상수 배치로 유지하기 위해 결과 배열을 활용하는 점이 특징입니다.

📊 시간/공간 복잡도 분석

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

피드백: 두 패스 방식으로 불필요한 나눗셈 없이 구현

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.util.*;

//시간 복잡도 O(n), 공간 복잡도 O(1)

class Solution {
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] answer = new int[n];

answer[0] = 1;
for (int i=1; i<n; i++) {
answer[i] = answer[i-1] * nums[i-1];
}

int suffix = 1;
for (int i = n-1; i >= 0; i--) {
answer[i] = answer[i] * suffix;
suffix *= nums[i];
}

return answer;
}
}
31 changes: 31 additions & 0 deletions top-k-frequent-elements/xeulbn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import java.util.*;

class Solution {
public int[] topKFrequent(int[] nums, int k) {
Map<Integer,Integer> countMap = new HashMap<>();

for(int i=0;i<nums.length;i++){
countMap.put(nums[i],countMap.getOrDefault(nums[i],0)+1);
}
PriorityQueue<Integer> pq = new PriorityQueue<>(
(a,b)-> countMap.get(a) - countMap.get(b)
);

for(int num: countMap.keySet()){
pq.offer(num);

if(pq.size()>k){
pq.poll();
}
}

int[] answer = new int[k];

for (int i = 0; i < k; i++) {
answer[i] = pq.poll();
}

return answer;

}
}
17 changes: 17 additions & 0 deletions two-sum/xeulbn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import java.util.*;

class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> numIndexMap = new HashMap<>();

for (int i=0; i<nums.length; i++) {
int need = target - nums[i];
if (numIndexMap.containsKey(need)) {
return new int[]{numIndexMap.get(need), i};
}
numIndexMap.put(nums[i], i);
}

return new int[]{};
}
}
33 changes: 33 additions & 0 deletions valid-anagram/xeulbn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import java.util.*;

// 시간복잡도 : O(n), 공간복잡도 : O(n)

class Solution {
public boolean isAnagram(String s, String t) {

if(s.length()!=t.length()){
return false;
}

Map<Character,Integer> characterMap = new HashMap<>();

for(int i=0;i<s.length();i++){
char c = s.charAt(i);
characterMap.put(c,characterMap.getOrDefault(c,0)+1);
}

for(int i=0;i<t.length();i++){
char c = t.charAt(i);

if(!characterMap.containsKey(c)){
return false;
}

characterMap.put(c, characterMap.get(c) - 1);
if(characterMap.get(c)<0){
return false;
}
}
return true;
}
}
39 changes: 39 additions & 0 deletions validate-binary-search-tree/xeulbn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.util.*;


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 validate(root, Long.MIN_VALUE, Long.MAX_VALUE);
}

private boolean validate(TreeNode node, long min, long max) {
if (node == null) {
return true;
}

if (node.val <= min || node.val >= max) {
return false;
}

return validate(node.left, min, node.val) && validate(node.right, node.val, max);
}
}
Loading