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

๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ

(126)
[์ฝ”๋”ฉ/LeetCode150-(8)] Best Time to Buy and Sell Stock II (122) ๋ณธ ๊ธ€์€ LeetCode Top Interview 150์— ์ •๋ฆฌ๋œ ๋ฌธ์ œ๋ฅผ ์š”์•ฝํ•˜๊ณ  ์ด์— ๋Œ€ํ•œ ๊ฐœ์ธ์ ์ธ ํ’€์ด๋ฅผ ํฌํ•จํ•˜๊ณ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณธ ํฌ์ŠคํŒ… ์‹œ๋ฆฌ์ฆˆ๋Š” 150 list์˜ ์ˆœ์„œ๋ฅผ ๋”ฐ๋ผ์„œ ๊ฒŒ์žฌ๋ฉ๋‹ˆ๋‹ค. ํ’€์ด ์–ธ์–ด๋Š” python์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‚œ์ด๋„: Medium ํ‚ค์›Œ๋“œ: Array, Greedy ๋ฌธ์ œ ๋ฌธ์ œ ์›๋ฌธ: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/?envType=study-plan-v2&envId=top-interview-150 prices๋ผ๋Š” ๋ฐฐ์—ด์ด ์ฃผ์–ด์ง„๋‹ค๊ณ ํ•˜์ž. ์ด ๋ฐฐ์—ด์˜ i๋ฒˆ์งธ ์›์†Œ๋“ค์€ ์ฃผ์‹์˜ ๊ฐ€๊ฒฉ์„ ์˜๋ฏธํ•œ๋‹ค. ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์˜ ์ฃผ์‹๋งŒ ๊ฐ€์ง€๊ณ  ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. ๋‹จ, ์ฃผ์‹์„ ํŒ”์ž๋งˆ์ž ๋‹น์ผ์— ๋ฐ”๋กœ ์‚ด ์ˆ˜ ์žˆ๋‹ค. ..
๋ฐฑ์ˆ˜ ๊ธฐ๋ก ep. 2 ๋ฐฑ์ˆ˜ ๊ธฐ๋ก ep. 2 ์‚ฌ๋ž‘ํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์„ ์œ„ํ•œ ๋‚˜์˜ ์‚ฌ๋ถ€์ž‘ ์ด๋ฒˆ ์ฃผ ํ–‰์  ๋˜๋Œ์•„๋ณด๊ธฐ ๊ด‘๋ณต์ ˆ, ๋‚˜์˜ ์ •๋ฆฌ๋Š” ๊ณ„์†๋œ๋‹ค ๐Ÿงน ์ง‘์— ์˜จ ์ดํ›„๋กœ ๋‚˜๋Š” ํ•œ๋™์•ˆ ์ •๋ฆฌ ์š”์ •์œผ๋กœ ์‚ฌ๋Š” ์ค‘์ด๋‹ค. ๊ทผ๋ฐ ๋†€๋ž๊ฒŒ๋„ ์ง‘ ๊ณณ๊ณณ์„ ์ •๋ฆฌํ•˜๋Š” ๊ฒŒ ๊ทธ๋ ‡๊ฒŒ ๊ธฐ์จ์ผ์ˆ˜๊ฐ€ ์—†๋‹ค. ์ฒ˜์Œ์—๋Š” '๋‚ด ์ง๋งŒ ์ •๋ฆฌํ•˜์ง€'๋กœ ์‹œ์ž‘ํ–ˆ๋Š”๋ฐ, ์ผ์ฃผ์ผ์— ๋‘ ๋ฒˆ ์ •๋„๋Š” ๋ช‡ ๊ฐœ์˜ ๊ตฌ์—ญ์„ ์ •ํ•ด์„œ ์ •๋ฆฌํ•˜๊ณ  ์žˆ๋‹ค. ์ง€๋‚œ์ฃผ๋งŒ ํ•ด๋„ ์„œ์žฌ๋ž‘ ํŒฌํŠธ๋ฆฌ, ์‹ ๋ฐœ์žฅ ๋“ฑ์„ ์ •๋ฆฌํ–ˆ๋‹ค. ๋ญ ์ด๋ฆฌ ์ •๋ฆฌํ• ๊ฒŒ ๋งŽ์œผ๋ƒ ํ•˜๊ฒ ์ง€๋งŒ, (์›๋ž˜ ์ง‘๋„ ๋Š˜ ๊นจ๋—ํ–ˆ์—ˆ๋‹ค) ์–ด๋จธ๋‹ˆ์˜ ํ‘œํ˜„์„ ๋นŒ๋ฆฌ์ž๋ฉด ๊ฐ™์€ ๊ณต๊ฐ„์ด๋ผ๋„ ๋‚ด ์†์ด ๋‹ฟ์œผ๋ฉด ๋ญ”๊ฐ€ ๋‹ค๋ฅด๋‹ค๊ณ ... ์‚ฌ์‹ค ๋‚ด๊ฐ€ ๋Š๋ผ๊ธฐ์—๋„ ๊ทธ๋ ‡๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๊ณผ์ •์ด ๋‚˜ํ•œํ…Œ๋Š” ๊ฝค๋‚˜ ํž๋ง์ด๋‹ค. ๋ชธ์€ ์ข€ ๊ณ ๋˜๋„ ๊ณต๊ฐ„์˜ ์ฃผ์ธ๋“ค์ด ๋‚ด๊ฐ€ ์‚ฌ๋ž‘ํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์ด๊ณ  ๋‚ด๊ฐ€ ์‚ฌ๋ž‘ํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์ด ๊ธฐ๋ปํ•˜๊ณ  ์ข‹์•„ํ•ด ์ฃผ๋‹ˆ..
[์ฝ”๋”ฉ/LeetCode150-(7)] Best Time to Buy and Sell Stock (121) ๋ณธ ๊ธ€์€ LeetCode Top Interview 150์— ์ •๋ฆฌ๋œ ๋ฌธ์ œ๋ฅผ ์š”์•ฝํ•˜๊ณ  ์ด์— ๋Œ€ํ•œ ๊ฐœ์ธ์ ์ธ ํ’€์ด๋ฅผ ํฌํ•จํ•˜๊ณ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณธ ํฌ์ŠคํŒ… ์‹œ๋ฆฌ์ฆˆ๋Š” 150 list์˜ ์ˆœ์„œ๋ฅผ ๋”ฐ๋ผ์„œ ๊ฒŒ์žฌ๋ฉ๋‹ˆ๋‹ค. ํ’€์ด ์–ธ์–ด๋Š” python์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‚œ์ด๋„: EASY ํ‚ค์›Œ๋“œ: Array ๋ฌธ์ œ ๋ฌธ์ œ ์›๋ฌธ: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/?envType=study-plan-v2&envId=top-interview-150 prices๋ผ๋Š” ๋ฐฐ์—ด์ด ์ฃผ์–ด์ง„๋‹ค๊ณ ํ•˜์ž. ์ด ๋ฐฐ์—ด์˜ i๋ฒˆ์งธ ์›์†Œ๋“ค์€ ์ฃผ์‹์˜ ๊ฐ€๊ฒฉ์„ ์˜๋ฏธํ•œ๋‹ค. ์ด ๋•Œ ์—ฌ๋Ÿฌ๋ถ„์€ ์ฃผ์‹์„ ์‚ฌ๋Š” ๋‚ ๊ณผ ํŒŒ๋Š” ๋‚ ์„ ์ •ํ•˜๋ ค๊ณ ํ•œ๋‹ค. ๊ฐ€๋Šฅํ•œ ๊ฑฐ๋ž˜ ๊ฒฝ์šฐ์˜ ์ˆ˜ ์ค‘ ์ตœ๋Œ€ ์ด์œค ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋ผ. (๋„ˆ๋ฌด ๋‹น์—ฐํ•˜๊ฒŒ๋„ ๋งค๋„๋Š” ๋งค..
[์ฝ”๋”ฉ/LeetCode150-(6)] Rotate Array (189) ๋ณธ ๊ธ€์€ LeetCode Top Interview 150์— ์ •๋ฆฌ๋œ ๋ฌธ์ œ๋ฅผ ์š”์•ฝํ•˜๊ณ  ์ด์— ๋Œ€ํ•œ ๊ฐœ์ธ์ ์ธ ํ’€์ด๋ฅผ ํฌํ•จํ•˜๊ณ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณธ ํฌ์ŠคํŒ… ์‹œ๋ฆฌ์ฆˆ๋Š” 150 list์˜ ์ˆœ์„œ๋ฅผ ๋”ฐ๋ผ์„œ ๊ฒŒ์žฌ๋ฉ๋‹ˆ๋‹ค. ํ’€์ด ์–ธ์–ด๋Š” python3์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‚œ์ด๋„: Medium ํ‚ค์›Œ๋“œ: Array ๐Ÿ”’ ๋ฌธ์ œ ๋ฌธ์ œ ์›๋ฌธ: https://leetcode.com/problems/rotate-array/description/?envType=study-plan-v2&envId=top-interview-150 ์ •์ˆ˜ ๋ฐฐ์—ด nums๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ ์ฃผ์–ด์ง„ ์Œ์ด ์•„๋‹Œ ์ •์ˆ˜ k์— ๋Œ€ํ•ด์„œ k ๋ฒˆ๋งŒํผ ๋ฐฐ์—ด์„ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํšŒ์ „์‹œ์ผœ๋ผ ๐Ÿงช ์˜ˆ์ œ Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4] Expla..
์ด์ผ€์•„ ๋™๋ถ€์‚ฐ์  ๋ฐฉ๋ฌธ๊ธฐ (์†ก์ • ๋ฌธํ† ์ŠคํŠธ ์ฐ๊ณ  ์ด์ผ€์•„ ๊ฐ„ ๋‚ , feat. ์•Œ๋ผํ”ผ์—˜, ๋ฐœํŠธ์‚ฌ๋ฅด ์‹ํƒ์˜์ž) ์•ˆ๋…•ํ•˜์„ธ์š”. ์˜ค๋Š˜์€ ์ง€๋‚œ์ฃผ, ํƒœํ’ ์นด๋ˆˆ์ด ์˜จ ๋‹น์ผ ํƒœํ’์ด ์ง€๋‚˜๊ฐ€๊ณ  ๋‚˜์„œ ๊ฐ‘์ž๊ธฐ ์ด์ผ€์•„๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ฒŒ ๋๋˜ ์ด์•ผ๊ธฐ๋ฅผ ์จ๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์—์„œ๋„ ์‚ด์ง ์–ธ๊ธ‰ํ–ˆ์ง€๋งŒ, ์‹ํƒ ์˜์ž ๊ฐ€์ฃฝ์ด ๋ฒ—๊ฒจ์ง€๊ธฐ์‹œ์ž‘ํ•˜๋Š”๊ฑธ ๋ฐœ๊ฒฌํ•ด์„œ ๊ต‰์žฅํžˆ ๊ฐ‘์ž‘์Šค๋Ÿฝ๊ฒŒ [์ด์ผ€์•„ ๋™๋ถ€์‚ฐ์ ]์„ ๋ฐฉ๋ฌธํ•˜๊ฒŒ ๋์Šต๋‹ˆ๋‹ค. ์ด ๋‚  ํƒœํ’ ์นด๋ˆˆ์ด ์•„์นจ์— ๋ถ€์‚ฐ์„ ์ง€๋‚˜๊ฐ€์„œ ์›๋ž˜ ๊ณ„ํšํ–ˆ๋˜ ์ผ์ •์ด ์ทจ์†Œ๋˜๊ณ  ์˜คํ›„์— ์–ด๋–ป๊ฒŒ ๋ณด๋‚ผ์ง€๋ฅผ ๊ณ ๋ฏผํ•˜๊ณ  ์žˆ์—ˆ๋Š”๋ฐ ์˜คํ›„ 4์‹œ๊ฐ€ ์ง€๋‚˜๊ฐ€๋‹ˆ ๋ฐ”๋žŒ์ด ์žฆ์•„๋“ค๋”๋ผ๊ณ ์š”. ํ‰์ผ ์šด์˜์‹œ๊ฐ„์„ ์ฐพ์•„๋ณด๋‹ˆ 9์‹œ๊นŒ์ง€ ๊ธธ๋ž˜ ๋ถ€๋žด๋ถ€๋žด ์ง‘์„ ๋‚˜์„œ๋ดค์Šต๋‹ˆ๋‹ค. (์ฐธ๊ณ ๋กœ ์›๋ž˜๋Š” 8์‹œ๊นŒ์ง€๋กœ ์•Œ๊ณ ์žˆ์–ด์š”. ๊ด‘๋ณต์ ˆ ๊ธฐ๊ฐ„๊นŒ์ง€ 9์‹œ๊นŒ์ง€๋กœ ์—ฐ์žฅ ์˜์—…ํ–ˆ๋˜ ๊ฒƒ ๊ฐ™์•„์š”) ์—ฌ๋‹ด์œผ๋กœ ๋ฌธํ† ์ŠคํŠธ ์ด์•ผ๊ธฐ๊ฐ€ ๊ปด์žˆ์œผ๋‹ˆ, ์ด์ผ€์•„ ๋ฐฉ๋ฌธ๊ธฐ๋Š” ๋ฐ”๋กœ ๋ฐ‘์œผ๋กœ ๋‚ด๋ ค๊ฐ€์ฃผ์„ธ์šฉ ๐ŸŒ• MOON ๋ฌธํ† ์ŠคํŠธ (@ ์†ก์ •) ๊ฐ€๋Š” ๊ธธ์— ์‚ด์ง ์ถœ์ถœํ•ด..
[์ฝ”๋”ฉ/LeetCode150-(5)] Majority Element (169) ๋ณธ ๊ธ€์€ LeetCode Top Interview 150์— ์ •๋ฆฌ๋œ ๋ฌธ์ œ๋ฅผ ์š”์•ฝํ•˜๊ณ  ์ด์— ๋Œ€ํ•œ ๊ฐœ์ธ์ ์ธ ํ’€์ด๋ฅผ ํฌํ•จํ•˜๊ณ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณธ ํฌ์ŠคํŒ… ์‹œ๋ฆฌ์ฆˆ๋Š” 150 list์˜ ์ˆœ์„œ๋ฅผ ๋”ฐ๋ผ์„œ ๊ฒŒ์žฌ๋ฉ๋‹ˆ๋‹ค. ํ’€์ด ์–ธ์–ด๋Š” python์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‚œ์ด๋„: EASY ํ‚ค์›Œ๋“œ: Array, Hash Table ๋ฌธ์ œ ๋ฌธ์ œ ์›๋ฌธ: https://leetcode.com/problems/majority-element/description/ ํฌ๊ธฐ๊ฐ€ n์ธ ๋ฐฐ์—ด nums๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ตœ๋นˆ๋„ (the majority) ์›์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•ด๋ผ. ์ตœ๋นˆ (majority) ์›์†Œ๋Š” ํ•ญ์ƒ ์กด์žฌํ•˜๋ฉฐ, ์ด ์›์†Œ์˜ ์ˆ˜๋Š” ๋ฐฐ์—ด ๊ธธ์ด์˜ ์ ˆ๋ฐ˜์„ ๋„˜๋Š”๋‹ค. ์˜ˆ์ œ Input: nums = [3,2,3] Output: 3 Input: nums = [2,2,1,1,1,2..
[์ฝ”๋”ฉ/LeetCode150-(4)] Remove Duplicates from Sorted Array II(80) ๋ณธ ๊ธ€์€ LeetCode Top Interview 150์— ์ •๋ฆฌ๋œ ๋ฌธ์ œ๋ฅผ ์š”์•ฝํ•˜๊ณ  ์ด์— ๋Œ€ํ•œ ๊ฐœ์ธ์ ์ธ ํ’€์ด๋ฅผ ํฌํ•จํ•˜๊ณ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณธ ํฌ์ŠคํŒ… ์‹œ๋ฆฌ์ฆˆ๋Š” 150 list์˜ ์ˆœ์„œ๋ฅผ ๋”ฐ๋ผ์„œ ๊ฒŒ์žฌ๋ฉ๋‹ˆ๋‹ค. ํ’€์ด ์–ธ์–ด๋Š” python์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‚œ์ด๋„: MEDIUM ํ‚ค์›Œ๋“œ: Array, Two-Pointers ๋ฌธ์ œ ๋ฌธ์ œ์›๋ฌธ: https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/description/?envType=study-plan-v2&envId=top-interview-150 ์ฃผ์–ด์ง„ ๊ฐ์†Œํ•˜์ง€ ์•Š๋Š” ์ˆœ์„œ๋กœ ์ •๋ ฌ๋œ ์ •์ˆ˜ ๋ฐฐ์—ด nums์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋™์ผํ•œ ๊ฐ’์ด ์ตœ๋Œ€ ๋‘๋ฒˆ๋งŒ ๋ฐ˜๋ณต๋˜๋„๋ก in-placeํ˜•ํƒœ๋กœ nums๋ฅผ ์—…๋ฐ์ดํŠธํ•ด๋ผ. (๋‹ค์‹œ ๋งํ•ด, 3..
[์ฝ”๋”ฉ/LeetCode150-(3)] Remove Duplicates from Sorted Array (26) ๋ณธ ๊ธ€์€ LeetCode Top Interview 150์— ์ •๋ฆฌ๋œ ๋ฌธ์ œ๋ฅผ ์š”์•ฝํ•˜๊ณ  ์ด์— ๋Œ€ํ•œ ๊ฐœ์ธ์ ์ธ ํ’€์ด๋ฅผ ํฌํ•จํ•˜๊ณ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณธ ํฌ์ŠคํŒ… ์‹œ๋ฆฌ์ฆˆ๋Š” 150 list์˜ ์ˆœ์„œ๋ฅผ ๋”ฐ๋ผ์„œ ๊ฒŒ์žฌ๋ฉ๋‹ˆ๋‹ค. ํ’€์ด ์–ธ์–ด๋Š” python์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‚œ์ด๋„: EASY ํ‚ค์›Œ๋“œ: Array, Two-Pointers ๋ฌธ์ œ ๋ฌธ์ œ์›๋ฌธ: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?envType=study-plan-v2&envId=top-interview-150 ์ฃผ์–ด์ง„ ๊ฐ์†Œํ•˜์ง€ ์•Š๋Š” ์ˆœ์„œ๋กœ ์ •๋ ฌ๋œ ์ •์ˆ˜ ๋ฐฐ์—ด nums์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์˜ค์ง uniqueํ•œ ๊ฐ’๋งŒ ์žˆ๋„๋ก in-placeํ˜•ํƒœ๋กœ nums๋ฅผ ์—…๋ฐ์ดํŠธํ•ด๋ผ. (๋‹ค์‹œ ๋งํ•ด, ์ค‘๋ณต๋œ ๊ฐ’์„ ์ œ๊ฑฐํ•ด๋ผ) ์ด ๋•Œ ๊ฐ’๋“ค์˜..