Skip to content

Conversation

WhiteHyun
Copy link
Member

@WhiteHyun WhiteHyun commented Jun 28, 2024

211. Design Add and Search Words Data Structure

Complexities ๐Ÿ“ˆ

  • Time Complexity: $O(26^n)$?
  • Space Complexity: $O(n)$

Explanation ๐Ÿ“

Trie๋ฅผ ๊ตฌ์„ฑํ•˜์—ฌ ํ’€์ดํ–ˆ์Šต๋‹ˆ๋‹ค.
Trie๋กœ ๊ฒ€์ƒ‰ํ•˜๋Š” ๊ตฌ๊ฐ„์—์„œ .์ด ๋“ค์–ด์˜ค๋Š” ๊ฒฝ์šฐ ๋ชจ๋“  ๊ฒฝ๋กœ๋ฅผ ํŒŒ์•…ํ•ด์ฃผ์–ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์—, $O(n)$๋ณด๋‹ค ์ข€ ๋” ์‹œ๊ฐ„์ด ๊ฑธ๋ฆด ๊ฒƒ์ž…๋‹ˆ๋‹ค.
์ด ๋•Œ ์‹œ๊ฐ„๋ณต์žก๋„ ๊ณ„์‚ฐ์„ ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ์ง€๋Š” ์ž˜ ๋ชจ๋ฅด๊ฒ ๋„ค์š” ๐Ÿ˜… ํ˜น์‹œ ์˜๊ฒฌ์ด ์žˆ์œผ์‹œ๋‹ค๋ฉด ์ฒจ์–ธ ๋ถ€ํƒ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

200. Number of Islands

Complexities ๐Ÿ“ˆ

  • Time Complexity: $O(n * m)$
    • n: row
    • m: column
  • Space Complexity: $O(n * m)$
    • n: row
    • m: column

Explanation ๐Ÿ“

N * M ๋งŒํผ ์ˆœํšŒํ•˜๋ฉด์„œ "1"์„ ์ฐพ๋Š” ์ฆ‰์‹œ DFS๋กœ ์ธ์ ‘ํ•œ ์„ฌ์˜ ๋„ˆ๋น„๋ฅผ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.

133. Clone Graph

Complexities ๐Ÿ“ˆ

  • Time Complexity: $O(n)$
  • Space Complexity: $O(n)$

Explanation ๐Ÿ“

์ƒˆ๋กœ์šด ๋…ธ๋“œ๋ฅผ ์บ์‹ฑํ•  ๋”•์…”๋„ˆ๋ฆฌ ํƒ€์ž…์„ ๋งŒ๋“ค๊ณ , ๊ธฐ์กด original Node๋ฅผ ๋Œ๋ ค๊ฐ€๋ฉฐ ์ƒˆ๋กœ์šด ๋ณต์‚ฌ๋ณธ์„ ์ƒ์„ฑ ๋ฐ ์ฒ˜๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค.

417. Pacific Atlantic Water Flow

Complexities ๐Ÿ“ˆ

  • Time Complexity: $O(m^2 * n^2)$
  • Space Complexity: $O(m * n)$

Explanation ๐Ÿ“

207. Course Schedule (์•„์ง ํ’€์ด ์•ˆ ํ•จ)

Complexities ๐Ÿ“ˆ

  • Time Complexity: $O(n^2)$
  • Space Complexity: $O(n^2)$

Explanation ๐Ÿ“

์ฒ˜์Œ์—๋Š” ์œ ๋‹ˆ์˜จ ํŒŒ์ธ๋“œ๋กœ ํ’€ ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์œผ๋‚˜ ์ƒํ˜ธ์ฐธ์กฐ์˜ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ๋ฐœ๊ฒฌํ•˜์ง€ ๋ชปํ•˜์—ฌ DFS๋กœ ํ’€์ดํ–ˆ์Šต๋‹ˆ๋‹ค.

@WhiteHyun WhiteHyun self-assigned this Jun 28, 2024
@WhiteHyun WhiteHyun marked this pull request as ready for review July 5, 2024 16:01
@DaleSeo DaleSeo self-requested a review July 7, 2024 01:27
Copy link
Member

@DaleSeo DaleSeo left a comment

Choose a reason for hiding this comment

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

์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค!

Comment on lines +40 to +54
func search(_ word: String) -> Bool {
let word = Array(word)
func dfs(index: Int, node: Node) -> Bool {
guard index < word.count else { return node.endOfWord }

let character = word[index]
if character == "." {
return node.children.values.contains { dfs(index: index + 1, node: $0) }
} else if let nextNode = node.children[character] {
return dfs(index: index + 1, node: nextNode)
}
return false
}
return dfs(index: 0, node: root)
}
Copy link
Member

@DaleSeo DaleSeo Jul 7, 2024

Choose a reason for hiding this comment

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

Trie๋ฅผ ๊ตฌ์„ฑํ•˜์—ฌ ํ’€์ดํ–ˆ์Šต๋‹ˆ๋‹ค.
Trie๋กœ ๊ฒ€์ƒ‰ํ•˜๋Š” ๊ตฌ๊ฐ„์—์„œ .์ด ๋“ค์–ด์˜ค๋Š” ๊ฒฝ์šฐ ๋ชจ๋“  ๊ฒฝ๋กœ๋ฅผ ํŒŒ์•…ํ•ด์ฃผ์–ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋ณด๋‹ค ์ข€ ๋” ์‹œ๊ฐ„์ด ๊ฑธ๋ฆด ๊ฒƒ์ž…๋‹ˆ๋‹ค.
์ด ๋•Œ ์‹œ๊ฐ„๋ณต์žก๋„ ๊ณ„์‚ฐ์„ ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ์ง€๋Š” ์ž˜ ๋ชจ๋ฅด๊ฒ ๋„ค์š” ๐Ÿ˜… ํ˜น์‹œ ์˜๊ฒฌ์ด ์žˆ์œผ์‹œ๋‹ค๋ฉด ์ฒจ์–ธ ๋ถ€ํƒ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

47๋ฒˆ์งธ ์ค„์˜ dfs() ํ•จ์ˆ˜ ์žฌ๊ท€ ํ˜ธ์ถœ์ด ์ตœ์•…์˜ ๊ฒฝ์šฐ 26๋ฒˆ ์ผ์–ด๋‚  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ๋ฌธ์ œ์—์„œ ๋‹จ์–ด์—๋Š” ์˜์–ด ์†Œ๋ฌธ์ž๋งŒ ๋“ค์–ด์žˆ์„ ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ๊ฐ ๊ธ€์ž์—์„œ ๋‹ค์Œ ๊ธ€์ž๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ์•„๋ฌด๋ฆฌ ๋งŽ์•„๋„ 26๊ฐœ๋Š” ๋„˜์„ ์ˆ˜ ์—†๋Š” ๊ฒƒ์ด์ง€์š”. ๊ทธ๋ฆฌ๊ณ  ํ˜ธ์ถœ ์Šคํƒ์€ ๊ฒ€์ƒ‰ํ•˜๋ ค๋Š” ๋‹จ์–ด์˜ ๊ธธ์ด์™€ ๋น„๋ก€ํ•ด์„œ ๊นŠ์ด์งˆ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ๋‹จ์–ด๊ฐ€ 5๊ฐœ์˜ ๊ธ€์ž๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค๋ฉด, ์ตœ์•…์˜ ๊ฒฝ์šฐ dfs() ํ•จ์ˆ˜๋ฅผ 26 * 26 * 26 * 26 * 26 = 26^5๋ฒˆ ํ˜ธ์ถœํ•ด์•ผ ํŠธ๋ผ์ด์—์„œ ์ฐพ์•„๋‚ผ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ search() ๋ฉ”์„œ๋“œ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(26^w)๊ฐ€ ๋˜๊ณ , ๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” O(w)๊ฐ€ ๋  ๊ฒƒ ๊ฐ™์€๋ฐ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋Š”์ง€์š”?

Copy link
Member Author

Choose a reason for hiding this comment

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

์ €๋„ ๋™์ผํ•˜๊ฒŒ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. Big-O ํ‘œ๊ธฐ๋ฒ•์—์„œ 26^n๊ณผ ๊ฐ™์ด ํ‘œ๊ธฐํ•˜๋Š” ๊ฒƒ๋„ ๊ดœ์ฐฎ์„๊นŒ ์‹ถ์–ด์„œ ์šฐ์„  n์œผ๋กœ ํ‘œ๊ธฐํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. O(26^w)์™€ ๊ฐ™์ด ํ‘œ๊ธฐํ•ด๋„ ๊ดœ์ฐฎ๋‚˜๋ณด๊ตฐ์š”!

}
return dfs(index: 0, node: root)
}
} No newline at end of file
Copy link
Member

Choose a reason for hiding this comment

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

ํŒŒ์ผ ๋ฏธ์ง€๋ง‰์— ์ค„ ๋ฐ”๊ฟˆ์ด ๋ˆ„๋ฝ์ด ๋˜์—ˆ๋„ค์š”. ์ฝ”๋“œ ์ž‘์„ฑํ•˜์‹ค ๋•Œ ์ด๋Ÿฐ ์‚ฌ์†Œํ•œ ๋ถ€๋ถ„์„ ์‹ ๊ฒฝ ์•ˆ ์“ฐ์‹œ์ง€ ์•Š๋„๋ก ํฌ๋ฉงํ„ฐ ์‚ฌ์šฉ์„ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

Copy link
Member Author

Choose a reason for hiding this comment

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

Swift ํฌ๋งทํ„ฐ๊ฐ€ ์—†์–ด์„œ ์ด๊ฒŒ ์ฐธ ์–ด๋ ต๋„ค์š” ๐Ÿ˜… ์ฃผ์˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

@WhiteHyun WhiteHyun merged commit 4bcdf88 into DaleStudy:main Jul 9, 2024
@WhiteHyun WhiteHyun deleted the whitehyun/week9 branch July 9, 2024 09:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

2 participants