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

์ „์ฒด ๊ธ€40

[์ž๋ฐ”] 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.
[์ž๋ฐ”] Leetcode 268 - Missing Number ๋ฌธ์ œ ๋ฐฐ์—ด nums๊ฐ€ ์ฃผ์–ด์ง€๋Š”๋ฐ, ์ด๋•Œ ๋ฐฐ์—ด์—๋Š” ๋ฒ”์œ„ [0,n]์— ์†ํ•˜๋Š” n๊ฐœ์˜ ์ˆซ์ž๊ฐ€ ๋‹ด๊ฒจ์žˆ๋‹ค. ๋ฒ”์œ„๊ฐ€ [0,n]์ด๋ผ๋ฉด ์ด n-1๊ฐœ์˜ ์ˆซ์ž๊ฐ€ ์žˆ์–ด์•ผํ•˜๋Š”๋ฐ, n๊ฐœ๋งŒ ๋‹ด๊ฒจ์žˆ๋‹ค๋Š” ๊ฑด ์ˆซ์ž ํ•œ๊ฐœ๊ฐ€ ๋น ์ ธ์žˆ๋‹ค๋Š” ๋œป. ๋น ์ง„ ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ๋‹ค. leetcode.com/problems/missing-number/ Missing Number - 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 ์ฒซ ๋ฒˆ์งธ Solution ์‰ฝ๊ฒŒ ์ƒ๊ฐํ–ˆ๋‹ค. ๊ธธ์ด n์„ ๊ฐ€์ง€๋Š” boolean ๋ฐฐ์—ด์„ ๋งŒ.. 2021. 3. 4.