๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

์•Œ๊ณ ๋ฆฌ์ฆ˜/๋ฌธ์ œ26

[์ž๋ฐ”] Leetcode 1302 - Deepest Leaves Sum leetcode.com/problems/deepest-leaves-sum/ Deepest Leaves Sum - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com ๊ฐ€์žฅ ์•„๋ž˜์— ์žˆ๋Š” ๋…ธ๋“œ๋“ค์˜ value๊ฐ’์„ ํ•ฉํ•˜๋Š” ๋ฌธ์ œ๋‹ค. '์ฒ˜์Œ๋ถ€ํ„ฐ ๋„ˆ๋ฌด ๋งŽ์€ ์ƒ๊ฐ์„ ํ•˜์ง€ ๋ง์ž!'๊ณ  ๊ฒฐ์‹ฌํ•˜๊ณ  ์ •๋ง ๋– ์˜ค๋ฅด๋Š” ๋Œ€๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋”๋‹ˆ ํ†ต๊ณผ๋Š” ํ–ˆ๋Š”๋ฐ ์• ์ž”ํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์™”๋‹ค ใ…‹ใ…‹ใ…‹ Solution ๋‚˜๋ฆ„์˜ BFS๋กœ ๊ฐ€์žฅ ์•„๋ž˜์˜ ๋…ธ๋“œ๊ฐ€ ๋ช‡ depth์— ์žˆ๋Š”์ง€ ์ฐพ๊ณ , ๋‹ค์‹œ ํ•œ๋ฒˆ Queue๋ฅผ ๋Œ๋ฆฌ๋ฉด์„œ ํ•ด.. 2021. 4. 11.
[์ž๋ฐ”] Leetcode 1332 - Remove Palindromic Subsequences ์‹ซ์–ด์š”๊ฐ€ ์ข‹์•„์š”๋ณด๋‹ค 2๋ฐฐ ์ด์ƒ ๋งŽ์•„์„œ ๊ธด์žฅํ–ˆ๋Š”๋ฐ, ์ƒ๊ฐ๋ณด๋‹ค ๋‚˜์˜์ง€ ์•Š์•˜๋‹ค. ๋‚˜๋ฆ„ ๋ฐœ์ƒ์˜ ์ „ํ™˜์„ ํ•„์š”๋กœ ํ•ด์„œ ๋‚ด ์•„๋ฌด๋ž˜๋„ ๊ธฐ๋ณธ๋‹ค์ง€๊ธฐ๋ฅผ ์ข‹์•„ํ•˜๋Š” ๋ฆฌํŠธ์ฝ”๋“œ ์œ ์ €๋“คํ•œํ…Œ๋Š” ๋ณ„๋กœ์˜€๋‚˜๋ณด๋‹ค. ์ด ๋ฌธ์ œ๋Š” ์ฃผ์–ด์ง€๋Š” ์•ŒํŒŒ๋ฒณ์ด a์™€ b๋ฟ์ด๋ผ๋Š” ์ ์ด ์—„์ฒญ๋‚œ ํžŒํŠธ๊ฐ€ ๋˜๊ณ , remove๋ฅผ ์‹ค์‹œํ•ด์•ผ ํ•˜๋Š” ํšŸ์ˆ˜๋งŒ ๊ตฌํ•˜๋ฉด ๋œ๋‹ค๋Š” ์ ์ด ํ’€์ด๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค. ์ฃผ์–ด์ง„ ์ œํ•œ ์†์—์„œ ๊ฐ€๋Šฅํ•œ ๋‹ต์€ 3๊ฐ€์ง€ ๋ฟ์ด๋‹ค. 1) s๊ฐ€ null์ผ ๋•Œ → return 0 2) s๊ฐ€ Palindrome์ผ ๋•Œ → return 1 3) s๊ฐ€ Palindrome์ด ์•„๋‹ ๋•Œ → ๊ธ€์ž ํ•˜๋‚˜๋ฅผ ์ œ๊ฑฐํ•ด์„œ ํŒฐ๋ฆฐ๋“œ๋กฌ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— return 2 ์ด์ค‘์—์„œ 1๋ฒˆ๊ณผ 2๋ฒˆ์€ ๋ฐ”๋กœ ์ดํ•ด๊ฐ€ ๋˜๋Š”๋ฐ, ๊ณผ์—ฐ 3๋ฒˆ์ด ์ •๋ง ๋ชจ๋“  ๊ฒฝ์šฐ์— ํ•ด๋‹น๋  ์ง€๊ฐ€ ์‚ฌ์‹ค ์ข€ ์˜๋ฌธ์ด๋‹ค. ์—ฌํƒœ ๋‚ด๊ฐ€ ๋งŒ.. 2021. 3. 9.
[์ž๋ฐ”] ๋ฆฌํŠธ์ฝ”๋“œ 706 - Design HashMap HashMap์€ Map์„ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค์ด๊ธฐ ๋•Œ๋ฌธ์—, ํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค. ํ•ญ์ƒ ์ฃผ์–ด์ง„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉ๋งŒ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—, ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š”์ง€์— ๋Œ€ํ•ด์„œ๋Š” ๊ณ ๋ฏผํ•ด๋ณด์ง€ ์•Š์•˜๋Š”๋ฐ ๋•๋ถ„์— bucket์ด๋ผ๋Š” ๊ฐœ๋…๋„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ์ด๋ฒˆ ๋ฌธ์ œ๋Š” ๊ณต๋ถ€ํ•˜๋Š” ์ฐจ์›์—์„œ ํ‘ผ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์—, ๋ฐ”๋กœ ํ’€์ด๋“ค์„ ์ข€ ์ฐพ์•„ ๋ณด์•˜๋‹ค. ์ฒซ ๋ฒˆ์งธ Solution ์–ด์ฐจํ”ผ key์˜ ๋ฒ”์œ„๊ฐ€ 0๋ถ€ํ„ฐ 1,000,000, ์ฆ‰ ๋ฐฑ๋งŒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ด์„œ ๊ตฌํ˜„ํ•ด๋„ ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค. MyHashMap ํด๋ž˜์Šค ์ƒ์„ฑ ์‹œ์—, ํฌ๊ธฐ 1000001์ธ ๋ฐฐ์—ด์„ ์„ ์–ธํ•œ ๋‹ค์Œ์— index๋ฅผ ํ‚ค๊ฐ’์ฒ˜๋Ÿผ ์ƒ๊ฐํ•ด์„œ ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค. ์‹œ๊ฐ„ ๋ณต์žก๋„ : O(1) (get, remove, put ๋ชจ๋‘์— ํ•ด๋‹น๋œ๋‹ค) ๊ณต๊ฐ„ ๋ณต์žก๋„ : O(n) class MyHashMap { int[] .. 2021. 3. 7.
[์ž๋ฐ”] Leetcode 637 - Average of Levels in Binary Tree leetcode.com/problems/average-of-levels-in-binary-tree/ Average of Levels in Binary Tree - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com ๊ธฐ์ดˆ BFS๋ฌธ์ œ์ธ๋ฐ, BFS๋กœ ํ’€ ์ˆ˜ ์žˆ์Œ์„ ํŒŒ์•…ํ•˜๋Š” ๋ฐ ์˜ค๋ž˜ ๊ฑธ๋ ธ๋‹ค. ์Šฌํ”„๋‹ค. root๋ฅผ Queue queue์— ๋„ฃ์€ ๋‹ค์Œ์—, ํ•˜๋‚˜์”ฉ ๋ฝ‘์œผ๋ฉด์„œ left์™€ right ๋…ธ๋“œ๋ฅผ ๋‹ค์‹œ queue์— ๋„ฃ์–ด์ค€๋‹ค. while๋ฌธ์„ ํƒ€๋ฉด์„œ ํ•œ ๋ ˆ๋ฒจ์˜ ๋…ธ๋“œ๊ฐœ์ˆ˜๊ฐ€ ๋ณ€์ˆ˜ siz.. 2021. 3. 5.