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

IN DEPTH CAKE/Supercoder

(25)
[c++/LeetCode-Stack] 1047. Remove All Adjacent Duplicates In String ๋‚œ์ด๋„: EASYํ‚ค์›Œ๋“œ: Stack  ๋ฌธ์ œ ์†Œ๋ฌธ์ž ์˜์–ด ์•ŒํŒŒ๋ฒณ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด s๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ค‘๋ณต ์ œ๊ฑฐ๋Š” ์„œ๋กœ ์ธ์ ‘ํ•˜๊ณ  ๊ฐ™์€ ๊ธ€์ž ๋‘ ๊ฐœ๋ฅผ ์„ ํƒํ•˜๊ณ  ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค.`s` ์—์„œ ๋” ์ด์ƒ ์ค‘๋ณต ์ œ๊ฑฐ๋ฅผ ํ•  ์ˆ˜ ์—†์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜์—ฌ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.๋ชจ๋“  ์ด๋Ÿฌํ•œ ์ค‘๋ณต ์ œ๊ฑฐ๊ฐ€ ์™„๋ฃŒ๋œ ํ›„ ์ตœ์ข… ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋‹ต์ด ์œ ์ผํ•จ์ด ์ฆ๋ช…๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ ์›๋ฌธ: https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/description/ ๋ฌธ์ œ ํ’€์ดclass Solution {public: string removeDuplicates(string s) { vector stack; for(a..
[c++/LeetCode-Stack] 155. MinStack ๋‚œ์ด๋„: Mediumํ‚ค์›Œ๋“œ: Stack  ๋ฌธ์ œ ์ƒ์ˆ˜ ์‹œ๊ฐ„ ๋‚ด์— push, pop, top ๋ฐ ์ตœ์†Œ ์š”์†Œ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ์Šคํƒ์„ ์„ค๊ณ„ํ•˜์‹ญ์‹œ์˜ค.MinStack ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์‹ญ์‹œ์˜ค:- MinStack()๋Š” ์Šคํƒ ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค.- void push(int val)๋Š” ์š”์†Œ val์„ ์Šคํƒ์— ๋„ฃ์Šต๋‹ˆ๋‹ค.- void pop()์€ ์Šคํƒ์˜ ๋งจ ์œ„์˜ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.- int top()์€ ์Šคํƒ์˜ ๋งจ ์œ„ ์š”์†Œ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.- int getMin()์€ ์Šคํƒ์—์„œ ์ตœ์†Œ ์š”์†Œ๋ฅผ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.๊ฐ ํ•จ์ˆ˜์— ๋Œ€ํ•ด O(1) ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง„ ์†”๋ฃจ์…˜์„ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ œ ํ’€์ด - O(1) ์‹œ๊ฐ„ ๋‚ด์— min ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด ์ถ”๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์จ์„œ min ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๊ฒŒ min_stack์„ ์ถ”๊ฐ€๋กœ  ์„ ์–ธํ•œ๋‹ค.- min_stack ๊ฐ’์— ..
[c++/LeetCode-2DArray] 48. Rotate Image ๋‚œ์ด๋„: Mediumํ‚ค์›Œ๋“œ: Array ๋ฌธ์ œ ์ฃผ์–ด์ง„ n x n 2์ฐจ์› ํ–‰๋ ฌ์€ ์ด๋ฏธ์ง€๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ด ์ด๋ฏธ์ง€๋ฅผ 90๋„(์‹œ๊ณ„ ๋ฐฉํ–ฅ)๋กœ ํšŒ์ „ํ•˜์‹ญ์‹œ์˜ค.์ด๋ฏธ์ง€๋ฅผ ์ œ์ž๋ฆฌ์—์„œ (in-place) ํšŒ์ „ํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์ž…๋ ฅ 2์ฐจ์› ํ–‰๋ ฌ์„ ์ง์ ‘ ์ˆ˜์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.๋‹ค๋ฅธ 2์ฐจ์› ํ–‰๋ ฌ์„ ํ• ๋‹นํ•˜์—ฌ ํšŒ์ „ํ•˜์ง€ ๋งˆ์‹ญ์‹œ์˜ค. ๋ฌธ์ œ ์›๋ฌธ : https://leetcode.com/problems/rotate-image/description/  ๋ฌธ์ œ ํ’€์ด: ์ฐธ๊ณ ๋กœ, ์ด๋ฒˆ ํฌ์ŠคํŒ…์œผ๋กœ ์ •๋ฆฌํ•œ ์ฝ”๋“œ๋Š” ์†๋„๊ฐ€ ๋น ๋ฅธ solution์€ ์•„๋‹ˆ๋‹ค. ๋‹ค๋งŒ ๋ฌธ์ œ ํ•ด๊ฒฐ ๊ณผ์ •๊ณผ ๊ฐ™์ด ์ •๋ฆฌํ•ด๋ณด๋ฉด ์ข‹์ง€ ์•Š์„๊นŒ ์‹ถ๋‹ค.์ผ๋‹จ ๋ฌธ์ œ๋ฅผ ์ดํ•ดํ•˜๋Š” ๊ณผ์ •๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด๋ณด๋ฉด, ๋ฌธ์ œ์—์„œ ์ œ์‹œํ•œ ์กฐ๊ฑด์„ ๊ฐ„๋‹จํ•œ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜๋‹ค. ๋‚˜๋Š” 3 x 3 ์˜ˆ์ œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ๋ถ„์„ํ–ˆ๋‹ค..
[c++/LeetCode-Array] 75. Sort Colors ๋‚œ์ด๋„: Mediumํ‚ค์›Œ๋“œ: Array  ๋ฌธ์ œ์ฃผ์–ด์ง„ ๋ฐฐ์—ด nums์—๋Š” ๋นจ๊ฐ„์ƒ‰, ํฐ์ƒ‰ ๋˜๋Š” ํŒŒ๋ž€์ƒ‰์œผ๋กœ ์ƒ‰์น ๋œ n๊ฐœ์˜ ๊ฐ์ฒด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋“ค์„ ๊ทธ ์ž๋ฆฌ์—์„œ ์ •๋ ฌํ•˜์—ฌ ๋™์ผํ•œ ์ƒ‰์ƒ์˜ ๊ฐ์ฒด๊ฐ€ ์„œ๋กœ ์ธ์ ‘ํ•˜๋„๋ก ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ด๋•Œ ์ƒ‰์ƒ์€ ๋นจ๊ฐ„์ƒ‰, ํฐ์ƒ‰ ๋ฐ ํŒŒ๋ž€์ƒ‰ ์ˆœ์„œ๋กœ ๋ฐฐ์—ด๋ฉ๋‹ˆ๋‹ค.๋นจ๊ฐ„์ƒ‰, ํฐ์ƒ‰ ๋ฐ ํŒŒ๋ž€์ƒ‰์„ ๊ฐ๊ฐ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด ์ •์ˆ˜ 0, 1 ๋ฐ 2๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ •๋ ฌ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ œ ์›๋ฌธ: https://leetcode.com/problems/sort-colors/description/  ๋ฌธ์ œ ํ’€์ด ์ƒ‰์ƒ ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ์ง€ ์•Š์œผ๋‹ˆ๊นŒ target ์ƒ‰์ƒ์„ ๋ฐ”๊ฟ”๊ฐ€๋ฉด์„œ sorting์„ ํ•˜์žin_place ๋กœ sorting์„ ํ•ด์•ผํ•˜๋‹ˆ๊นŒ, swapํ•ด์•ผํ•  index๋ฅผ ๊ธฐ์–ตํ•ด๊ฐ€๋ฉด์„œ swa..
[c++/LeetCode-Array] 56. Merge Intervals ๋‚œ์ด๋„: Mediumํ‚ค์›Œ๋“œ: Array  ๋ฌธ์ œ ๋ฌธ์ œ์›๋ฌธ: https://leetcode.com/problems/merge-intervals/description/์ฃผ์–ด์ง„ ๊ฐ„๊ฒฉ ๋ฐฐ์—ด์—์„œ, intervals[i] = [starti, endi]์ธ ๊ฐ„๊ฒฉ๋“ค์ด ์žˆ์„ ๋•Œ, ๊ฒน์น˜๋Š” ๋ชจ๋“  ๊ฐ„๊ฒฉ์„ ๋ณ‘ํ•ฉํ•˜๊ณ , ์ž…๋ ฅ๋œ ๋ชจ๋“  ๊ฐ„๊ฒฉ์„ ํฌ๊ด„ํ•˜๋Š” ๊ฒน์น˜์ง€ ์•Š๋Š” ๊ฐ„๊ฒฉ๋“ค์˜ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜์‹œ์˜ค. ๋ฌธ์ œ ํ’€์ด ๊ฒน์น˜๋Š” ๊ตฌ๊ฐ„๋“ค์„ ํ•ฉ์ณ์ฃผ๊ธฐ์œ„ํ•ด์„œ, intervals ๋ฅผ start time ๊ธฐ์ค€์œผ๋กœ sortingํ•˜๊ณ  merge๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด sort๋Š” O(n log n)์ด ๋ณด์žฅ๋˜๊ณ , sorting ๋œ array์— ๋Œ€ํ•ด์„œ merge๋งŒ ํ•ด์ฃผ๋ฉด ๋˜๋ฏ€๋กœ O(n). ์ตœ์ข…์ ์œผ๋กœ O(n log n) ๋ณต์žก๋„๋กœ ํ•ด๊ฒฐ. #include #include using na..
[c++/LeetCode-Array] 162. Find Peak Element ๋‚œ์ด๋„: Mediumํ‚ค์›Œ๋“œ: Array ๋ฌธ์ œ๋ฌธ์ œ ์›๋ฌธ:  https://leetcode.com/problems/find-peak-element/description/  Peak ์š”์†Œ๋Š” ์ด์›ƒ๋ณด๋‹ค ์—„๊ฒฉํ•˜๊ฒŒ ํฐ ์š”์†Œ๋ฅผ ๋œปํ•œ๋‹ค. 0๋ถ€ํ„ฐ ์ƒ‰์ธ์ด ์ง€์ •๋œ ์ •์ˆ˜ ๋ฐฐ์—ด nums๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, Peak ์š”์†Œ๋ฅผ ์ฐพ๊ณ  ํ•ด๋‹น ์ƒ‰์ธ์„ ๋ฐ˜ํ™˜ํ•˜๋ผ. ๋ฐฐ์—ด์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ Peak์ด ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉด ์–ด๋–ค Peak์˜ ์ƒ‰์ธ์ด๋“  ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.nums[-1] = nums[n] = -∞๋กœ ๊ฐ„์ฃผํ•ด๋„ ๋œ๋‹ค. ์ฆ‰, ๋ฐฐ์—ด ์™ธ๋ถ€์— ์žˆ๋Š” ์ด์›ƒ๋ณด๋‹ค ์š”์†Œ๋Š” ํ•ญ์ƒ ์—„๊ฒฉํ•˜๊ฒŒ ํฌ๋‹ค๊ณ  ๊ฐ„์ฃผ๋œ๋‹ค.O(log n) ์‹œ๊ฐ„ ๋‚ด์— ์‹คํ–‰๋˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค. ๋ฌธ์ œ ํ’€์ด ๋ฌธ์ œ ์ž์ฒด๋งŒ ๋†“๊ณ  ๋ณด๋ฉด Medium ๋‚œ์ด๋„๋Š” ์•„๋‹ˆ๋‹ค. ๊ฐ€์žฅ ์‰ฝ๊ฒŒ ์ƒ๊ฐํ•ด๋ณด๋ฉด O(n) ์•ˆ์— itera..
[์ฝ”๋”ฉ/LeetCode150-(11)] 274. H-Index ๋ณธ ๊ธ€์€ LeetCode Top Interview 150์— ์ •๋ฆฌ๋œ ๋ฌธ์ œ๋ฅผ ์š”์•ฝํ•˜๊ณ  ์ด์— ๋Œ€ํ•œ ๊ฐœ์ธ์ ์ธ ํ’€์ด๋ฅผ ํฌํ•จํ•˜๊ณ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณธ ํฌ์ŠคํŒ… ์‹œ๋ฆฌ์ฆˆ๋Š” 150 list์˜ ์ˆœ์„œ๋ฅผ ๋”ฐ๋ผ์„œ ๊ฒŒ์žฌ๋ฉ๋‹ˆ๋‹ค. ํ’€์ด ์–ธ์–ด๋Š” python3์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‚œ์ด๋„: Medium ํ‚ค์›Œ๋“œ: Array, Sorting ๋ฌธ์ œ ๋ฌธ์ œ ์›๋ฌธ: https://leetcode.com/problems/h-index/description/?envType=study-plan-v2&envId=top-interview-150 ์ •์ˆ˜ ๋ฐฐ์—ด citations ์ด ์ฃผ์–ด์งˆ ๋•Œ H-index๋ฅผ ๋ฐ˜ํ™˜ํ•ด๋ผ ๋ฌธ์ œ ํ’€์ด class Solution: def hIndex(self, citations: List[int]) -> int: citations.sort(reverse..
[์ฝ”๋”ฉ/LeetCode150-(10)] 45. Jump Game II ๋ณธ ๊ธ€์€ LeetCode Top Interview 150์— ์ •๋ฆฌ๋œ ๋ฌธ์ œ๋ฅผ ์š”์•ฝํ•˜๊ณ  ์ด์— ๋Œ€ํ•œ ๊ฐœ์ธ์ ์ธ ํ’€์ด๋ฅผ ํฌํ•จํ•˜๊ณ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณธ ํฌ์ŠคํŒ… ์‹œ๋ฆฌ์ฆˆ๋Š” 150 list์˜ ์ˆœ์„œ๋ฅผ ๋”ฐ๋ผ์„œ ๊ฒŒ์žฌ๋ฉ๋‹ˆ๋‹ค. ํ’€์ด ์–ธ์–ด๋Š” python3์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‚œ์ด๋„: Medium ํ‚ค์›Œ๋“œ: Array, DP ๋ฌธ์ œ ๋ฌธ์ œ ์›๋ฌธ: https://leetcode.com/problems/jump-game-ii/?envType=study-plan-v2&envId=top-interview-150 ์ •์ˆ˜ํ˜• ๋ฐฐ์—ด nums๊ฐ€ ์ฃผ์–ด์กŒ๋‹ค๊ณ  ํ•˜์ž. ๋‹น์‹ ์€ ์ฒ˜์Œ์— ๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ index์— ์œ„์น˜ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฐฐ์—ด์˜ ๊ฐ ์›์†Œ๋Š” ํ•ด๋‹น ์œ„์น˜์—์„œ๋ถ€ํ„ฐ ๋‹น์‹ ์ด ์ตœ๋Œ€๋กœ ์ ํ”„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธธ์ด๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์ด ๋•Œ, ๋งˆ์ง€๋ง‰ index ์— ๋„๋‹ฌํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ jump ์ˆ˜์˜ ์ตœ์†Ÿ๊ฐ’..