Skip to content

[yuseok89] WEEK 10 Solutions - #2836

Merged
parkhojeong merged 7 commits into
DaleStudy:mainfrom
yuseok89:main
Aug 29, 2026
Merged

parkhojeong merged 7 commits into
DaleStudy:mainfrom
yuseok89:main

Conversation

@yuseok89

@yuseok89 yuseok89 commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

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

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

  • 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/yuseok89.py
# TC: O(N)
# SC: O(N)
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:

        if not root:
            return None
        else:
            cur_node = TreeNode(root.val)

            cur_node.right = self.invertTree(root.left)
            cur_node.left = self.invertTree(root.right)

            return cur_node
  • ํŒจํ„ด: Binary Search, Depth-First Search, Divide and Conquer
  • ์„ค๋ช…: ์ด ์ฝ”๋“œ๋Š” ์ด์ง„ํŠธ๋ฆฌ์˜ ์ž์‹ ๋…ธ๋“œ๋ฅผ ์„œ๋กœ ๋ฐ”๊ฟ” ํŠธ๋ฆฌ๋ฅผ ๋ฐ˜์ „์‹œํ‚ค๋Š” ์žฌ๊ท€์  ๋ถ„ํ•  ์ฒ˜๋ฆฌ๋กœ์„œ, DFS ๋ฐฉ์‹์œผ๋กœ ๊ฐ ์„œ๋ธŒํŠธ๋ฆฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ฉฐ ๋ถ„ํ• ํ•ด์„œ ํ•ด๊ฒฐํ•˜๋Š” ํŒจํ„ด์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

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

์œ ์ € ๋ถ„์„ ์‹ค์ œ ๋ถ„์„ ๊ฒฐ๊ณผ
Time O(N) O(n) โœ…
Space O(N) O(h) โŒ

ํ”ผ๋“œ๋ฐฑ: ํŠธ๋ฆฌ์˜ ๊ฐ ๋…ธ๋“œ๋ฅผ ํ•œ ๋ฒˆ์”ฉ ๋ฐฉ๋ฌธํ•˜์—ฌ ์ž์‹ ์ •๋ณด๋ฅผ ๊ตํ™˜ํ•˜๋ฏ€๋กœ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n)์ด๊ณ , ์žฌ๊ท€ ํ˜ธ์ถœ ์Šคํƒ์˜ ๊นŠ์ด์— ๋น„๋ก€ํ•˜๋Š” ๊ณต๊ฐ„ ๋ณต์žก๋„ O(h)์ด๋‹ค.

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

@github-actions github-actions Bot added the py label Aug 24, 2026
@dalestudy

dalestudy Bot commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

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

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

๋ฌธ์ œ ๋‚œ์ด๋„ ์œ ํ˜• ๋ถ„์„
course-schedule Medium โœ… ์˜๋„ํ•œ ์œ ํ˜•
invert-binary-tree Easy โœ… ์˜๋„ํ•œ ์œ ํ˜•
jump-game Medium โœ… ์˜๋„ํ•œ ์œ ํ˜•
merge-k-sorted-lists Hard โœ… ์˜๋„ํ•œ ์œ ํ˜•
search-in-rotated-sorted-array Medium โœ… ์˜๋„ํ•œ ์œ ํ˜•

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

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

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

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

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

๐Ÿ”ข API ์‚ฌ์šฉ๋Ÿ‰ (gpt-5-nano)
์š”์ฒญ ์ž…๋ ฅ ํ† ํฐ ์ถœ๋ ฅ ํ† ํฐ ํ•ฉ๊ณ„ ๋น„์šฉ
1 347 37 384 $0.000032
2 1,876 192 2,068 $0.000171
3 1,805 208 2,013 $0.000173
ํ•ฉ๊ณ„ 4,028 437 4,465 $0.000376

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.

์•ž์˜ if๊ฐ€ ์ด๋ฏธ ๋ฆฌํ„ด์„ ํ•˜๊ณ  ์žˆ์–ด์„œ else ๋ฅผ ํฌ๊ฒŒ ์“ฐ์ง€ ์•Š์œผ์…”๋„ ๋˜์ง€ ์•Š์„๊นŒ? ๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ญ๋‹ˆ๋‹ค!
๋˜ํ•œ cur_node ๋ฅผ ๋งŒ๋“ค์ง€ ์•Š์œผ์‹œ๊ณ  root๋ฅผ ์ง์ ‘ ๋ฐ”๊พธ์…”๋„ ์ •๋‹ต์ฒ˜๋ฆฌ ๋˜์‹ค๊ฑฐ์—์š”!

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.

์ข‹๋„ค์š” !
๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ๐Ÿ™

@JeonJe
JeonJe self-requested a review August 25, 2026 12:48

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/yuseok89.py
# TC: O(N)
# SC: O(N)
class Solution:
    def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:

        prereq_dict = defaultdict(set)
        in_deg = [0] * numCourses
        is_completed = set()

        for prereq in prerequisites:
            prereq_dict[prereq[1]].add(prereq[0])
            in_deg[prereq[0]] += 1

        while True:
            course_list = []

            for idx in range(numCourses):
                if idx not in is_completed and in_deg[idx] == 0:
                    course_list.append(idx)

            if len(course_list) == 0:
                break

            for course in course_list:
                is_completed.add(course)

                for nxt in prereq_dict[course]:
                    in_deg[nxt] -= 1

        for idx in range(numCourses):
            if idx not in is_completed:
                return False

        return True
  • ํŒจํ„ด: Depth-First Search, Greedy, Dynamic Programming, Hash Map / Hash Set
  • ์„ค๋ช…: ๊ทธ๋ž˜ํ”„์˜์œ„์ƒ์ •๋ ฌ ์•„์ด๋””์–ด๋กœ ์„ ํ–‰๊ด€๊ณ„ ์ œ๊ฑฐํ•˜๋ฉฐ ์‚ฌ์ดํด์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ํŒจํ„ด์œผ๋กœ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. ๊ตฌํ˜„์€ indegree ๊ธฐ๋ฐ˜์œผ๋กœ 0์ธ ๋…ธ๋“œ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์ด๋ผ ์œ„์ƒ์ •๋ ฌ์˜ ์ „ํ˜•์  ํ๋ฆ„์„ ๋ณด์ด๊ณ , ๋ถ€ํŠธ์ŠคํŠธ๋žฉ ๊ณผ์ •์—์„œ ์šฐ์„ ์ˆœ์œ„๋ฅผ ์„ ํƒํ•˜๋Š” ์ ์ด ๊ทธ๋ฆฌ๋””ํ•˜๊ฒŒ ํ•ด์„๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ์ง‘ํ•ฉ์œผ๋กœ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

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

์œ ์ € ๋ถ„์„ ์‹ค์ œ ๋ถ„์„ ๊ฒฐ๊ณผ
Time O(N) O(V + E) โŒ
Space O(N) O(V + E) โŒ

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

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

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/yuseok89.py
# TC: O(N)
# SC: O(logN)
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:

        if not root:
            return None

        root.left, root.right = self.invertTree(root.right), self.invertTree(root.left)

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

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

์œ ์ € ๋ถ„์„ ์‹ค์ œ ๋ถ„์„ ๊ฒฐ๊ณผ
Time O(N) O(n) โœ…
Space O(logN) O(h) โŒ

ํ”ผ๋“œ๋ฐฑ: ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ํ•œ ๋ฒˆ์”ฉ ๋ฐฉ๋ฌธํ•˜๊ณ  ํ˜ธ์ถœ ์Šคํƒ ๊นŠ์ด๋Š” ํŠธ๋ฆฌ์˜ ๋†’์ด์™€ ๊ฐ™์Œ

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

Comment thread jump-game/yuseok89.py

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.

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

jump-game/yuseok89.py
# TC: O(N)
# SC: O(1)
class Solution:
    def canJump(self, nums: List[int]) -> bool:

        n = len(nums)
        m, idx = 0, 0

        while idx <= m and idx < n:
            m = max(m, idx + nums[idx])
            idx += 1

        return m >= n - 1
  • ํŒจํ„ด: Greedy, Two Pointers
  • ์„ค๋ช…: ์ด ์ฝ”๋“œ๋Š” ํ˜„์žฌ ์ธ๋ฑ์Šค๊นŒ์ง€ ๋„๋‹ฌ ๊ฐ€๋Šฅํ•œ ์ตœ๋Œ€ ๋„๋‹ฌ ์œ„์น˜๋ฅผ ์ถ”์ ํ•˜๋ฉฐ, ๊ฐ ์œ„์น˜์—์„œ ์•ž์œผ๋กœ ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ฒ”์œ„๋ฅผ ํ™•์žฅํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ตœ๋Œ“๊ฐ’์„ ๊ฐฑ์‹ ํ•œ๋‹ค. ๋‹จ์ผ ๋ฃจํ”„(์„ ํ˜•)๋กœ ๊ฐ€๋Šฅํ•œ์ง€ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•˜๋ฏ€๋กœ Greedy ํŒจํ„ด์— ์†ํ•˜๊ณ , ์–‘ ๋์„ ๋งค๊ฐœ๋กœ ํ•˜๋Š” ํƒ์ƒ‰์˜ ํ๋ฆ„์ด Two Pointers์™€ ์œ ์‚ฌํ•˜๊ฒŒ ๋™์ž‘ํ•œ๋‹ค.

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

์œ ์ € ๋ถ„์„ ์‹ค์ œ ๋ถ„์„ ๊ฒฐ๊ณผ
Time O(N) O(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.

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

merge-k-sorted-lists/yuseok89.py
# TC: O(NlogN)
# SC: O(N)
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:

        ret_head = ListNode()
        cur = ret_head

        heap = []
        for i, node in enumerate(lists):
            if node is not None:
                heapq.heappush(heap, (node.val, i, node))

        while heap:
            val, i, node = heapq.heappop(heap)
            cur.next = node
            cur = cur.next

            if node.next is not None:
                heapq.heappush(heap, (node.next.val, i, node.next))

        return ret_head.next
  • ํŒจํ„ด: Heap / Priority Queue, Divide and Conquer
  • ์„ค๋ช…: ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ๋ฅผ ํž™์œผ๋กœ ๋ณ‘ํ•ฉํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ตœ์†Œ ์›์†Œ๋ฅผ ๋งค๋ฒˆ ๊บผ๋‚ด๋Š” ํŒจํ„ด์ด๋‹ค. ๊ฐ ๋ฆฌ์ŠคํŠธ ์›์†Œ๋ฅผ ํž™์— ๋‹ด์•„ ์ˆœ์ฐจ์ ์œผ๋กœ ์—ฐ๊ฒฐํ•˜๋ฏ€๋กœ ์šฐ์„ ์ˆœ์œ„ ํ์˜ ํ™œ์šฉ์ด ํ•ต์‹ฌ์ด๋‹ค.

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

์œ ์ € ๋ถ„์„ ์‹ค์ œ ๋ถ„์„ ๊ฒฐ๊ณผ
Time O(NlogN) O(N log k) โŒ
Space O(N) O(k) โŒ

ํ”ผ๋“œ๋ฐฑ: ์ด ๋…ธ๋“œ ์ˆ˜๋ฅผ N์ด๋ผ ํ•  ๋•Œ ๊ฐ ์‚ฝ์ž…/์‚ญ์ œ๊ฐ€ ๋กœ๊ทธ k ์ด๋ฏ€๋กœ ์ „์ฒด ์‹œ๊ฐ„์€ O(N log k), ํž™์€ ๋ฆฌ์ŠคํŠธ ์ˆ˜ k ๋งŒํผ ์‚ฌ์šฉ

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

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/yuseok89.py
# TC: O(logN)
# SC: O(1)
class Solution:
    def search(self, nums: List[int], target: int) -> int:
        n = len(nums)
        l, r = 0, n - 1

        while l < r:
            m = (l + r) // 2

            if nums[m] > nums[r]:
                l = m + 1
            else:
                r = m

        if target >= nums[0]:
            r = (l + n - 1) % n
            l = 0
        else:
            r = n - 1

        while l <= r:
            m = (l + r) // 2

            if nums[m] == target:
                return m
            elif nums[m] < target:
                l = m + 1
            else:
                r = m - 1

        return -1
  • ํŒจํ„ด: Binary Search, Two Pointers
  • ์„ค๋ช…: ๊ตฌํ˜„์€ ํšŒ์ „๋œ ์ •๋ ฌ ๋ฐฐ์—ด์—์„œ ๋ชฉํ‘œ๊ฐ’์„ ์ด์ง„ ํƒ์ƒ‰์œผ๋กœ ์ฐพ๋Š” ๊ณผ์ •์„ ๋‘ ๋‹จ๊ณ„๋กœ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋จผ์ € ํšŒ์ „ ์œ„์น˜๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด ์ด์ง„ ํƒ์ƒ‰์˜ ํŠน์„ฑ์„ ์ด์šฉํ•˜๊ณ , ๊ทธ ํ›„ ๊ตฌ๊ฐ„์„ ๋‚˜๋ˆ„์–ด ์ด์ง„ ํƒ์ƒ‰์œผ๋กœ ํƒ€๊นƒ์„ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.

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

์œ ์ € ๋ถ„์„ ์‹ค์ œ ๋ถ„์„ ๊ฒฐ๊ณผ
Time O(logN) 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.

์ •๋ง ๊น”๋”ํ•˜๊ฒŒ ์ž˜ ํ•ด๊ฒฐํ•ด ์ฃผ์…จ๋Š”๋ฐ์š”!
์ œ ์ƒ๊ฐ์— is_completed์— ์ˆ˜๋ฃŒ๋ฅผ ์™„๋ฃŒํ•œ ๊ณผ๋ชฉ์˜ ๊ฐฏ์ˆ˜๋งŒ ๊ธฐ๋กํ•ด๋„ ๊ฐ€๋Šฅํ•˜์ง€ ์•Š์„๊นŒ? ๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ์–ด์š”
์–ด๋–ค์ง€ ํ•œ๋ฒˆ ์ƒ๊ฐํ•ด๋ณด์‹œ๋ฉด ๋”์šฑ ์ข‹์„๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!

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.

๋•๋ถ„์— ๋งŽ์€ ๊ณต๋ถ€๊ฐ€ ๋˜๋„ค์š”
๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค !

Comment thread jump-game/yuseok89.py

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.

์ƒ๋‹นํžˆ ๊น”๋”ํ•œ ๊ทธ๋ฆฌ๋””๋„ค์š”! ์ž˜ ๋ณด๊ณ  ๊ฐ‘๋‹ˆ๋‹ค

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.

๊น”๋”ํ•˜๊ฒŒ ์ž˜ ํ•ด๊ฒฐํ•ด ์ฃผ์…จ๋Š”๋ฐ์š”, ํ•ด๋‹น ๋ฌธ์ œ๋Š” ๋‹จ์ผ while๋กœ๋„ ํ•ด๊ฒฐ ๊ฐ€๋Šฅํ•˜๋‹ต๋‹ˆ๋‹ค!
ํ•œ๋ฒˆ ์‹œ๋„ํ•ด๋ณด์‹œ๋ฉด ์ข‹์„๊ฒƒ ๊ฐ™์•„์š”

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.

๊ทธ๋ ‡๋„ค์š” ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค !

l = m + 1
else:
r = m

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.

r = (l + n - 1) % n์—์„œ % n์˜ ์—ญํ• ์ด ์กฐ๊ธˆ ๋” ์‰ฝ๊ฒŒ ์ฝํžˆ๋ฉด ์ข‹๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ํšŒ์ „์ด ์—†์–ด์„œ ํ”ผ๋ฒ—์ด 0์ผ ๋•Œ l - 1 = -1์ด ๋˜๋Š” ๊ฑธ n - 1๋กœ ๋Œ๋ ค๋ง‰๋Š” ์šฉ๋„๋กœ ๋ณด์ด๋Š”๋ฐ, ์ฒ˜์Œ ์ฝ์œผ๋ฉด ์™œ ๋ชจ๋“ˆ๋Ÿฌ๊ฐ€ ๋‚˜์˜ค๋Š”์ง€ ํ•œ ๋ฒˆ ๋˜์งš์–ด๋ด์•ผ ํ•˜๋”๋ผ๊ณ ์š”. ์ฒซ ๋ฃจํ”„๊ฐ€ ๋๋‚œ l์— ์ด๋ฆ„์„ ๋ถ™์ด๊ณ  ๋‘ ๋ถ„๊ธฐ์—์„œ l, r์„ ๋‘˜ ๋‹ค ๋ช…์‹œํ•ด์ฃผ๋ฉด ์ฝ”๋“œ๊ฐ€ ์ข€ ๋” ๊ธธ์–ด์ง€๋”๋ผ๋„ ์ฝ๊ธฐ๋Š” ์‰ฌ์›Œ์งˆ ๊ฒƒ ๊ฐ™์•„์š”. ์˜ˆ๋ฅผ ๋“ค์–ด

  pivot = l
  if target >= nums[0]:
      l, r = 0, (pivot - 1 if pivot else n - 1)
  else:
      l, r = pivot, n - 1


while True:
course_list = []

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.

๋ผ์šด๋“œ๋งˆ๋‹ค for idx in range(numCourses)๋กœ ์ „์ฒด ๊ณผ๋ชฉ์„ ๋‹ค์‹œ ํ›‘๊ณ  ์žˆ์–ด์„œ, ๋ผ์šด๋“œ ์ˆ˜ ร— ๊ณผ๋ชฉ ์ˆ˜๋งŒํผ ๊ณ„์‚ฐ์ด ์˜ˆ์ƒ๋ฉ๋‹ˆ๋‹ค. ์„ ์ˆ˜๊ด€๊ณ„๊ฐ€ 0 โ†’ 1 โ†’ 2 โ†’ ...์ฒ˜๋Ÿผ ํ•œ ์ค„๋กœ ์ด์–ด์ง€๋ฉด ๋ผ์šด๋“œ๊ฐ€ ๊ณผ๋ชฉ ์ˆ˜๋งŒํผ ์ƒ๊ธฐ๋Š”๋ฐ, ์ œ์•ฝ ์ƒํ•œ์ธ 2000๊ณผ๋ชฉ์ด๋ฉด ๋ผ์šด๋“œ๊ฐ€ 2001ํšŒ ๋Œ๊ณ  ์Šค์บ”์ด 400๋งŒ ๋ฒˆ ์ผ์–ด๋‚˜์„œ O(Vยฒ + E)์— ๊ฐ€๊นŒ์šธ ๊ฒƒ ๊ฐ™์•„์š”.

in_deg๊ฐ€ ๊ฐ์†Œํ•˜๋Š” ๊ทธ ์ž๋ฆฌ์—์„œ 0์ด ๋๋Š”์ง€ ๋ฐ”๋กœ ํŒ์ •ํ•ด๋‘๋ฉด "๋‹ค์Œ์— ์ฒ˜๋ฆฌํ•  ๊ณผ๋ชฉ์„ ์ฐพ๋Š”" ๋‹จ๊ณ„๊ฐ€ ํ†ต์งธ๋กœ ์—†์–ด์ง€๊ณ , ๊ฐ ๊ณผ๋ชฉ๊ณผ ๊ฐ ๊ฐ„์„ ์„ ํ•œ ๋ฒˆ์”ฉ๋งŒ ๋ณด๊ฒŒ ๋˜์–ด O(V + E)๋กœ ์ค„์ผ ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ ํ•ฉ๋‹ˆ๋‹ค.

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.

๋•๋ถ„์— ๋งŽ์€ ๊ณต๋ถ€๊ฐ€ ๋˜๋„ค์š”
๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค !

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/yuseok89.py
# TC: O(V + E)
# SC: O(V + E)
class Solution:
    def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:

        graph = [[] for _ in range(numCourses)]
        in_deg = [0] * numCourses

        for nxt, prev in prerequisites:
            graph[prev].append(nxt)
            in_deg[nxt] += 1

        queue = deque(idx for idx in range(numCourses) if in_deg[idx] == 0)
        completed = 0

        while queue:
            course = queue.popleft()
            completed += 1

            for nxt in graph[course]:
                in_deg[nxt] -= 1
                if in_deg[nxt] == 0:
                    queue.append(nxt)

        return completed == numCourses
  • ํŒจํ„ด: BFS, Topological Sort
  • ์„ค๋ช…: ๊ทธ๋ž˜ํ”„์˜ ์œ„์ƒ ์ •๋ ฌ(BFS ๊ธฐ๋ฐ˜ ํ๋ฅผ ์ด์šฉํ•œ ์ˆœ์ฐจ ๋ฐฉ๋ฌธ)๋กœ ์‚ฌ์ดํด ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค. ์ง„์ž… ์ฐจ์ˆ˜, ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ™œ์šฉํ•ด ์ˆœ์„œ๋Œ€๋กœ ๋…ธ๋“œ๋ฅผ ์ œ๊ฑฐํ•˜๋ฉฐ ๋ชจ๋“  ์ •์ ์„ ๋ฐฉ๋ฌธํ•˜๋ฉด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

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

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

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

ํ”ผ๋“œ๋ฐฑ: ๊ทธ๋ž˜ํ”„๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  ์ง„์ž… ์ฐจ์ˆ˜๋กœ ํ๋ฅผ ๊ด€๋ฆฌํ•˜๋ฉด์„œ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์ฒ˜๋ฆฌํ•œ๋‹ค.

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

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

ํ”ผ๋“œ๋ฐฑ: ๊ทธ๋ž˜ํ”„๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  ์ง„์ž… ์ฐจ์ˆ˜๋กœ ํ๋ฅผ ๊ด€๋ฆฌํ•˜๋ฉด์„œ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์ฒ˜๋ฆฌํ•œ๋‹ค.

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

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/yuseok89.py
# TC: O(logN)
# SC: O(1)
class Solution:
    def search(self, nums: List[int], target: int) -> int:
        n = len(nums)
        l, r = 0, n - 1


        while l <= r:
            m = (l + r) // 2

            if nums[m] == target:
                return m

            if nums[l] <= nums[m]:
                if nums[l] <= target < nums[m]:
                    r = m - 1
                else:
                    l = m + 1
            else:
                if nums[m] < target <= nums[r]:
                    l = m + 1
                else:
                    r = m - 1

        return -1
  • ํŒจํ„ด: Binary Search, Monotonic Stack, Divide and Conquer
  • ์„ค๋ช…: ํšŒ์ „๋œ ์ •๋ ฌ ๋ฐฐ์—ด์—์„œ ์ค‘๊ฐ„๊ฐ’ ๋น„๊ต๋กœ ๋ฒ”์œ„๋ฅผ ์ ˆ๋ฐ˜์”ฉ ์ค„์—ฌ ํƒ์ƒ‰ํ•˜๋Š” ์ด์ง„ ํƒ์ƒ‰์˜ ๋ณ€ํ˜•์œผ๋กœ, ๋ฐฐ์—ด์˜ ๋‹จ์กฐ์„ฑ(๊ฐ ๋ถ€๋ถ„์ด ์ •๋ ฌ๋จ)์„ ์ด์šฉํ•ด target ์œ„์น˜๋ฅผ ์ถ”์ •ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ฃผ ํŒจํ„ด์€ Binary Search์ด๋ฉฐ, ๋ฐฐ์—ด์˜ ๊ตฌ๊ฐ„ ์ถ•์†Œ๊ฐ€ ํ•ต์‹ฌ์ž…๋‹ˆ๋‹ค.

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

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

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

ํ”ผ๋“œ๋ฐฑ: ํšŒ์ „๋œ ๋ฐฐ์—ด์—์„œ๋„ ์ค‘๋ณต ์—†์ด ๋ชฉํ‘œ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด ์–‘ ๋๊ณผ ์ค‘๊ฐ„ ๊ฐ’์„ ๋น„๊ตํ•˜๋ฉฐ ๊ตฌ๊ฐ„์„ ์ ˆ๋ฐ˜์œผ๋กœ ์ถ•์†Œํ•œ๋‹ค.

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

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

ํ”ผ๋“œ๋ฐฑ: ํšŒ์ „๋œ ๋ฐฐ์—ด์—์„œ๋„ ์ค‘๋ณต ์—†์ด ๋ชฉํ‘œ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด ์–‘ ๋๊ณผ ์ค‘๊ฐ„ ๊ฐ’์„ ๋น„๊ตํ•˜๋ฉฐ ๊ตฌ๊ฐ„์„ ์ ˆ๋ฐ˜์œผ๋กœ ์ถ•์†Œํ•œ๋‹ค.

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

@parkhojeong
parkhojeong merged commit d456146 into DaleStudy:main Aug 29, 2026
1 check passed
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.

4 participants