Skip to content

[JeonJe] WEEK 10 Solutions - #2837

Open
JeonJe wants to merge 3 commits into
DaleStudy:mainfrom
JeonJe:week10
Open

[JeonJe] WEEK 10 Solutions#2837
JeonJe wants to merge 3 commits into
DaleStudy:mainfrom
JeonJe:week10

Conversation

@JeonJe

@JeonJe JeonJe commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

๋‹ต์•ˆ ์ œ์ถœ ๋ฌธ์ œ

  • Invert Binary Tree
  • Search in Rotated Sorted Array
  • Course Schedule
  • Jump Game
  • Merge K Sorted Lists

์ž‘์„ฑ์ž ์ฒดํฌ ๋ฆฌ์ŠคํŠธ

  • Projects์˜ ์˜ค๋ฅธ์ชฝ ๋ฒ„ํŠผ(โ–ผ)์„ ๋ˆŒ๋Ÿฌ ํ™•์žฅํ•œ ๋’ค, Week๋ฅผ ํ˜„์žฌ ์ฃผ์ฐจ๋กœ ์„ค์ •ํ•ด์ฃผ์„ธ์š”.
  • ๋ฌธ์ œ๋ฅผ ๋ชจ๋‘ ํ‘ธ์‹œ๋ฉด ํ”„๋กœ์ ํŠธ์—์„œ Status๋ฅผ In Review๋กœ ์„ค์ •ํ•ด์ฃผ์„ธ์š”.
  • ์ฝ”๋“œ ๊ฒ€ํ† ์ž 1๋ถ„ ์ด์ƒ์œผ๋กœ๋ถ€ํ„ฐ ์Šน์ธ์„ ๋ฐ›์œผ์…จ๋‹ค๋ฉด PR์„ ๋ณ‘ํ•ฉํ•ด์ฃผ์„ธ์š”.

๊ฒ€ํ† ์ž ์ฒดํฌ ๋ฆฌ์ŠคํŠธ

Important

๋ณธ์ธ ๋‹ต์•ˆ ์ œ์ถœ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ๋ถ„ PR ํ•˜๋‚˜ ์ด์ƒ์„ ๋ฐ˜๋“œ์‹œ ๊ฒ€ํ† ๋ฅผ ํ•ด์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค!

  • ๋ฐ”๋กœ ์ด์ „์— ์˜ฌ๋ผ์˜จ PR์— ๋ณธ์ธ์„ ์ฝ”๋“œ ๋ฆฌ๋ทฐ์–ด๋กœ ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”.
  • ๋ณธ์ธ์ด ๊ฒ€ํ† ํ•ด์•ผํ•˜๋Š” PR์˜ ๋‹ต์•ˆ ์ฝ”๋“œ์— ํ”ผ๋“œ๋ฐฑ์„ ์ฃผ์„ธ์š”.
  • ํ† ์š”์ผ ์ „๊นŒ์ง€ PR์„ ๋ณ‘ํ•ฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์Šน์ธํ•ด์ฃผ์„ธ์š”.

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.

๐Ÿท๏ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํŒจํ„ด ๋ถ„์„

invert-binary-tree/JeonJe.java
import java.util.*;

// TC: O(n)
// SC: O(n)
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }

        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;

        invertTree(root.left);
        invertTree(root.right);

        return root;
    }
}
  • ํŒจํ„ด: Depth-First Search, Binary Search
  • ์„ค๋ช…: ๋ฆฌํ”„๊นŒ์ง€ ๊นŠ์ด ํƒ์ƒ‰ํ•˜๋ฉฐ ์–‘์ชฝ ์ž์‹ ๋…ธ๋“œ๋ฅผ ์„œ๋กœ ๊ตํ™˜ํ•˜๋Š” ์žฌ๊ท€ DFS ๋ฐฉ์‹์œผ๋กœ ํŠธ๋ฆฌ๋ฅผ ๋ฐ˜์ „์‹œํ‚ต๋‹ˆ๋‹ค. ๊ฐ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•ด ์ขŒ์šฐ๋ฅผ ์Šค์™•ํ•˜๊ณ  ํ•˜์œ„ ํŠธ๋ฆฌ๋„ ๋™์ผํ•˜๊ฒŒ ์žฌ๊ท€์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“Š ์‹œ๊ฐ„/๊ณต๊ฐ„ ๋ณต์žก๋„ ๋ถ„์„

โ„น๏ธ ์ด ํŒŒ์ผ์—๋Š” 2๊ฐ€์ง€ ํ’€์ด๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์–ด ๊ฐ๊ฐ ๋ถ„์„ํ•ฉ๋‹ˆ๋‹ค.

ํ’€์ด 1: Solution.invertTree โ€” Time: โœ… O(n) โ†’ O(n) / Space: โŒ O(n) โ†’ O(h)
์œ ์ € ๋ถ„์„ ์‹ค์ œ ๋ถ„์„ ๊ฒฐ๊ณผ
Time O(n) O(n) โœ…
Space O(n) O(h) โŒ

ํ”ผ๋“œ๋ฐฑ: ํŠธ๋ฆฌ์˜ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•˜๋ฉฐ ๊ฐ ๋…ธ๋“œ์˜ ์ขŒ์šฐ ์ž์‹ ํฌ์ธํ„ฐ๋ฅผ ๊ตํ™˜ํ•œ๋‹ค. ์žฌ๊ท€ ๊นŠ์ด๋Š” ํŠธ๋ฆฌ์˜ ๋†’์ด์— ๋”ฐ๋ผ ๊ณต๊ฐ„์ด ๊ฒฐ์ •๋œ๋‹ค.

๊ฐœ์„  ์ œ์•ˆ: ํ˜„์žฌ ๊ตฌํ˜„์ด ์ ์ ˆํ•ด ๋ณด์ž…๋‹ˆ๋‹ค.

ํ’€์ด 2: Solution.invertTree โ€” Time: โœ… O(n) โ†’ O(n) / Space: โŒ O(n) โ†’ O(h)
์œ ์ € ๋ถ„์„ ์‹ค์ œ ๋ถ„์„ ๊ฒฐ๊ณผ
Time O(n) O(n) โœ…
Space O(n) O(h) โŒ

ํ”ผ๋“œ๋ฐฑ: ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ํ•œ ๋ฒˆ์”ฉ ๋ฐฉ๋ฌธํ•˜๊ณ  ํฌ์ธํ„ฐ๋ฅผ ์Šค์™‘ํ•˜๋ฏ€๋กœ ์‹œ๊ฐ„์€ ์„ ํ˜•, ์žฌ๊ท€ ์Šคํƒ์€ ํŠธ๋ฆฌ์˜ ๋†’์ด์— ๋น„๋ก€ํ•œ๋‹ค.

๊ฐœ์„  ์ œ์•ˆ: ํ˜„์žฌ ๊ตฌํ˜„์ด ์ ์ ˆํ•ด ๋ณด์ž…๋‹ˆ๋‹ค.

@dalestudy

dalestudy Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

๐Ÿ“Š JeonJe ๋‹˜์˜ ํ•™์Šต ํ˜„ํ™ฉ

์ด๋ฒˆ ์ฃผ ์ œ์ถœ ๋ฌธ์ œ

๋ฌธ์ œ ๋‚œ์ด๋„ ์œ ํ˜• ๋ถ„์„
course-schedule Medium โœ… ์˜๋„ํ•œ ์œ ํ˜•
invert-binary-tree Easy โœ… ์˜๋„ํ•œ ์œ ํ˜•
search-in-rotated-sorted-array Medium โš ๏ธ ์œ ํ˜• ๋ถˆ์ผ์น˜

๋ˆ„์  ํ•™์Šต ์š”์•ฝ

  • ํ’€์ดํ•œ ๋ฌธ์ œ: 38 / 75๊ฐœ
  • ์ด๋ฒˆ ์ฃผ ์œ ํ˜• ์ผ์น˜์œจ: 67% (3๋ฌธ์ œ ์ค‘ 2๋ฌธ์ œ ์ผ์น˜)

๋ฌธ์ œ ํ’€์ด ํ˜„ํ™ฉ

์นดํ…Œ๊ณ ๋ฆฌ ์ง„ํ–‰๋„ ์™„๋ฃŒ
Dynamic Programming โ– โ– โ– โ– โ– โ– โ–ก 9 / 11 (Easy 1, Medium 8)
Array โ– โ– โ– โ– โ– โ– โ–ก 8 / 10 (Medium 5, Easy 3)
String โ– โ– โ– โ– โ– โ–กโ–ก 7 / 10 (Medium 4, Easy 3)
Matrix โ– โ– โ– โ– โ–กโ–กโ–ก 2 / 4 (Medium 2)
Binary โ– โ– โ– โ–กโ–กโ–กโ–ก 2 / 5 (Easy 2)
Graph โ– โ– โ– โ–กโ–กโ–กโ–ก 3 / 8 (Medium 3)
Linked List โ– โ– โ–กโ–กโ–กโ–กโ–ก 2 / 6 (Easy 2)
Heap โ– โ– โ–กโ–กโ–กโ–กโ–ก 1 / 3 (Medium 1)
Tree โ– โ– โ–กโ–กโ–กโ–กโ–ก 4 / 14 (Medium 3, Easy 1)
Interval โ–กโ–กโ–กโ–กโ–กโ–กโ–ก 0 / 5 โ† ์•„์ง ์‹œ์ž‘ ์•ˆ ํ•จ

๐Ÿค– ์ด ๋Œ“๊ธ€์€ GitHub App์„ ํ†ตํ•ด ์ž๋™์œผ๋กœ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ”ข API ์‚ฌ์šฉ๋Ÿ‰ (gpt-5-nano)
์š”์ฒญ ์ž…๋ ฅ ํ† ํฐ ์ถœ๋ ฅ ํ† ํฐ ํ•ฉ๊ณ„ ๋น„์šฉ
1 808 84 892 $0.000074
2 1,382 111 1,493 $0.000114
ํ•ฉ๊ณ„ 2,190 195 2,385 $0.000188

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.

๐Ÿท๏ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํŒจํ„ด ๋ถ„์„

search-in-rotated-sorted-array/JeonJe.java
import java.util.*;

// TC: O(log n)
// SC: O(1)
class Solution {
    public int search(int[] nums, int target) {
        int pivot = findRotationPoint(nums);

        if (pivot > 0 && target >= nums[0]) {
            return binarySearch(nums, target, 0, pivot - 1);
        }
        return binarySearch(nums, target, pivot, nums.length - 1);
    }

    private int findRotationPoint(int[] nums) {
        int lo = 0;
        int hi = nums.length - 1;

        while (lo < hi) {
            if (nums[lo] < nums[hi]) {
                return lo;
            }

            int mid = (lo + hi) / 2;
            if (nums[lo] <= nums[mid]) {
                lo = mid + 1;
            } else {
                hi = mid;
            }
        }

        return lo;
    }

    private int binarySearch(int[] nums, int target, int lo, int hi) {
        while (lo <= hi) {
            int mid = (lo + hi) / 2;
            if (nums[mid] == target) {
                return mid;
            }
            if (nums[mid] < target) {
                lo = mid + 1;
            } else {
                hi = mid - 1;
            }
        }

        return -1;
    }
}
  • ํŒจํ„ด: Binary Search, Divide and Conquer
  • ์„ค๋ช…: ๋ฐฐ์—ด์„ ๋‘ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆ  ์ด์ง„ ํƒ์ƒ‰์œผ๋กœ ํƒ€๊นƒ์„ ์ฐพ๋Š” ๊ตฌ์กฐ์ด๋ฉฐ, ํšŒ์ „๋œ ๋ฐฐ์—ด์—์„œ ํ”ผ๋ฒ—์„ ์ฐพ์•„ ๊ตฌ๊ฐ„์„ ๋‚˜๋ˆ  ์ด์ง„ ํƒ์ƒ‰์„ ์ˆ˜ํ–‰ํ•˜๋Š” ํŒจํ„ด์ด๋‹ค. ๋”ฐ๋ผ์„œ Binary Search์™€ Divide and Conquer์— ํ•ด๋‹นํ•œ๋‹ค.

๐Ÿ“Š ์‹œ๊ฐ„/๊ณต๊ฐ„ ๋ณต์žก๋„ ๋ถ„์„

์œ ์ € ๋ถ„์„ ์‹ค์ œ ๋ถ„์„ ๊ฒฐ๊ณผ
Time O(log n) O(log n) โœ…
Space O(1) 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.

ํ”ผ๋ด‡ ํฌ์ธํŠธ๋ฅผ ์ฐพ์•„์„œ ํ•˜๋Š”๊ฒƒ๋„ ๊ฝค ์ข‹์€ ์•„์ด๋””์–ด์‹œ๋„ค์š”!
๋‹ค๋งŒ ํ•œ๋ฒˆ์˜ binary search๋กœ ํ•ด๊ฒฐํ•˜์‹ท์ˆ˜๋„ ์žˆ์–ด์š”!

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.

๋ง์”€ํ•ด์ฃผ์‹  ์ œ์•ˆ์„ ์ƒ๊ฐํ•ด๋ณด๋‹ˆ, mid๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ๋œ ๊ตฌ๊ฐ„์„ ํ™•์ธํ•˜๊ณ 
ํƒ€๊นƒ์ด ํ•ด๋‹น ๋ฒ”์œ„์— ์žˆ๋Š”์ง€์— ๋”ฐ๋ผ ํƒ์ƒ‰ ๋ฒ”์œ„๋ฅผ ์ค„์—ฌ๊ฐ€๋Š” ๋ฐฉ์‹์œผ๋กœ
ํ”ผ๋ฒ— ์œ„์น˜๋ฅผ ๋จผ์ € ๊ตฌํ•˜์ง€ ์•Š๊ณ ๋„ ํ•œ ๋ฒˆ์˜ ์ด์ง„ ํƒ์ƒ‰์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋„ค์š”. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

Comment on lines +15 to +16
invertTree(root.left);
invertTree(root.right);

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.

ํ•ด๋‹น ๋‘ ๋ถ€๋ถ„์ด ๊ฐ๊ฐ์˜ ์„œ๋ธŒํŠธ๋ฆฌ๋ฅผ ๋ฆฌํ„ดํ•˜๋‹ˆ๊นŒ ์ด๊ฑธ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•ด์„œ root.left์™€ root.right์— ๋„ฃ์–ด๋„ ์ข‹์ง€ ์•Š์„๊นŒ ํ•˜๋Š” ์ƒ๊ฐ์ด ์žˆ์Šต๋‹ˆ๋‹ค!

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.

๋ฐ˜ํ™˜๋œ ์„œ๋ธŒํŠธ๋ฆฌ๋ฅผ root.left์™€ root.right์— ๋‹ค์‹œ ๋„ฃ์„์ง€ ๊ณ ๋ฏผํ–ˆ๋Š”๋ฐ,
๊ฐ ์„œ๋ธŒํŠธ๋ฆฌ๋ฅผ ์ œ์ž๋ฆฌ์—์„œ ๋ณ€๊ฒฝํ•˜๋ฏ€๋กœ ๋‹ค์‹œ ๋Œ€์ž…ํ•˜์ง€ ์•Š๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.
์˜๋„๋ฅผ ๋” ๋ช…ํ™•ํ•˜๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ์ œ์•ˆ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

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.

๐Ÿท๏ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํŒจํ„ด ๋ถ„์„

course-schedule/JeonJe.java
import java.util.ArrayList;
import java.util.List;

// TC: O(V + E)
// SC: O(V + E)
class Solution {
    private static final int UNVISITED = 0;
    private static final int VISITING = 1;
    private static final int VISITED = 2;

    public boolean canFinish(int numCourses, int[][] prerequisites) {
        List<List<Integer>> nextCourses = new ArrayList<>(numCourses);
        int[] visitState = new int[numCourses];

        for (int course = 0; course < numCourses; course++) {
            nextCourses.add(new ArrayList<>());
        }

        for (int[] dependency : prerequisites) {
            int course = dependency[0];
            int prerequisite = dependency[1];
            nextCourses.get(prerequisite).add(course);
        }

        for (int course = 0; course < numCourses; course++) {
            if (visitState[course] == UNVISITED
                    && hasCycle(course, nextCourses, visitState)) {
                return false;
            }
        }

        return true;
    }

    private boolean hasCycle(
            int course,
            List<List<Integer>> nextCourses,
            int[] visitState
    ) {
        if (visitState[course] == VISITING) {
            return true;
        }
        if (visitState[course] == VISITED) {
            return false;
        }

        visitState[course] = VISITING;

        for (int nextCourse : nextCourses.get(course)) {
            if (hasCycle(nextCourse, nextCourses, visitState)) {
                return true;
            }
        }

        visitState[course] = VISITED;
        return false;
    }
}
  • ํŒจํ„ด: Depth-First Search, Greedy
  • ์„ค๋ช…: ์ฝ”๋“œ์—์„œ DFS๋ฅผ ์‚ฌ์šฉํ•ด ์œ„์ƒ ๊ทธ๋ž˜ํ”„์˜ ์‚ฌ์ดํด ์—ฌ๋ถ€๋ฅผ ํƒ์ง€ํ•ฉ๋‹ˆ๋‹ค. ๋ฐฉ๋ฌธ ์ƒํƒœ๋ฅผ ์ถ”์ ํ•˜๋ฉฐ ์‚ฌ์ดํด์ด ์žˆ์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์‚ฌ์ดํด์ด ์—†์œผ๋ฉด ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค. ์œ„์ƒ ์ •๋ ฌ-์‚ฌ์ดํด ํŒ๋ณ„์˜ ๋Œ€ํ‘œ ํŒจํ„ด์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ“Š ์‹œ๊ฐ„/๊ณต๊ฐ„ ๋ณต์žก๋„ ๋ถ„์„

โ„น๏ธ ์ด ํŒŒ์ผ์—๋Š” 2๊ฐ€์ง€ ํ’€์ด๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์–ด ๊ฐ๊ฐ ๋ถ„์„ํ•ฉ๋‹ˆ๋‹ค.

ํ’€์ด 1: Solution.canFinish โ€” Time: โœ… O(V + E) โ†’ O(V + E) / Space: โœ… O(V + E) โ†’ O(V + E)
์œ ์ € ๋ถ„์„ ์‹ค์ œ ๋ถ„์„ ๊ฒฐ๊ณผ
Time O(V + E) O(V + E) โœ…
Space O(V + E) O(V + E) โœ…

ํ”ผ๋“œ๋ฐฑ: ๊ทธ๋ž˜ํ”„๋ฅผ ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋กœ ํ‘œํ˜„ํ•˜๊ณ  DFS๋กœ ์‚ฌ์ดํด ์—ฌ๋ถ€๋ฅผ ๊ฒ€์‚ฌํ•œ๋‹ค. ๊ฐ ๋…ธ๋“œ๋Š” ํ•œ ๋ฒˆ๋งŒ ๋ฐฉ๋ฌธํ•˜๋ฏ€๋กœ ์ „์ฒด ์‹œ๊ฐ„์€ ์ •์ /๊ฐ„์„ ์˜ ํ•ฉ์— ๋น„๋ก€ํ•œ๋‹ค.

๊ฐœ์„  ์ œ์•ˆ: ํ˜„์žฌ ๊ตฌํ˜„์ด ์ ์ ˆํ•ด ๋ณด์ž…๋‹ˆ๋‹ค.

ํ’€์ด 2: Solution.hasCycle โ€” Time: O(V + E) / Space: O(V + E)
๋ณต์žก๋„
Time O(V + E)
Space O(V + E)

ํ”ผ๋“œ๋ฐฑ: ๊ฐ ๋…ธ๋“œ์˜ ๋ฐฉ๋ฌธ ์ƒํƒœ๋ฅผ ์„ธ ๊ฐ€์ง€๋กœ ๊ด€๋ฆฌํ•˜์—ฌ ์‚ฌ์ดํด์„ ๊ฒ€์ถœํ•œ๋‹ค. ์žฌ๊ท€ ๊นŠ์ด์— ์ฃผ์˜ ํ•„์š”.

๊ฐœ์„  ์ œ์•ˆ: ํ˜„์žฌ ๊ตฌํ˜„์ด ์ ์ ˆํ•ด ๋ณด์ž…๋‹ˆ๋‹ค.

๐Ÿ’ก ํ’€์ด์— ์‹œ๊ฐ„/๊ณต๊ฐ„ ๋ณต์žก๋„๋ฅผ ์ฃผ์„์œผ๋กœ ๋‚จ๊ฒจ๋ณด์„ธ์š”!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

2 participants