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
16 changes: 16 additions & 0 deletions merge-intervals/Yiseull.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.

🏷️ 알고리즘 패턴 분석

merge-intervals/Yiseull.java
class Solution {
    public int[][] merge(int[][] intervals) {
        Arrays.sort(intervals, (o1, o2) -> o1[0] - o2[0]);

        int index = 0;
        for (int i = 0; i < intervals.length; i++) {
            if (intervals[index][1] < intervals[i][0]) {
                intervals[++index] = intervals[i];
            } else {
                intervals[index][1] = Math.max(intervals[index][1], intervals[i][1]);
            }
        }

        return Arrays.copyOf(intervals, index + 1);
    }
}
  • 패턴: Two Pointers, Greedy, Sort
  • 설명: 간단한 정렬 후 투포인터로 중복 구간을 합치는 방식으로 문제를 해결합니다. 연속 구간 비교 및 합치기 과정이 핵심이며, 두 포인터로 구간 배열을 한 번 순회합니다.

📊 시간/공간 복잡도 분석

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

피드백: 입력 구간을 시작점 기준으로 정렬한 뒤, 현재 구간과 다음 구간의 관계를 검사하며 필요 시 합치는 방식으로 한 번의 순회로 최종 구간들을 얻습니다.

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

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

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.

저는 정답 배열을 선언했는데, intervals 배열을 인플레이스로 수정한 후에 슬라이스하셨군요

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.

네넹! okyungjin님 리뷰 달 때 안그래도 인플레이스 방법 말씀드리려다가 말았었는데, 클로드 말로는 자바에서 인플레이스가 의미 있었던 건 ArrayList 증설 + 구간마다 clone() + toArray 복사가 한꺼번에 사라졌기 때문인데, 파이썬은 없어지는 게 리스트 객체 하나와 append 호출뿐이라 자바만큼에 큰 이득은 아니라고 하더라구요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public int[][] merge(int[][] intervals) {
Arrays.sort(intervals, (o1, o2) -> o1[0] - o2[0]);

int index = 0;
for (int i = 0; i < intervals.length; i++) {
if (intervals[index][1] < intervals[i][0]) {
intervals[++index] = intervals[i];
} else {
intervals[index][1] = Math.max(intervals[index][1], intervals[i][1]);
}
}

return Arrays.copyOf(intervals, index + 1);
}
}
13 changes: 13 additions & 0 deletions missing-number/Yiseull.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.

🏷️ 알고리즘 패턴 분석

missing-number/Yiseull.java
class Solution {
    public int missingNumber(int[] nums) {
        int n = nums.length;
        int expectedSum = n * (n + 1) / 2;
        int actualSum = 0;

        for (int num : nums) {
            actualSum += num;
        }

        return expectedSum - actualSum;
    }
}
  • 패턴: Greedy, Bit Manipulation, Dynamic Programming, Divide and Conquer, Hash Map / Hash Set, Two Pointers, Sliding Window, Fast & Slow Pointers, BFS, DFS, Backtracking, Union Find, Trie, Monotonic Stack, Heap / Priority Queue, Binary Search, Divide and Conquer, Monotonic Stack
  • 설명: 해당 코드는 등차합을 이용해 누락된 숫자를 구하는 방식으로, 패턴은 보통 '수학적 접근'에 가까운 그리디나 비트 조합 패턴으로도 해석 가능하지만 주로 합의 차를 이용하는 아이디어는 DP/탐색과 무관합니다. 본 경우에 가장 적절한 패턴은 Greedy 혹은 Bit Manipulation으로 간주될 수 있습니다.

📊 시간/공간 복잡도 분석

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

피드백: 배열의 모든 원소를 한 번 순회하며 합을 구하고, 가용한 산술로 누락된 숫자를 계산합니다.

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

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

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.

파이썬에는 sum 함수가 있어서 찾아보니 자바에서는 아래와 같이 작성할 수 있는 것 같습니다!
for로 합산하는 것과 차이가 있으려나요?

Arrays.stream(nums).sum();

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.

자바에서 for문이랑 stream이랑 약간의 성능이랑 동작 차이가 있는데, 저는 이 풀이에서는 그런것까진 따지지 않고 for문을 쓰긴 했어요 ㅎ

클로드한테 물어보니 아래일 때 어떤 걸 선택하는게 좋은지 정리해주네요~!

  • 데이터 변환·집계 파이프라인(filter → map → collect, groupingBy) → stream이 압도적으로 읽기 좋습니다
  • 인덱스가 필요하거나 두 리스트를 동시에 순회 → for문
  • 루프 내에서 외부 상태를 바꿔야 함 → for문 (stream에서 하면 side effect라 병렬 시 깨집니다)
  • 조기 종료 + 복잡한 조건 분기 → for문
  • 원시 타입 대량 연산의 핫패스 → for문, 혹은 IntStream

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public int missingNumber(int[] nums) {
int n = nums.length;
int expectedSum = n * (n + 1) / 2;
int actualSum = 0;

for (int num : nums) {
actualSum += num;
}

return expectedSum - actualSum;
}
}
Loading