Skip to content

[alphaorderly] WEEK 07 Solutions - #2793

Merged
alphaorderly merged 5 commits into
DaleStudy:mainfrom
alphaorderly:main
Aug 9, 2026
Merged

[alphaorderly] WEEK 07 Solutions#2793
alphaorderly merged 5 commits into
DaleStudy:mainfrom
alphaorderly:main

Conversation

@alphaorderly

@alphaorderly alphaorderly commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

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

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

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

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

Important

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

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

@dalestudy

dalestudy Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

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

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

๋ฌธ์ œ ๋‚œ์ด๋„ ์œ ํ˜• ๋ถ„์„
longest-substring-without-repeating-characters Medium โœ… ์˜๋„ํ•œ ์œ ํ˜•
number-of-islands Medium โœ… ์˜๋„ํ•œ ์œ ํ˜•
reverse-linked-list Easy โœ… ์˜๋„ํ•œ ์œ ํ˜•
set-matrix-zeroes Medium โœ… ์˜๋„ํ•œ ์œ ํ˜•
unique-paths Medium โœ… ์˜๋„ํ•œ ์œ ํ˜•

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

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

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

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

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

๐Ÿ”ข API ์‚ฌ์šฉ๋Ÿ‰ (gpt-5-nano)
์š”์ฒญ ์ž…๋ ฅ ํ† ํฐ ์ถœ๋ ฅ ํ† ํฐ ํ•ฉ๊ณ„ ๋น„์šฉ
1 2,225 179 2,404 $0.000183
2 3,371 216 3,587 $0.000255
3 3,352 220 3,572 $0.000256
4 3,335 212 3,547 $0.000252
5 3,335 207 3,542 $0.000250
6 3,337 241 3,578 $0.000263
7 3,335 260 3,595 $0.000271
ํ•ฉ๊ณ„ 22,290 1,535 23,825 $0.001729

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.

2D DP์—์„œ dp ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•˜๊ณ 
๋Œ€๊ฐ์„ ์œผ๋กœ ๋ณด๋ฉด Pascal Triangle์ด ๋งŒ๋“ค์–ด์ง‘๋‹ˆ๋‹ค.
์ด๊ฒƒ๋Œ€๋กœ Combination ์—ฐ์‚ฐ์œผ๋กœ ํ•˜์…”๋„ ๋งจ ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์–ป์„์ˆ˜ ์žˆ์œผ์„ธ์š”!

@DaleStudy DaleStudy deleted a comment from dalestudy Bot Aug 3, 2026
@DaleStudy DaleStudy deleted a comment from dalestudy Bot Aug 3, 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.

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

  • ํŒจํ„ด: Sliding Window, Hash Map / Hash Set
  • ์„ค๋ช…: ๋‘ ํฌ์ธํ„ฐ(left, right)๋กœ ์—ฐ์† ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ์ฐฝ/window๋กœ ํ™•์žฅ ์ถ•์†Œํ•˜๋ฉฐ ์„œ๋กœ ๋‹ค๋ฅธ ๋ฌธ์ž๋งŒ ๋‚จ๋„๋ก ์ค‘๋ณต ์—ฌ๋ถ€๋ฅผ ํ•ด์‹œ ๋งต์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. ์ด๋Š” ๊ธธ์ด๊ฐ€ ์ตœ๋Œ€๋กœ ๋˜๋Š” ๋ชจ๋“  ๊ณ ์œ  ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ์ฐพ๋Š” ์ผ๋ฐ˜์ ์ธ Sliding Window ํŒจํ„ด์— ํ•ด๋‹นํ•œ๋‹ค.

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

๋ณต์žก๋„
Time O(n)
Space O(n)

ํ”ผ๋“œ๋ฐฑ: ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ์™€ ํ•ด์‹œ๋งต(๋”•์…”๋„ˆ๋ฆฌ) ํ™œ์šฉ์œผ๋กœ ๊ฐ ๋ฌธ์ž ๋“ฑ์žฅ ์—ฌ๋ถ€๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค. right ์ฆ๊ฐ€์— ๋”ฐ๋ผ ์ค‘๋ณต์ด ์ƒ๊ธฐ๋ฉด left๋ฅผ ์ด๋™์‹œํ‚ค๋ฉฐ ์œˆ๋„์šฐ ํฌ๊ธฐ๋ฅผ ์กฐ์ •ํ•œ๋‹ค.

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

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

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.

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

  • ํŒจํ„ด: Depth-First Search, Breadth-First Search, Hash Map / Hash Set
  • ์„ค๋ช…: ์ฝ”๋“œ๋Š” BFS์™€ DFS ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹์œผ๋กœ ์„ฌ์„ ํƒ์ƒ‰ํ•˜๋ฉฐ, ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด ๊ฒฉ์ž ๋ฐ์ดํ„ฐ๋ฅผ '#''๋กœ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค. ๋‘ ๊ตฌํ˜„ ๋ชจ๋‘ ์ธ์ ‘ํ•œ ๋•…์„ ํƒ์ƒ‰ํ•˜๊ณ  ์„ฌ์˜ ์ˆ˜๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๋Š” ํŒจํ„ด์„ ๋ณด์ด๋ฉฐ, ๋ฐฉ๋ฌธ ๊ด€๋ฆฌ์— ์ถ”๊ฐ€ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ์ ์—์„œ ํ•ด์‹œ ๋งต/์„ธํŠธ์˜ ์ง์ ‘ ์‚ฌ์šฉ์€ ๋ณด์กฐ์ ์œผ๋กœ ํŒ๋‹จ๋ฉ๋‹ˆ๋‹ค.

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

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

ํ’€์ด 1: Solution.numIslands โ€” Time: O(m * n) / Space: O(m * n)
๋ณต์žก๋„
Time O(m * n)
Space O(m * n)

ํ”ผ๋“œ๋ฐฑ: ๋‘ ๊ฐ€์ง€ ๊ตฌํ˜„์ด ์ œ์‹œ๋˜์–ด ์žˆ์œผ๋ฉฐ ๋ชจ๋‘ ํƒ์ƒ‰์œผ๋กœ ๋ชจ๋“  ์œก์ง€('1')๋ฅผ ๋ฐฉ๋ฌธํ•œ๋‹ค. ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด grid๋ฅผ ๋ณ€ํ˜•ํ•˜์—ฌ ์ถ”๊ฐ€ ๊ณต๊ฐ„์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

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

ํ’€์ด 2: Solution.numIslands โ€” Time: O(m * n) / Space: O(m * n)
๋ณต์žก๋„
Time O(m * n)
Space O(m * n)

ํ”ผ๋“œ๋ฐฑ: ์žฌ๊ท€๋ฅผ ์ด์šฉํ•œ DFS๋กœ ๋ชจ๋“  ์—ฐ๊ฒฐ๋œ ์œก์ง€๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค. ์Šคํƒ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ตฌํ˜„๊ณผ ๋™์ผํ•œ ์‹œ๊ฐ„/๊ณต๊ฐ„ ๋ณต์žก๋„๋‹ค.

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

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

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, Linked List
  • ์„ค๋ช…: ํ—ค๋“œ์™€ ํ”„๋ฆฌ๋ธŒ ํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•ด linked list๋ฅผ ์—ญ์ˆœ์œผ๋กœ ์ˆœํšŒํ•˜๋ฉฐ ๋…ธ๋“œ ์—ฐ๊ฒฐ์„ ์žฌ์„ค์ •ํ•˜๋Š” ์ „ํ˜•์ ์ธ ํˆฌ ํฌ์ธํ„ฐ ๊ธฐ๋ฒ•. ๊ณต๊ฐ„ ๋ณต์žก๋„ O(1), ๋ฐ˜๋ณต ๊ตฌ์กฐ๋กœ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•œ ๋ฒˆ ์ˆœํšŒํ•œ๋‹ค.

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

๋ณต์žก๋„
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.

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

  • ํŒจํ„ด: Dynamic Programming, Greedy, Hash Map / Hash Set
  • ์„ค๋ช…: ์ด ์ฝ”๋“œ๋Š” ํ–‰/์—ด์˜ ํ”Œ๋ž˜๊ทธ๋ฅผ ์ฒซ ํ–‰/์—ด์„ ์ž„์‹œ ์ €์žฅ์†Œ๋กœ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ œ๋กœ๋ฅผ ํ‘œ์‹œํ•˜๋Š” ํŒจํ„ด์ด๋‹ค. ๊ณต๊ฐ„์„ ์ถ”๊ฐ€๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์›๋ž˜ ๋ฐฐ์—ด์˜ ํ–‰/์—ด์„ ์ด์šฉํ•ด ์กฐ๊ฑด์„ ์ „ํŒŒํ•˜๋ฏ€๋กœ ์ผ๋ฐ˜์ ์œผ๋กœ ์ตœ์ ํ™”๋œ ํƒ์ƒ‰/ํ‘œ์‹œ ๊ธฐ๋ฒ•์œผ๋กœ ๋ถ„๋ฅ˜๋œ๋‹ค.

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

๋ณต์žก๋„
Time O(m * 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.

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

  • ํŒจํ„ด: Dynamic Programming, Combinatorial, Memoization
  • ์„ค๋ช…: ๋‹ค์–‘ํ•œ ๊ตฌํ˜„์—์„œ 2D/1D DP๋ฅผ ์ด์šฉํ•œ ์ตœ๋‹จ ๊ฒฝ๋กœ์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ , ์žฌ๊ท€+์บ์‹œ(๋ฉ”๋ชจ์ด์ œ์ด์…˜) ํŒจํ„ด์œผ๋กœ ์ค‘๋ณต ๊ณ„์‚ฐ์„ ์ค„์ด๋Š” ์‚ฌ๋ก€๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์žฌ๊ท€ ๊ธฐ๋ฐ˜์˜ ์ƒํ–ฅ์‹ DP๋ฅผ ๋ณด์™„ํ•˜๋Š” memoization ๊ธฐ๋ฒ•์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

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

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

ํ’€์ด 1: Solution.uniquePaths โ€” Time: O(m * n) / Space: O(m * n)
๋ณต์žก๋„
Time O(m * n)
Space O(m * n)

ํ”ผ๋“œ๋ฐฑ: ์—ฌ๋Ÿฌ ์ ‘๊ทผ ๋ฐฉ์‹์ด ํฌํ•จ๋˜์–ด ์žˆ์ง€๋งŒ ๊ฐ๊ฐ์˜ ๊ตฌํ˜„์€ ์„œ๋กœ ๋‹ค๋ฅธ ๊ณต๊ฐ„/์‹œ๊ฐ„ ํŠน์„ฑ์„ ๊ฐ€์ง„๋‹ค. ๋ฌธ์ œ์— ๋”ฐ๋ผ ์„ ํƒ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

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

ํ’€์ด 2: Solution.uniquePaths โ€” Time: O(m * n) / Space: O(n)
๋ณต์žก๋„
Time O(m * n)
Space O(n)

ํ”ผ๋“œ๋ฐฑ: ํ–‰๋งˆ๋‹ค ์—ด์˜ ๊ฒฝ๋กœ๋ฅผ ๋ˆ„์  ์—…๋ฐ์ดํŠธํ•˜์—ฌ ๊ณต๊ฐ„์„ ์ค„์ธ๋‹ค.

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

ํ’€์ด 3: Solution.uniquePaths โ€” Time: O(1) / Space: O(1)
๋ณต์žก๋„
Time O(1)
Space O(1)

ํ”ผ๋“œ๋ฐฑ: ํŒฉํ† ๋ฆฌ์–ผ ๊ธฐ๋ฐ˜์˜ ์กฐํ•ฉ ๊ณ„์‚ฐ์œผ๋กœ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” ์ž…๋ ฅ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง€์ง€๋งŒ ์ผ๋ฐ˜์ ์œผ๋กœ ์ƒ์ˆ˜ ๊ณ„์ˆ˜์˜ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.

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

ํ’€์ด 4: Solution.uniquePaths โ€” Time: O(m * n) / Space: O(m * n)
๋ณต์žก๋„
Time O(m * n)
Space O(m * n)

ํ”ผ๋“œ๋ฐฑ: ์žฌ๊ท€ ๊ธฐ๋ฐ˜์˜ ๋ฐฉ๋ฒ•์œผ๋กœ ์ค‘๋ณต๋˜๋Š” ๋ถ€๋ถ„ ๋ฌธ์ œ๋ฅผ ์ €์žฅํ•ด ํšจ์œจ์„ ์œ ์ง€ํ•œ๋‹ค.

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

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

@parkhojeong
parkhojeong self-requested a review August 5, 2026 03:45

@parkhojeong parkhojeong left a comment

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.

์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค. ํ’€์ด์™€ ๊ด€๊ณ„ ์—†๋Š” ์ปค๋ฐ‹์€ ์ •๋ฆฌ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

Comment on lines +20 to +22
while count[val] > 1:
count[s[left]] -= 1
left += 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.

left๋ฅผ ํ•˜๋‚˜์”ฉ ์˜ฎ๊ธฐ์ง€ ์•Š๊ณ  ์ค‘๋ณต๋˜๋Š” ์ธ๋ฑ์Šค ๋ฐ”๋กœ ๋‹ค์Œ์œผ๋กœ ์˜ฎ๊ฒจ์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ตœ์ ํ™” ํ•ด๋ณผ ์ˆ˜ ์žˆ์„ ๊ฑฐ ๊ฐ™๋„ค์š”

Comment on lines +17 to +18
row_check = any(matrix[0][c] == 0 for c in range(COL))
col_check = any(matrix[r][0] == 0 for r in range(ROW))

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.

row_check, col_check๊ฐ€ ์–ด๋–ค ์˜๋ฏธ์ธ์ง€ ์•Œ๊ธฐ๋Š” ์–ด๋ ค์šด ๊ฑฐ ๊ฐ™์•„์„œ ์ ์ ˆํ•œ ๋„ค์ด๋ฐ ํ•ด์ฃผ์‹œ๋ฉด ์ข‹์„ ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Comment on lines +51 to +53
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
return comb(m + n - 2, n - 1)

@parkhojeong parkhojeong Aug 7, 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.

์˜ค ์ด๋ ‡๊ฒŒ๋„ ํ’€ ์ˆ˜ ์žˆ๊ตฐ์š”. ํŒจํ„ด์ด ์žˆ๋Š” ๊ฑฐ ๊ฐ™์•„ ๋ณด์ด๊ธด ํ–ˆ๋Š”๋ฐ ์ˆ˜์‹ ๋„์ถœ์ด ์ž˜ ์•ˆ๋˜๋”๋ผ๊ตฌ์š”. ์ด๋Ÿฐ ํ’€์ด ์•„์ด๋””์–ด๋Š” ์–ด๋–ป๊ฒŒ ์–ป์œผ์…จ๋Š”์ง€ ๊ถ๊ธˆํ•˜๋„ค์š”.

Comment on lines +22 to +23
while head:
prev, head.next, head = head, prev, head.next

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.

์ฒซ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” head๊ฐ€ ์ˆœํšŒํ•˜๋Š” ๋…ธ๋“œ๋กœ ์‚ฌ์šฉ๋˜์–ด์„œ ์‹ค์ œ๋กœ๋Š” ๋‘ ๊ฐ€์ง€ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๋Š” ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ˆœํšŒ ์ค‘์ธ ๋…ธ๋“œ๋ฅผ ์œ„ํ•œ ๋ณ„๋„์˜ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ์–ด๋–จ๊นŒ์š”?

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.

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

longest-substring-without-repeating-characters/alphaorderly.py
"""
Time Complexity: O(n)
Space Complexity: O(n)

Approach:
- Use a defaultdict to track the count of each character in the current window.
- Maintain two pointers, 'left' and 'right', to represent the sliding window over the string.
- As we iterate over the string with 'right', increment the count for the current character.
- If a duplicate character appears in the window (count > 1), move the 'left' pointer forward and decrement counts until there are no duplicates.
- After adjusting, update 'ans' with the maximum length found for a window with all unique characters.
"""
class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        count = defaultdict(int)
        ans = left = 0

        for right, val in enumerate(s):
            count[val] += 1

            while count[val] > 1:
                count[s[left]] -= 1
                left += 1

            ans = max(ans, right - left + 1)

        return ans
  • ํŒจํ„ด: Sliding Window, Hash Map / Hash Set
  • ์„ค๋ช…: ๋‘ ํฌ์ธํ„ฐ(left, right)๋กœ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ์ฐฝ์„ ํ™•์žฅ/์ถ•์†Œํ•˜๋ฉฐ ์ค‘๋ณต ๋ฌธ์ž๋ฅผ ํ•ด์‹œ ๋งต์œผ๋กœ ์ถ”์ ํ•œ๋‹ค. ์ค‘๋ณต ๋ฐœ์ƒ ์‹œ ์ฐฝ์„ ์ขŒ์ธก์œผ๋กœ ์ขํ˜€ ๊ธธ์ด๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅธ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์˜ ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ ๊ตฌํ•œ๋‹ค.

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

๋ณต์žก๋„
Time O(n)
Space O(n)

ํ”ผ๋“œ๋ฐฑ: ํ˜„์žฌ ๊ตฌํ˜„์€ ํˆฌ ํฌ์ธํ„ฐ์™€ ํ•ด์‹œ๋งต์œผ๋กœ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ํ•œ ๋ฒˆ์”ฉ ๋ฐฉ๋ฌธํ•˜๋ฏ€๋กœ ์ตœ์•…์˜ ๊ฒฝ์šฐ ์„ ํ˜• ์‹œ๊ฐ„๊ณผ ์„ ํ˜• ๊ณต๊ฐ„์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

๊ฐœ์„  ์ œ์•ˆ: ๊ณ ๋ คํ•ด๋ณผ ๋งŒํ•œ ๋Œ€์•ˆ: ์นด์šดํŠธ ๋Œ€์‹  ์ธ๋ฑ์Šค ์ €์žฅ ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋ถˆํ•„์š”ํ•œ ์นด์šดํŠธ๋ฅผ ์ค„์ด๊ณ , ๋”•์…”๋„ˆ๋ฆฌ ๋Œ€์‹  ๋ฐฐ์—ด ๊ธฐ๋ฐ˜ ์ธ๋ฑ์Šค ๋งคํ•‘์„ ์‚ฌ์šฉํ•ด ์ƒ์ˆ˜ ๊ณต๊ฐ„์— ๊ฐ€๊น๊ฒŒ ์ตœ์ ํ™” ๊ฐ€๋Šฅ.

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

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.

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

number-of-islands/alphaorderly.py
"""
Time Complexity: O(m * n)
Space Complexity: O(m * n)

### BFS Approach ###

Approach:
- Use BFS to traverse all parts of each island in the grid.
- Employ a queue to process all adjacent land cells iteratively.
- Use a 'bound' helper function to check if a cell is within the grid bounds.
- In 'island_marker', mark visited '1's with '#' to avoid revisiting.
- For every cell in the grid, when a land cell ('1') is encountered, initiate BFS and increment the island count.
"""
class Solution:
    def numIslands(self, grid: List[List[str]]) -> int:
        DIR = [[0, 1], [1, 0], [-1, 0], [0, -1]]
        ROW = len(grid)
        COL = len(grid[0])

        def bound(row: int, col: int) -> bool:
            return 0 <= row < ROW and 0 <= col < COL

        def island_marker(row: int, col: int) -> None:
            q = deque([(row, col)])
            grid[row][col] = "#"

            while q:
                r, c = q.popleft()

                for dr, dc in DIR:
                    tr, tc = r + dr, c + dc

                    if not bound(tr, tc) or grid[tr][tc] != "1":
                        continue

                    grid[tr][tc] = "#"
                    q.append((tr, tc))

        ans = 0

        for r in range(ROW):
            for c in range(COL):
                if grid[r][c] == "1":
                    island_marker(r, c)
                    ans += 1

        return ans

"""
Time Complexity: O(m * n)
Space Complexity: O(m * n)

### DFS Approach ###

Approach:
- Use DFS to traverse all parts of each island in the grid.
- Visitation is done recursively rather than with a stack, so stack comment is removed for clarity.
- Use a 'bound' helper function to check if a cell is within the grid bounds.
- In 'island_marker', mark visited '1's with '#' to avoid revisiting.
- For every cell in the grid, when a land cell ('1') is encountered, initiate DFS and increment the island count.
"""
class Solution:
    def numIslands(self, grid: List[List[str]]) -> int:
        DIR = [[0, 1], [1, 0], [-1, 0], [0, -1]]
        ROW = len(grid)
        COL = len(grid[0])

        def bound(row: int, col: int) -> bool:
            return 0 <= row < ROW and 0 <= col < COL

        def island_marker(row: int, col: int) -> None:
            grid[row][col] = '#'

            for dr, dc in DIR:
                tr, tc = row + dr, col + dc
                if bound(tr, tc) and grid[tr][tc] == '1':
                    island_marker(tr, tc)

        ans = 0

        for r in range(ROW):
            for c in range(COL):
                if grid[r][c] == "1":
                    island_marker(r, c)
                    ans += 1

        return ans
  • ํŒจํ„ด: Depth-First Search, Breadth-First Search, Hash Map / Hash Set
  • ์„ค๋ช…: ์ฝ”๋“œ์—์„œ ์„ฌ์„ ๋ชจ์–‘๋Œ€๋กœ ํƒ์ƒ‰ํ•˜๋ฉฐ ์ธ์ ‘ํ•œ ๋•…์„ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌํ•œ๋‹ค. DFS ๊ตฌํ˜„์€ ์žฌ๊ท€๋กœ, BFS ๊ตฌํ˜„์€ ํ๋กœ ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰์„ ์ด์šฉํ•œ๋‹ค. ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋Š” '#''๋กœ ๋งˆํ‚นํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ด€๋ฆฌ๋˜๋ฉฐ, ๊ฐ™์€ ์›์†Œ๋ฅผ ๋‹ค์‹œ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š๋Š”๋‹ค.

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

๋ณต์žก๋„
Time O(m * n)
Space O(m * n)

ํ”ผ๋“œ๋ฐฑ: ๋‘ ๊ฐ€์ง€ ๊ตฌํ˜„์ด ์ œ์‹œ๋˜์—ˆ๊ณ , ๋‘˜ ๋‹ค ์ „์ฒด ๊ฒฉ์ž๋ฅผ ํ•œ ๋ฒˆ ์ด์ƒ ํƒ์ƒ‰ํ•ฉ๋‹ˆ๋‹ค. ์ถ”๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ์€ BFS๊ฐ€ ํฐ ์ฐจ์ง€์ž…๋‹ˆ๋‹ค.

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

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

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.

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

reverse-linked-list/alphaorderly.py
"""
Time Complexity: O(n)
Space Complexity: O(1)

- We use a while loop to traverse the linked list.
- We use a prev pointer to store the previous node.
- We use a head pointer to store the current node.
- We use a old_next pointer to store the next node.
- We use a prev, head = head, old_next to update the prev and head pointers.
- We return the prev pointer.
"""

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        prev = None

        while head:
            prev, head.next, head = head, prev, head.next

        return prev
  • ํŒจํ„ด: Two Pointers, Linked List, Greedy, Dynamic Programming
  • ์„ค๋ช…: ํ—ค๋“œ์™€ ํ”„๋ฆฌ๋ธŒ ํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•ด ๋‹จ๋ฐฉํ–ฅ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋ฅผ ์—ญ์ˆœ์œผ๋กœ ์ˆœํšŒํ•˜๋ฉด์„œ ํฌ์ธํ„ฐ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ ๋ฒˆ์˜ ์ˆœํšŒ๋กœ O(n) ์‹œ๊ฐ„, O(1) ์ถ”๊ฐ€ ๊ณต๊ฐ„์œผ๋กœ ์—ญ์ˆœ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

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

๋ณต์žก๋„
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.

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

set-matrix-zeroes/alphaorderly.py
"""
Time Complexity: O(m * n)
Space Complexity: O(1)

Approach:
- Use the first row and first column as markers to track which rows and columns should be zeroed.
- First, check if the original first row or first column should be zeroed by scanning them separately.
- Then, scan the rest of the matrix. If an element is zero, set its corresponding first row and first column positions to zero.
- Next, iterate through the matrix (excluding the first row and column) and set elements to zero if their corresponding first row or first column are zero.
- Finally, zero the first row and/or first column if initially flagged.
"""
class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
        ROW = len(matrix)
        COL = len(matrix[0])

        row_zero_check = any(matrix[0][c] == 0 for c in range(COL))
        col_zero_check = any(matrix[r][0] == 0 for r in range(ROW))

        for r in range(ROW):
            for c in range(COL):
                if matrix[r][c] == 0:
                    matrix[r][0] = 0
                    matrix[0][c] = 0

        for r in range(1, ROW):
            for c in range(1, COL):
                if matrix[r][0] == 0 or matrix[0][c] == 0:
                    matrix[r][c] = 0

        if row_zero_check:
            for c in range(COL):
                matrix[0][c] = 0

        if col_zero_check:
            for r in range(ROW):
                matrix[r][0] = 0
  • ํŒจํ„ด: Two Pointers, Greedy, Dynamic Programming, Hash Map / Hash Set, Bit Manipulation, Divide and Conquer, Union Find, Trie, BFS, DFS, Backtracking, Heap / Priority Queue, Monotonic Stack, Binary Search, Sliding Window
  • ์„ค๋ช…: ์ด ์ฝ”๋“œ๋Š” ๊ณต๊ฐ„ ์ ˆ์•ฝ์„ ์œ„ํ•ด ํ–‰/์—ด์„ ํ‘œ์‹œ๊ธฐ๋กœ ์žฌํ™œ์šฉํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ 0์˜ ์œ„์น˜๋ฅผ ์ „ํŒŒํ•œ๋‹ค. ํ–‰๊ณผ ์—ด ํ‘œ์‹œ๋ฅผ ํ†ตํ•ด ์ œ์ž๋ฆฌ์—์„œ ์Šค์บ”ํ•˜๋ฉฐ ์กฐ๊ฑด์— ๋งž๋Š” ์›์†Œ๋ฅผ 0์œผ๋กœ ์„ค์ •ํ•˜๋Š” ๊ธฐ๋ฒ•์€ ๋Œ€ํ‘œ์ ์ธ Sliding Window๊ฐ€ ์•„๋‹Œ, ๊ณต๊ฐ„ ํšจ์œจ์  ๋งˆ์ปค ํ™œ์šฉ ํŒจํ„ด์ด๋‹ค.

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

๋ณต์žก๋„
Time O(m * 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.

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

unique-paths/alphaorderly.py
"""
Time Complexity: O(m * n)
Space Complexity: O(m * n)

Dynamic Programming (2D DP approach):
- Use a 2D array where maze[i][j] represents the number of unique paths to cell (i, j).
- Initialize the first row and first column with 1 (since there's only one way to reach each cell: only right moves for the first row or only down moves for the first column).
- For all other cells, maze[i][j] = maze[i-1][j] + maze[i][j-1] (sum of paths from the cell above and the cell to the left).
- Return maze[m-1][n-1] as the answer, which is the total number of unique paths.
"""
class Solution:
    def uniquePaths(self, m: int, n: int) -> int:
        maze = [[1] * n for _ in range(m)]

        for i in range(1, m):
            for j in range(1, n):
                maze[i][j] = maze[i - 1][j] + maze[i][j - 1]

        return maze[m - 1][n - 1]

"""
Time Complexity: O(m * n)
Space Complexity: O(n)

Dynamic Programming (1D DP optimization):
- Use a 1D array dp of size n.
- dp[c] keeps track of the number of unique paths to column c in the current row.
- Initialize dp with 1s (the first row has only one way to reach each column).
- For every row from the second onward, update dp[c] = dp[c] + dp[c - 1] (add ways from the left neighbor to ways accumulated so far).
- Return dp[-1] as the answer, representing the number of unique paths to the bottom-right cell.
"""
class Solution:
    def uniquePaths(self, m: int, n: int) -> int:
        dp = [1] * n

        for _ in range(m - 1):
            for c in range(1, n):
                dp[c] += dp[c - 1]

        return dp[-1]

"""
Time Complexity: O(m + n)
Space Complexity: O(1)

Combinatorial approach:
- The problem reduces to choosing (m-1) moves down from (m+n-2) total movements (or equivalently (n-1) moves right).
- The number of unique paths is given by the formula (m+n-2)! / [(m-1)! * (n-1)!], representing all possible orderings of down and right moves.
- Use the combinatorial (factorial) formula to compute the result efficiently.
"""
class Solution:
    def uniquePaths(self, m: int, n: int) -> int:
        return comb(m + n - 2, n - 1)

"""
Time Complexity: O(m * n)
Space Complexity: O(m * n)

### Top down dynamic programming (with memoization) ###

Approach:
- Use recursion with memoization (via functools.cache) to store the number of unique paths to (row, col).
- The recursive function dp(row, col) returns the number of unique paths from the top-left to (row, col).
- Base case: If row == 1 or col == 1, there's only one unique path.
- Otherwise, dp(row, col) = dp(row-1, col) + dp(row, col-1).
- The answer is dp(m, n), the number of unique paths to the bottom-right cell.
"""
class Solution:
    @cache
    def uniquePaths(self, m: int, n: int) -> int:
        return 1 if (m == 1 or n == 1) else self.uniquePaths(m - 1, n) + self.uniquePaths(m, n - 1)
  • ํŒจํ„ด: Dynamic Programming, Monotonic Stack, Hash Map / Hash Set, Greedy, Divide and Conquer, Two Pointers, Sliding Window, Fast & Slow Pointers, BFS, DFS, Backtracking, Union Find, Trie, Bit Manipulation, Heap / Priority Queue
  • ์„ค๋ช…: ์ฃผ๋กœ 2D/1D DP๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ํŒจํ„ด์ด ์‚ฌ์šฉ๋˜๋ฉฐ, ์ตœ๋‹จ ๊ฒฝ๋กœ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋Œ€ํ‘œ์ ์ธ ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ(DP) ํŒจํ„ด(๋ฉ”๋ชจ์ด์ œ์ด์…˜, ๋ฐ”ํ…€์—…, ๊ณต๊ฐ„ ์ตœ์ ํ™”)๊ณผ ์žฌ๊ท€ + ๋ฉ”๋ชจ์ด์ œ์ด์…˜(ํƒ์ƒ‰ ๊ธฐ๋ฐ˜ DP)์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ํŒจํ„ด์€ ์‚ฌ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

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

๋ณต์žก๋„
Time O(m * n)
Space O(m * n)

ํ”ผ๋“œ๋ฐฑ: ๋‹ค์–‘ํ•œ ์†”๋ฃจ์…˜์ด ์ œ์‹œ๋˜์–ด ์žˆ์–ด ์„ ํƒ์˜ ํญ์ด ๋„“์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ DP ์ ‘๊ทผ์ด ๊ฐ€์žฅ ์ง๊ด€์ ์ž…๋‹ˆ๋‹ค.

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

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

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.

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

set-matrix-zeroes/alphaorderly.py
"""
Time Complexity: O(m * n)
Space Complexity: O(1)

Approach:
- Use the first row and first column as markers to track which rows and columns should be zeroed.
- First, check if the original first row or first column should be zeroed by scanning them separately.
- Then, scan the rest of the matrix. If an element is zero, set its corresponding first row and first column positions to zero.
- Next, iterate through the matrix (excluding the first row and column) and set elements to zero if their corresponding first row or first column are zero.
- Finally, zero the first row and/or first column if initially flagged.
"""
class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
        ROW = len(matrix)
        COL = len(matrix[0])

        row_check = any(matrix[0][c] == 0 for c in range(COL))
        col_check = any(matrix[r][0] == 0 for r in range(ROW))

        for r in range(ROW):
            for c in range(COL):
                if matrix[r][c] == 0:
                    matrix[r][0] = 0
                    matrix[0][c] = 0

        for r in range(1, ROW):
            for c in range(1, COL):
                if matrix[r][0] == 0 or matrix[0][c] == 0:
                    matrix[r][c] = 0

        if row_check:
            for c in range(COL):
                matrix[0][c] = 0

        if col_check:
            for r in range(ROW):
                matrix[r][0] = 0
  • ํŒจํ„ด: Two Pointers, Monotonic Stack, Hash Map / Hash Set, Greedy, Dynamic Programming, Binary Search, BFS, DFS, Backtracking, Divide and Conquer, Union Find, Trie, Bit Manipulation, Heap / Priority Queue
  • ์„ค๋ช…: ์ฃผ์–ด์ง„ ์ฝ”๋“œ๋Š” ์ถ”๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ ์—†์ด ํ–‰๊ณผ ์—ด์˜ ์ •๋ณด๋ฅผ ํ–‰/์—ด์˜ ์ฒซ ํ–‰๊ณผ ์ฒซ ์—ด์˜ ๋งˆ์ปค๋กœ ์žฌ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ 0์œผ๋กœ ๋งŒ๋“œ๋Š” ๋ฌธ์ œ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์ถ”๊ฐ€ ๋ฐฐ์—ด ์—†์ด ์ œ๋กœ ์œ„์น˜๋ฅผ ํ‘œ์‹œํ•˜๊ณ  ์ˆœ์ฐจ์ ์œผ๋กœ ๊ฐ’์„ ๋ฐ”๊พธ๋Š” ๋ฐฉ์‹์€ ๊ณต๊ฐ„ ์ ˆ์•ฝ ํŒจํ„ด์˜ ๋Œ€ํ‘œ์ ์ธ ์˜ˆ๋กœ 'Two Pointers'์‹ ์ ‘๊ทผ๊ณผ ์ธ๋ฑ์Šค ๋งˆ์ปค๋ฅผ ํ™œ์šฉํ•œ ์ œ๋กœ ๋ฐฐ์น˜๋กœ ํ•ด์„ํ•  ์ˆ˜ ์žˆ๋‹ค.

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

๋ณต์žก๋„
Time O(m * 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.

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

unique-paths/alphaorderly.py
"""
Time Complexity: O(m * n)
Space Complexity: O(m * n)

Dynamic Programming (2D DP approach):
- Use a 2D array where maze[i][j] represents the number of unique paths to cell (i, j).
- Initialize the first row and first column with 1 (since there's only one way to reach each cell: only right moves for the first row or only down moves for the first column).
- For all other cells, maze[i][j] = maze[i-1][j] + maze[i][j-1] (sum of paths from the cell above and the cell to the left).
- Return maze[m-1][n-1] as the answer, which is the total number of unique paths.
"""
class Solution:
    def uniquePaths(self, m: int, n: int) -> int:
        maze = [[1] * n for _ in range(m)]

        for i in range(1, m):
            for j in range(1, n):
                maze[i][j] = maze[i - 1][j] + maze[i][j - 1]

        return maze[m - 1][n - 1]

"""
Time Complexity: O(m * n)
Space Complexity: O(n)

Dynamic Programming (1D DP optimization):
- Use a 1D array dp of size n.
- dp[c] keeps track of the number of unique paths to column c in the current row.
- Initialize dp with 1s (the first row has only one way to reach each column).
- For every row from the second onward, update dp[c] = dp[c] + dp[c - 1] (add ways from the left neighbor to ways accumulated so far).
- Return dp[-1] as the answer, representing the number of unique paths to the bottom-right cell.
"""
class Solution:
    def uniquePaths(self, m: int, n: int) -> int:
        dp = [1] * n

        for _ in range(m - 1):
            for c in range(1, n):
                dp[c] += dp[c - 1]

        return dp[-1]

"""
Time Complexity: O(m + n)
Space Complexity: O(1)

Combinatorial approach:
- The problem reduces to choosing (m-1) moves down from (m+n-2) total movements (or equivalently (n-1) moves right).
- The number of unique paths is given by the formula (m+n-2)! / [(m-1)! * (n-1)!], representing all possible orderings of down and right moves.
- Use the combinatorial (factorial) formula to compute the result efficiently.
"""
class Solution:
    def uniquePaths(self, m: int, n: int) -> int:
        return comb(m + n - 2, n - 1)

"""
Time Complexity: O(m * n)
Space Complexity: O(m * n)

### Top down dynamic programming (with memoization) ###

Approach:
- Use recursion with memoization (via functools.cache) to store the number of unique paths to (row, col).
- The recursive function dp(row, col) returns the number of unique paths from the top-left to (row, col).
- Base case: If row == 1 or col == 1, there's only one unique path.
- Otherwise, dp(row, col) = dp(row-1, col) + dp(row, col-1).
- The answer is dp(m, n), the number of unique paths to the bottom-right cell.
"""
class Solution:
    @cache
    def uniquePaths(self, m: int, n: int) -> int:
        if m == 1 or n == 1:
            return 1

        return self.uniquePaths(m - 1, n) + self.uniquePaths(m, n - 1)
  • ํŒจํ„ด: Dynamic Programming, Binary Search
  • ์„ค๋ช…: ์ฃผ๋กœ 2D/1D DP๋กœ ๊ฒฉ์ž ๊ฒฝ๋กœ์˜ ํ•ฉ์„ ๊ตฌํ•˜๊ฑฐ๋‚˜ ์žฌ๊ท€+๋ฉ”๋ชจํ™”๋กœ ํ•ด๋ฅผ ์ฐพ๋Š” ๋ฐฉ์‹์ด ํ•ต์‹ฌ์ด๋ฏ€๋กœ Dynamic Programming์ด ์ฃผ ํŒจํ„ด์ด๊ณ , ์ฃผ์–ด์ง„ ์ฝ”๋“œ๋Š” ์ตœ๋‹จ ๊ฒฝ๋กœ์™€ ์กฐํ•ฉ ์ˆ˜๋ฅผ DP๋กœ ์ •ํ™•ํžˆ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. Binary Search ํŒจํ„ด์€ ๋ณธ ์ฝ”๋“œ์— ํ•ด๋‹นํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

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

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

ํ’€์ด 1: Solution.uniquePaths โ€” Time: O(m * n) / Space: O(m * n)
๋ณต์žก๋„
Time O(m * n)
Space O(m * n)

ํ”ผ๋“œ๋ฐฑ: 2D DP ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•ด ๋ชจ๋“  ๊ฒฝ๋กœ ์ˆ˜๋ฅผ ๋ˆ„์  ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ์ง๊ด€์ ์ด์ง€๋งŒ ๊ณต๊ฐ„ ์‚ฌ์šฉ์ด ํฝ๋‹ˆ๋‹ค.

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

ํ’€์ด 2: Solution.uniquePaths โ€” Time: O(m * n) / Space: O(n)
๋ณต์žก๋„
Time O(m * n)
Space O(n)

ํ”ผ๋“œ๋ฐฑ: ํ–‰ ์ˆœํšŒ ์‹œ ํ˜„์žฌ ์—ด์˜ ๊ฒฝ๋กœ ์ˆ˜๋ฅผ ์™ผ์ชฝ ์ด์›ƒ๊ณผ ํ•ฉ์ณ ๊ฐฑ์‹ ํ•˜๋ฏ€๋กœ ์ถ”๊ฐ€ ๋ฐฐ์—ด ์—†์ด๋„ ๊ฒฐ๊ณผ๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.

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

ํ’€์ด 3: Solution.uniquePaths โ€” Time: O(1) / Space: O(1)
๋ณต์žก๋„
Time O(1)
Space O(1)

ํ”ผ๋“œ๋ฐฑ: ๊ฒฐํ•ฉ ๊ณ„์‚ฐ์œผ๋กœ ํ•ด๋ฅผ ์ง์ ‘ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์ตœ์ ์˜ ์‹œ๊ฐ„/๊ณต๊ฐ„์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค(ํŒฉํ† ๋ฆฌ์–ผ ํ•„์š” ์‹œ ํฐ ์ˆ˜ ์ฃผ์˜).

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

ํ’€์ด 4: Solution.uniquePaths โ€” Time: O(m * n) / Space: O(m * n)
๋ณต์žก๋„
Time O(m * n)
Space O(m * n)

ํ”ผ๋“œ๋ฐฑ: ์žฌ๊ท€ + ์บ์‹œ๋กœ ์ค‘๋ณต ๊ณ„์‚ฐ์„ ํ”ผํ•˜์ง€๋งŒ ์Šคํƒ ๊นŠ์ด ๋ฐ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ์ด ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

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

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

@alphaorderly

Copy link
Copy Markdown
Contributor Author

@parkhojeong
๋‚˜์ค‘์— ๋ฆฌ๋ทฐ ๋ถ€ํƒ๋“œ๋ ค์š”! ์ฝ”๋“œ ์ˆ˜์ •์„ ํ•˜๋‹ˆ๊นŒ ๋‹ค์‹œ ์ทจ์†Œ๊ฐ€ ๋˜์—ˆ๋„ค์š”

@alphaorderly
alphaorderly merged commit 08ab93e into DaleStudy:main Aug 9, 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.

2 participants