[Yiseull] WEEK 11 Solutions - #2853
Conversation
There was a problem hiding this comment.
๐ท๏ธ ์๊ณ ๋ฆฌ์ฆ ํจํด ๋ถ์
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) |
ํผ๋๋ฐฑ: ์ ๋ ฅ ๊ตฌ๊ฐ์ ์์์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ ๋ค, ํ์ฌ ๊ตฌ๊ฐ๊ณผ ๋ค์ ๊ตฌ๊ฐ์ ๊ด๊ณ๋ฅผ ๊ฒ์ฌํ๋ฉฐ ํ์ ์ ํฉ์น๋ ๋ฐฉ์์ผ๋ก ํ ๋ฒ์ ์ํ๋ก ์ต์ข ๊ตฌ๊ฐ๋ค์ ์ป์ต๋๋ค.
๊ฐ์ ์ ์: ํ์ฌ ๊ตฌํ์ด ์ ์ ํด ๋ณด์ ๋๋ค.
๐ก ํ์ด์ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋๋ฅผ ์ฃผ์์ผ๋ก ๋จ๊ฒจ๋ณด์ธ์!
๐ Yiseull ๋์ ํ์ต ํํฉ์ด๋ฒ ์ฃผ ์ ์ถ ๋ฌธ์
๋์ ํ์ต ์์ฝ
๋ฌธ์ ํ์ด ํํฉ
๐ค ์ด ๋๊ธ์ GitHub App์ ํตํด ์๋์ผ๋ก ์์ฑ๋์์ต๋๋ค. ๐ข API ์ฌ์ฉ๋ (gpt-5-nano)
|
There was a problem hiding this comment.
๐ท๏ธ ์๊ณ ๋ฆฌ์ฆ ํจํด ๋ถ์
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) |
ํผ๋๋ฐฑ: ๋ฐฐ์ด์ ๋ชจ๋ ์์๋ฅผ ํ ๋ฒ ์ํํ๋ฉฐ ํฉ์ ๊ตฌํ๊ณ , ๊ฐ์ฉํ ์ฐ์ ๋ก ๋๋ฝ๋ ์ซ์๋ฅผ ๊ณ์ฐํฉ๋๋ค.
๊ฐ์ ์ ์: ํ์ฌ ๊ตฌํ์ด ์ ์ ํด ๋ณด์ ๋๋ค.
๐ก ํ์ด์ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋๋ฅผ ์ฃผ์์ผ๋ก ๋จ๊ฒจ๋ณด์ธ์!
okyungjin
left a comment
There was a problem hiding this comment.
๊ณ ์ํ์ จ์ต๋๋ค๐
There was a problem hiding this comment.
ํ์ด์ฌ์๋ sum ํจ์๊ฐ ์์ด์ ์ฐพ์๋ณด๋ ์๋ฐ์์๋ ์๋์ ๊ฐ์ด ์์ฑํ ์ ์๋ ๊ฒ ๊ฐ์ต๋๋ค!
for๋ก ํฉ์ฐํ๋ ๊ฒ๊ณผ ์ฐจ์ด๊ฐ ์์ผ๋ ค๋์?
Arrays.stream(nums).sum();
There was a problem hiding this comment.
์๋ฐ์์ for๋ฌธ์ด๋ stream์ด๋ ์ฝ๊ฐ์ ์ฑ๋ฅ์ด๋ ๋์ ์ฐจ์ด๊ฐ ์๋๋ฐ, ์ ๋ ์ด ํ์ด์์๋ ๊ทธ๋ฐ๊ฒ๊น์ง ๋ฐ์ง์ง ์๊ณ for๋ฌธ์ ์ฐ๊ธด ํ์ด์ ใ
ํด๋ก๋ํํ ๋ฌผ์ด๋ณด๋ ์๋์ผ ๋ ์ด๋ค ๊ฑธ ์ ํํ๋๊ฒ ์ข์์ง ์ ๋ฆฌํด์ฃผ๋ค์~!
- ๋ฐ์ดํฐ ๋ณํยท์ง๊ณ ํ์ดํ๋ผ์ธ(filter โ map โ collect, groupingBy) โ stream์ด ์๋์ ์ผ๋ก ์ฝ๊ธฐ ์ข์ต๋๋ค
- ์ธ๋ฑ์ค๊ฐ ํ์ํ๊ฑฐ๋ ๋ ๋ฆฌ์คํธ๋ฅผ ๋์์ ์ํ โ for๋ฌธ
- ๋ฃจํ ๋ด์์ ์ธ๋ถ ์ํ๋ฅผ ๋ฐ๊ฟ์ผ ํจ โ for๋ฌธ (stream์์ ํ๋ฉด side effect๋ผ ๋ณ๋ ฌ ์ ๊นจ์ง๋๋ค)
- ์กฐ๊ธฐ ์ข ๋ฃ + ๋ณต์กํ ์กฐ๊ฑด ๋ถ๊ธฐ โ for๋ฌธ
- ์์ ํ์ ๋๋ ์ฐ์ฐ์ ํซํจ์ค โ for๋ฌธ, ํน์ IntStream
There was a problem hiding this comment.
์ ๋ ์ ๋ต ๋ฐฐ์ด์ ์ ์ธํ๋๋ฐ, intervals ๋ฐฐ์ด์ ์ธํ๋ ์ด์ค๋ก ์์ ํ ํ์ ์ฌ๋ผ์ด์คํ์ จ๊ตฐ์
There was a problem hiding this comment.
๋ค๋น! okyungjin๋ ๋ฆฌ๋ทฐ ๋ฌ ๋ ์๊ทธ๋๋ ์ธํ๋ ์ด์ค ๋ฐฉ๋ฒ ๋ง์๋๋ฆฌ๋ ค๋ค๊ฐ ๋ง์์๋๋ฐ, ํด๋ก๋ ๋ง๋ก๋ ์๋ฐ์์ ์ธํ๋ ์ด์ค๊ฐ ์๋ฏธ ์์๋ ๊ฑด ArrayList ์ฆ์ค + ๊ตฌ๊ฐ๋ง๋ค clone() + toArray ๋ณต์ฌ๊ฐ ํ๊บผ๋ฒ์ ์ฌ๋ผ์ก๊ธฐ ๋๋ฌธ์ธ๋ฐ, ํ์ด์ฌ์ ์์ด์ง๋ ๊ฒ ๋ฆฌ์คํธ ๊ฐ์ฒด ํ๋์ append ํธ์ถ๋ฟ์ด๋ผ ์๋ฐ๋งํผ์ ํฐ ์ด๋์ ์๋๋ผ๊ณ ํ๋๋ผ๊ตฌ์!
๋ต์ ์ ์ถ ๋ฌธ์
์์ฑ์ ์ฒดํฌ ๋ฆฌ์คํธ
In Review๋ก ์ค์ ํด์ฃผ์ธ์.๊ฒํ ์ ์ฒดํฌ ๋ฆฌ์คํธ
Important
๋ณธ์ธ ๋ต์ ์ ์ถ ๋ฟ๋ง ์๋๋ผ ๋ค๋ฅธ ๋ถ PR ํ๋ ์ด์์ ๋ฐ๋์ ๊ฒํ ๋ฅผ ํด์ฃผ์ ์ผ ํฉ๋๋ค!