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

์•Œ๊ณ ๋ฆฌ์ฆ˜26

[์ž๋ฐ”] 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.
[์ž๋ฐ”] Leetcode 234 - Palindrome Linked List ์ตœ์ ํ™”๋œ ๋ฐฉ๋ฒ•์€ ์•„๋‹ˆ๋‹ค. ์ผ๋‹จ Linked List๋ฅผ ๋๊นŒ์ง€ ํƒ€๋ฉด์„œ ArrayList list์— ๋‹ด์•„์ฃผ๊ณ , ๊ทธ list์˜ ์–‘ ๋๊ฐ’๋“ค์„ ํ•˜๋‚˜์”ฉ ๋น„๊ตํ•ด๊ฐ€๋ฉด์„œ ์–ด๊ธ‹๋‚˜๋Š” ๊ฒŒ ์žˆ์œผ๋ฉด ๋ฐ”๋กœ answer = false๋ฅผ ๋„ฃ๊ณ  ๋‚˜์™€๋ฒ„๋ฆฐ๋‹ค. Linked List๋ฅผ ์ˆœํšŒํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„ O(n) + ArrayList์˜ ์ ˆ๋ฐ˜์„ ์ˆœํšŒํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„ O(n/2)ํ•ด์„œ ๊ฒฐ๊ตญ ๋Œ€๋žต O(n)์ด๊ธฐ๋Š” ํ•œ๋ฐ, ์„ฑ๋Šฅ์ด ํ•˜์œ„ 70%์— ์†ํ•œ๋‹ค ใ…Ž ์‹œ๊ฐ„์ด ๋น ๋ฅธ ๋‹ค๋ฅธ ํ’€์ด๋ฅผ ๋ณด๋ฉด ๋‘ ๊ฐœ์˜ ๋ณ€์ˆ˜ (fast, slow)๋ฅผ ์จ์„œ ๋ญ”๊ฐ€๋ฅผ ํ•˜๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ, ์ดํ•ด๋Š” ๋‚˜์ค‘์— ํ•ด์•ผ๊ฒ ๋‹ค! ์ฒ˜์Œ ์ œ์ถœํ–ˆ์„ ๋•Œ, [-129, -129] ์ผ€์ด์Šค์—์„œ Wrong Answer๊ฐ€ ๋‚˜์™”๋‹ค. ์ผ๋‹จ ์ž˜๋ชป ์ƒ๊ฐํ–ˆ๋˜ ์ ์€, ์ž๋ฐ”๋ฅผ ๋„ˆ๋ฌด ๊ฒ‰ํ•ฅ๊ธฐ ์‹์œผ๋กœ ํ–ˆ์–ด์„œ ๊ทธ๋Ÿฐ์ง€ Integer๋„ .. 2021. 3. 1.
[์ž๋ฐ”] Leetcode 100 - Same Tree ์–ด์ฉŒ๋‹ค ๋ฉด์ ‘์ด ์žกํ˜€์„œ ๋ฐœ๋“ฑ์— ๋ถˆ๋–จ์–ด์ง„ ๋Š๋‚Œ์œผ๋กœ ๋ถ€๋žด๋ถ€๋žด ๋ฌธ์ œ ํ‘ธ๋Š”์ค‘ ใ…Ž ์นœ๊ตฌํ•œํ…Œ ๋ฆฌํŠธ์ฝ”๋“œ ์ถ”์ฒœ ๋ฐ›์•„์„œ Difficulty Easy๋‹จ๊ณ„๋ถ€ํ„ฐ ํ’€๊ณ  ์žˆ๋Š”๋ฐ, ์–ด๋ ต์ง€๋Š” ์•Š๋‹ค. ํ•˜์ง€๋งŒ ์ตœ์ ํ™”๋Š” ์ •๋ง ๋์ด ์—†๊ณ , ๋˜‘๋˜‘ํ•œ ์‚ฌ๋žŒ๋“ค์€ ์ด ์„ธ์ƒ์— ๋งŽ๋‹ค๋Š” ๊ฑธ ๊ณ„์†ํ•ด์„œ ๊นจ๋‹ซ๊ฒŒ ๋œ๋‹ค. TreeNode 2๊ฐœ๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋˜์ ธ์ฃผ๋ฉด ๊ทธ 2๊ฐœ์˜ TreeNode๋“ค์ด ๋™์ผํ•œ์ง€ ์•„๋‹ˆ์ง€๋ฅผ ํŒ๋ณ„ํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์งœ๋Š” ๋ฌธ์ œ๋‹ค. Class TreeNode๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ๊ตฌํ˜„๋˜์–ด ์žˆ๋‹ค๋Š” ๊ฐ€์ •ํ•˜์— ๋ฉ”์†Œ๋“œ ๋กœ์ง๋งŒ ์ฑ„์šฐ๋ฉด ๋œ๋‹ค. public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode() {} TreeNode(int val) { this.val = val; } TreeNode(int va.. 2021. 2. 28.
[์ž๋ฐ”] ๋ฐฑ์ค€ 2042 - ๊ตฌ๊ฐ„ ํ•ฉ ๊ตฌํ•˜๊ธฐ ์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ ๋ฌธ์ œ๋‹ค. ์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ๋Š” ํ•œ ๋ฒˆ๋„ ๊ทธ ์–ด๋””์—์„œ๋„ ๋“ค์–ด๋ณธ ์ ๋„ ์—†์—ˆ๋Š”๋ฐ, ์•„๋ž˜ ๊ธ€ ์ฝ์œผ๋‹ˆ๊นŒ ์–ด๋Š ์ •๋„ ์ดํ•ด๊ฐ€ ๋๋‹ค. www.crocus.co.kr/648 ์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ(Segment Tree) ์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ(Segment Tree)๋Š” ์š”์ฒญํ•˜๋Š” ์ฟผ๋ฆฌ์— ๋Œ€ํ•ด ๋ฐฉ์‹์ด ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ์œผ๋‚˜, ๋ชจ๋“  ์ฟผ๋ฆฌ๋ฅผ ๋‹ค๋ฃฐ ์ˆ˜ ์—†๊ธฐ์— ๊ตฌ๊ฐ„ ํ•ฉ์— ๋Œ€ํ•œ ์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ๋ฅผ ์ •๋ฆฌํ•ด ๋‘์—ˆ์Šต๋‹ˆ๋‹ค. ๋‚ด์šฉ์ด ๊ธธ์ง€๋งŒ ๊ทธ๋งŒํผ ์ž์„ธํžˆ ์„ค www.crocus.co.kr # ์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ - ์‹œ๊ฐ„ ๋ณต์žก๋„ : O(logN) - tree ๋ฐฐ์—ด๊ณผ array ๋ฐฐ์—ด์€ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋‹ค. array ๋ฐฐ์—ด์€ 1๋ถ€ํ„ฐ N๊นŒ์ง€์˜ ๊ฐ’์ด ๋“ค์–ด๊ฐ€ ์žˆ๋Š” ๋ฐฐ์—ด์ด๋ผ๋ฉด, tree ๋ฐฐ์—ด์€ ๊ตฌ๊ฐ„ํ•ฉ๋“ค์ด ๋“ค์–ด๊ฐ€์žˆ๋Š” ๋ฐฐ์—ด์ด๋‹ค. ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ๋ฅผ ๊ฐ ๋ฐฐ์—ด์— ๋Œ€์น˜์‹œํ‚จ ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.. 2021. 1. 6.