Skip to content

[EGON] Week15 Solutions#609

Merged
EgonD3V merged 7 commits intoDaleStudy:mainfrom
EgonD3V:feat/week15
Nov 23, 2024
Merged

[EGON] Week15 Solutions#609
EgonD3V merged 7 commits intoDaleStudy:mainfrom
EgonD3V:feat/week15

Conversation

@EgonD3V
Copy link
Copy Markdown
Contributor

@EgonD3V EgonD3V commented Nov 22, 2024

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

์ฒดํฌ ๋ฆฌ์ŠคํŠธ

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

@EgonD3V EgonD3V added the py label Nov 22, 2024
@EgonD3V EgonD3V requested a review from obzva November 22, 2024 21:15
@EgonD3V EgonD3V self-assigned this Nov 22, 2024
@EgonD3V EgonD3V requested a review from a team as a code owner November 22, 2024 21:15
Copy link
Copy Markdown
Contributor

@obzva obzva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค Egon๋‹˜!! 15์ฃผ๊ฐ€ ๊ธˆ๋ฐฉ ์ง€๋‚˜๊ฐ”๋„ค์š”, ์•ž์œผ๋กœ๋„ ์—ฐ์ด ๋‹ฟ๋Š”๋‹ค๋ฉด ๊ฐ™์ด ๊ณต๋ถ€ํ•  ๊ธฐํšŒ๊ฐ€ ์žˆ์œผ๋ฉด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค ใ…Žใ…Žใ…Ž
๋ช‡๊ฐ€์ง€ ์งˆ๋ฌธ์‚ฌํ•ญ ๋‚จ๊ฒจ๋‘์—ˆ์Šต๋‹ˆ๋‹ค

Time Complexity: O(n ^ 3)
- s์˜ ๊ธธ์ด๋ฅผ n์ด๋ผ ํ•˜๋ฉด, s์˜ ๊ธธ์ด - 1 ๋งŒํผ ์กฐํšŒํ•˜๋Š”๋ฐ O(n - 1)
- ๊ฐ ๋ฌธ์ž๋งˆ๋‹ค sliding_window๋ฅผ 2ํšŒ ํ˜ธ์ถœํ•˜๋Š”๋ฐ, ๊ฐ ํ˜ธ์ถœ๋งˆ๋‹ค ์ตœ๋Œ€ s์˜ ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณตํ•˜๋ฏ€๋กœ, * 2 * O(n), upper bound
- ๋ฐ˜๋ณต ํ›„ s๋ฅผ slicingํ•˜๋Š”๋ฐ ์ตœ๋Œ€ * O(n), upper bound
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.

์ €๋Š” ์ด ๋ถ€๋ถ„์˜ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ์ข€ ์•„๊น๊ฒŒ ๋А๊ปด์กŒ์Šต๋‹ˆ๋‹ค
str slicing์ด ๊ฝค ๋ฌด๊ฑฐ์šด ์—ฐ์‚ฐ์ธ ๊ฒƒ์œผ๋กœ ๋ณด์ด๋Š”๋ฐ, ์‹œ์ž‘๊ณผ ๋ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ sliding_windowํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•˜๋ฉด ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ 3์ฐจ์›์—์„œ 2์ฐจ์›๊นŒ์ง€ ๋‚ฎ์ถœ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š” :)

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.

์ €๋Š” ์ด ๋ถ€๋ถ„์˜ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ์ข€ ์•„๊น๊ฒŒ ๋А๊ปด์กŒ์Šต๋‹ˆ๋‹ค str slicing์ด ๊ฝค ๋ฌด๊ฑฐ์šด ์—ฐ์‚ฐ์ธ ๊ฒƒ์œผ๋กœ ๋ณด์ด๋Š”๋ฐ, ์‹œ์ž‘๊ณผ ๋ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ sliding_windowํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•˜๋ฉด ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ 3์ฐจ์›์—์„œ 2์ฐจ์›๊นŒ์ง€ ๋‚ฎ์ถœ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š” :)

์ด์ „์— ์ด ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋˜ ์ ์ด ์žˆ์–ด์„œ ๊ธฐ์กด ๋ชจ๋ฒ” ๋‹ต์•ˆ์„ ๊ทธ๋Œ€๋กœ ์ œ์ถœํ–ˆ๋Š”๋ฐ, ์‹คํ–‰ ํ™˜๊ฒฝ์— ๋”ฐ๋ฅธ GC ํ˜ธ์ถœ ์ฐจ์ด ๋•Œ๋ฌธ์ธ์ง€ ๋ฌธ์ž์—ด ์Šฌ๋ผ์ด์‹ฑ์„ ๋ฏธ๋ฆฌํ•ด์„œ ๋ฆฌํ„ดํ•˜๋ฉด ํ›จ์”ฌ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ข‹๋”๋ผ๊ณ ์š”. ์‹œ๊ฐ„๋ณต์žก๋„๋Š” ํˆฌ ํฌ์ธํ„ฐ๋กœ ํ’€๋ฉด ๋‚˜์•„์ง€๋Š”๊ฑด ๋งž๊ธดํ•œ๋ฐ, ์• ์ดˆ์— ํŒฐ๋ฆฐ๋“œ๋กฌ ๋ฌธ์ œ ์ž์ฒด์˜ ์ œ์•ฝ์‚ฌํ•ญ์ด 1000์ž ์•„๋ž˜์—ฌ์„œ ๋ณ„ ์ฐจ์ด๊ฐ€ ์—†๋Š” ๊ฑธ ์•Œ๊ณ ์žˆ๊ธด ํ–ˆ์Šต๋‹ˆ๋‹ค.

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.

@obzva manacher ์•Œ๊ณ ๋ฆฌ์ฆ˜๋„ ์ด๋ฒˆ์— ์•Œ๊ฒŒ๋˜์–ด์„œ ์ถ”๊ฐ€ํ•ด๋ดค๋Š”๋ฐ ํ•œ ๋ฒˆ ๋ณด์‹œ๋ฉด ๋„์›€๋˜์‹ค ๊ฒƒ ๊ฐ™์•„ ๋‚จ๊น๋‹ˆ๋‹ค :)

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.

๊ทธ๋ ‡๊ตฐ์š” ใ…Žใ…Žใ…Ž ๊ทธ๋Ÿฐ ์ด์œ ๋ผ๋ฉด ์—๊ณค๋‹˜ ๋ณธ๋ž˜์˜ ํ’€์ด๊ฐ€ ๋” ํ•ฉ๋ฆฌ์ ์ธ ์„ ํƒ ๊ฐ™์Šต๋‹ˆ๋‹ค

Time Complexity: O(n ^ 3)
- s์˜ ๊ธธ์ด๋ฅผ n์ด๋ผ ํ•˜๋ฉด, s์˜ ๊ธธ์ด - 1 ๋งŒํผ ์กฐํšŒํ•˜๋Š”๋ฐ O(n - 1)
- ๊ฐ ๋ฌธ์ž๋งˆ๋‹ค sliding_window๋ฅผ 2ํšŒ ํ˜ธ์ถœํ•˜๋Š”๋ฐ, ๊ฐ ํ˜ธ์ถœ๋งˆ๋‹ค ์ตœ๋Œ€ s์˜ ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณตํ•˜๋ฏ€๋กœ, * 2 * O(n), upper bound
- ๋ฐ˜๋ณต ํ›„ s๋ฅผ slicingํ•˜๋Š”๋ฐ ์ตœ๋Œ€ * O(n), upper bound
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.

์ €๋Š” ์ด ๋ถ€๋ถ„์˜ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ์ข€ ์•„๊น๊ฒŒ ๋А๊ปด์กŒ์Šต๋‹ˆ๋‹ค
str slicing์ด ๊ฝค ๋ฌด๊ฑฐ์šด ์—ฐ์‚ฐ์ธ ๊ฒƒ์œผ๋กœ ๋ณด์ด๋Š”๋ฐ, ์‹œ์ž‘๊ณผ ๋ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ sliding_windowํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•˜๋ฉด ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ 3์ฐจ์›์—์„œ 2์ฐจ์›๊นŒ์ง€ ๋‚ฎ์ถœ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š” :)

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.

๊ทธ๋Ÿผ ๊ณต๊ฐ„๋ณต์žก๋„๋„ O(1)์œผ๋กœ ์ž์—ฐ์Šค๋ ˆ ๊ฐœ์„ ๋  ๊ฒƒ ๊ฐ™์•„์š”


"""
Runtime: 0 ms (Beats 100.00%)
Time Complexity: O(n ^ 2)
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.

๊ธฐ๋ง‰ํžŒ ํ’€์ด๋„ค์š” ใ…‹ใ…‹ใ…‹ ์ž˜ ๋ฐฐ์› ์Šต๋‹ˆ๋‹ค!!

Space Complexity: O(max(n, m))
- stack์˜ ์ตœ๋Œ€ ํฌ๊ธฐ๋Š” root ํŠธ๋ฆฌ๊ฐ€ ํŽธํ–ฅ๋œ ๊ฒฝ์šฐ์ด๋ฉฐ, ์ด๋Š” root ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ์˜ ์ด ๊ฐฏ์ˆ˜์™€ ๊ฐ™์œผ๋ฏ€๋กœ O(n), upper bound
- is_same_tree ํ•จ์ˆ˜์˜ ์žฌ๊ท€ ์Šคํƒ์˜ ์ตœ๋Œ€ ๊นŠ์ด๋Š” subRoot ํŠธ๋ฆฌ๊ฐ€ ํŽธํ–ฅ๋œ ๊ฒฝ์šฐ์ด๋ฉฐ, ์ด๋Š” subRoot ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ์˜ ์ด ๊ฐฏ์ˆ˜์™€ ๊ฐ™์œผ๋ฏ€๋กœ O(m), upper bound
> O(n) + O(m) ~= O(max(n, 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.

์ œ๊ฐ€ ์ขŒ๋ณ€๊นŒ์ง€๋Š” ์ดํ•ดํ–ˆ๋Š”๋ฐ ์šฐ๋ณ€์œผ๋กœ ์ด์–ด์ง€๋Š” ๋…ผ๋ฆฌ๋Š” ์ดํ•ดํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค @lymchgmk ๋‹˜ ใ…œ
ํ˜น์‹œ O(N + M) ~= O(max(N, M)) ์ด ๋˜๋Š” ์ด์œ ์— ๋Œ€ํ•ด ์„ค๋ช… ๋ถ€ํƒ๋“œ๋ ค๋„ ๋ ๊นŒ์š”?
์ œ ์ƒ๊ฐ์—”, ์Šคํƒ์˜ ๊ณต๊ฐ„๋ณต์žก๋„์™€ is_same_tree์˜ ๊ณต๊ฐ„๋ณต์žก๋„๋Š” ๋…๋ฆฝ์ ์ธ ๊ฒƒ ๊ฐ™์•„์„œ O(N + M)์ด๋ผ๊ณ  ๋ด์•ผ ํ•  ๊ฒƒ ๊ฐ™์•„์„œ์š” ๐Ÿ˜ตโ€๐Ÿ’ซ

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.

์ œ๊ฐ€ ์ขŒ๋ณ€๊นŒ์ง€๋Š” ์ดํ•ดํ–ˆ๋Š”๋ฐ ์šฐ๋ณ€์œผ๋กœ ์ด์–ด์ง€๋Š” ๋…ผ๋ฆฌ๋Š” ์ดํ•ดํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค @lymchgmk ๋‹˜ ใ…œ ํ˜น์‹œ O(N + M) ~= O(max(N, M)) ์ด ๋˜๋Š” ์ด์œ ์— ๋Œ€ํ•ด ์„ค๋ช… ๋ถ€ํƒ๋“œ๋ ค๋„ ๋ ๊นŒ์š”? ์ œ ์ƒ๊ฐ์—”, ์Šคํƒ์˜ ๊ณต๊ฐ„๋ณต์žก๋„์™€ is_same_tree์˜ ๊ณต๊ฐ„๋ณต์žก๋„๋Š” ๋…๋ฆฝ์ ์ธ ๊ฒƒ ๊ฐ™์•„์„œ O(N + M)์ด๋ผ๊ณ  ๋ด์•ผ ํ•  ๊ฒƒ ๊ฐ™์•„์„œ์š” ๐Ÿ˜ตโ€๐Ÿ’ซ

๋ง์”€ํ•˜์‹  ๊ฒƒ ์ฒ˜๋Ÿผ ๋‘˜์ด ๋…๋ฆฝ์ธ๊ฒŒ ๋งž์Šต๋‹ˆ๋‹ค. O(n + m)์œผ๋กœ ์ˆ˜์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

@EgonD3V EgonD3V merged commit 72e93b7 into DaleStudy:main Nov 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

No open projects

Development

Successfully merging this pull request may close these issues.

2 participants