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

IN DEPTH CAKE

(40)
[c++/LeetCode-DP] 91. Decode Ways (w/ DP ํ’€์ด ์ „๋žต) ๋‚œ์ด๋„: Mediumํ‚ค์›Œ๋“œ: DP  ๐Ÿ“ ๋ฌธ์ œ ์•ŒํŒŒ๋ฒณ A-Z์˜ ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฉ”์‹œ์ง€๋Š” ๋‹ค์Œ ๋งคํ•‘์„ ์‚ฌ์šฉํ•˜์—ฌ ์ˆซ์ž๋กœ ์ธ์ฝ”๋”ฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:'A' -> "1"'B' -> "2"...'Z' -> "26" ์ธ์ฝ”๋”ฉ๋œ ๋ฉ”์‹œ์ง€๋ฅผ ๋””์ฝ”๋”ฉํ•˜๋ ค๋ฉด, ๋ชจ๋“  ์ˆซ์ž๋ฅผ ๊ทธ๋ฃนํ™”ํ•˜๊ณ  ์œ„์˜ ๋งคํ•‘์„ ๋ฐ˜๋Œ€๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์‹œ ๋ฌธ์ž๋กœ ๋งคํ•‘ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค (์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์ด ์žˆ์„ ์ˆ˜ ์žˆ์Œ). ์˜ˆ๋ฅผ ๋“ค์–ด, "11106"์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋งคํ•‘๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:(1 1 10 6)์„ ๊ทธ๋ฃนํ™”ํ•˜์—ฌ "AAJF"(11 10 6)์„ ๊ทธ๋ฃนํ™”ํ•˜์—ฌ "KJF"(1 11 06)์„ ๊ทธ๋ฃนํ™”ํ•˜๋Š” ๊ฒƒ์€ "06"์„ 'F'๋กœ ๋งคํ•‘ํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ์ž˜๋ชป๋œ ๊ฒƒ์ž„์— ์œ ์˜ํ•˜์„ธ์š”.์ˆซ์ž๋งŒ ํฌํ•จํ•˜๋Š” ๋ฌธ์ž์—ด s๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ด๋ฅผ ๋””์ฝ”๋”ฉํ•˜๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”.ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ๋‹ต์ด 32๋น„ํŠธ ์ •์ˆ˜์— ๋งž๋„..
[c++/LeetCode-DP] 746. Min Cost Climbing Stairs ๋‚œ์ด๋„: Easyํ‚ค์›Œ๋“œ: DP    ๐Ÿ“ ๋ฌธ์ œ์ •์ˆ˜ ๋ฐฐ์—ด cost๊ฐ€ ์ฃผ์–ด์ง€๋ฉฐ, cost[i]๋Š” ๊ณ„๋‹จ์˜ i๋ฒˆ์งธ ๋‹จ๊ณ„์˜ ๋น„์šฉ์ž…๋‹ˆ๋‹ค. ๋น„์šฉ์„ ์ง€๋ถˆํ•˜๋ฉด 1๊ฐœ ๋˜๋Š” 2๊ฐœ์˜ ๋‹จ๊ณ„๋ฅผ ์˜ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.์ธ๋ฑ์Šค 0 ๋˜๋Š” ์ธ๋ฑ์Šค 1์˜ ๋‹จ๊ณ„์—์„œ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.์ตœ์ƒ์œ„ ์ธต์— ๋„๋‹ฌํ•˜๊ธฐ ์œ„ํ•œ ์ตœ์†Œ ๋น„์šฉ์„ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”.  Example 1:Input: cost = [10,15,20]Output: 15Explanation: You will start at index 1.- Pay 15 and climb two steps to reach the top.The total cost is 15. Example 2:Input: cost = [1,100,1,1,1,100,1,1,100,1]Output: 6Explanation: You will..
[c++/LeetCode-DP] 70. Climbing Stairs ๋‚œ์ด๋„: Easyํ‚ค์›Œ๋“œ: DP    ๐Ÿ“ ๋ฌธ์ œ ๊ณ„๋‹จ์„ ์˜ค๋ฅด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ผญ๋Œ€๊ธฐ์— ๋„๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด n๊ฐœ์˜ ๋‹จ๊ณ„๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.๊ฐ ๋‹จ๊ณ„๋งˆ๋‹ค 1๊ฐœ ๋˜๋Š” 2๊ฐœ์˜ ๋‹จ๊ณ„๋ฅผ ์˜ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ผญ๋Œ€๊ธฐ์— ๋„๋‹ฌํ•˜๋Š” ๋ฐ ๋ช‡ ๊ฐ€์ง€ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”์ง€ ๊ณ„์‚ฐํ•˜์„ธ์š”. Example 1:Input: n = 2Output: 2Explanation: There are two ways to climb to the top.1. 1 step + 1 step2. 2 steps Example 2:Input: n = 3Output: 3Explanation: There are three ways to climb to the top.1. 1 step + 1 step + 1 step2. 1 step + 2 steps3. 2 steps + 1 step ๋ฌธ..
[c++/LeetCode-Hash Table] 939. Minimum Area Rectangle ๋‚œ์ด๋„: Mediumํ‚ค์›Œ๋“œ: Hash Table   ๐ŸŽฒ ๋ฌธ์ œX-Y ํ‰๋ฉด์ƒ์˜ ์ ๋“ค์˜ ๋ฐฐ์—ด points๊ฐ€ ์ฃผ์–ด์กŒ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ points[i] = [xi, yi]์ž…๋‹ˆ๋‹ค.์ด๋Ÿฌํ•œ ์ ๋“ค๋กœ ํ˜•์„ฑ๋œ X์ถ•๊ณผ Y์ถ•์— ํ‰ํ–‰ํ•œ ์ง์‚ฌ๊ฐํ˜•์˜ ์ตœ์†Œ ๋ฉด์ ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ทธ๋Ÿฌํ•œ ์ง์‚ฌ๊ฐํ˜•์ด ์—†๋‹ค๋ฉด, 0์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ œ ์›๋ฌธ: https://leetcode.com/problems/minimum-area-rectangle/description/ Example 1:Input: points = [[1,1],[1,3],[3,1],[3,3],[2,2]]Output: 4 ๐ŸŽฒ  ๋ฌธ์ œ ํ’€์ด๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์‚ฌ๊ฐํ˜•์„ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ์‚ฌ๊ฐํ˜•๋“ค ์ค‘ ์ตœ์†Œ ๋ฉด์  ๊ฐ’์„ ๊ตฌํ•˜๊ณ ์žํ•œ๋‹ค.์ด๋ฅผ ์œ„ํ•ด์„œ ๋‘ ๊ฐœ์˜ ์ ์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ์ด ๋‘ ์  ์™ธ์— ์‚ฌ๊ฐํ˜•์„ ๋งŒ๋“ค ์ˆ˜ ..
[c++/LeetCode-Hash Table] 692. Top K Frequent Words ๋‚œ์ด๋„: Mediumํ‚ค์›Œ๋“œ: Hash Table ๐ŸŽฒ ๋ฌธ์ œ ๋ฌธ์ž์—ด ๋ฐฐ์—ด words์™€ ์ •์ˆ˜ k๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, k๊ฐœ์˜ ๊ฐ€์žฅ ๋นˆ๋ฒˆํ•œ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ๋œ ๋‹ต์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋™์ผํ•œ ๋นˆ๋„๋ฅผ ๊ฐ€์ง„ ๋ฌธ์ž์—ด์€ ์‚ฌ์ „์ˆœ์œผ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.  ๐ŸŽฒ ์˜ˆ์‹œExample 1:Input: words = ["i","love","leetcode","i","love","coding"], k = 2Output: ["i","love"]Explanation: "i" and "love" are the two most frequent words.Note that "i" comes before "love" due to a lower alphabetical order. Example 2:Input: words = ["th..
[c++/LeetCode-Hash Table] 387. First Unique Character in a String ๋‚œ์ด๋„: Easyํ‚ค์›Œ๋“œ: Stack  ๋ฌธ์ œ ๋ฌธ์ž์—ด s๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ฒซ ๋ฒˆ์งธ๋กœ ๋ฐ˜๋ณต๋˜์ง€ ์•Š๋Š” ๋ฌธ์ž๋ฅผ ์ฐพ๊ณ  ๊ทธ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ทธ๋Ÿฐ ๋ฌธ์ž๊ฐ€ ์—†๋‹ค๋ฉด, -1์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ž…์ถœ๋ ฅ ์˜ˆ์‹œ Example 1:Input: s = "leetcode"Output: 0 Example 2:Input: s = "loveleetcode"Output: 2  Example 3:Input: s = "aabb"Output: -1 ๋ฌธ์ œ ํ’€์ดclass Solution {public: int firstUniqChar(string s) { unordered_map hash; for(const auto c : s){ const auto ptr = hash.find(c); ..
[c++/LeetCode-Hash Table] 1. Two Sum ๋‚œ์ด๋„: Easyํ‚ค์›Œ๋“œ: Stack  ๋ฌธ์ œ ์ •์ˆ˜ ๋ฐฐ์—ด nums์™€ ์ •์ˆ˜ target์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋‘ ์ˆซ์ž์˜ ํ•ฉ์ด target์ด ๋˜๋„๋กํ•˜๋Š” ์ˆซ์ž์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.๊ฐ ์ž…๋ ฅ์—๋Š” ์ •ํ™•ํžˆ ํ•˜๋‚˜์˜ ํ•ด๊ฒฐ์ฑ…์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋™์ผํ•œ ์š”์†Œ๋ฅผ ๋‘ ๋ฒˆ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.์–ด๋–ค ์ˆœ์„œ๋กœ๋“  ๋‹ต์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ ์›๋ฌธ: https://leetcode.com/problems/two-sum/description/ Example 1:Input: nums = [2,7,11,15], target = 9Output: [0,1]Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. ๋ฌธ์ œ ํ’€์ดO(n) ์‹œ๊ฐ„ ์•ˆ์— ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด์„œ, array์˜ iteratio..
[c++/LeetCode-Stack] 394. Decode String ๋‚œ์ด๋„: Mediumํ‚ค์›Œ๋“œ: Stack๋ฌธ์ œ ์ธ์ฝ”๋”ฉ๋œ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง€๋ฉด ํ•ด๋‹น ๋ฌธ์ž์—ด์„ ๋””์ฝ”๋”ฉํ•œ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ธ์ฝ”๋”ฉ ๊ทœ์น™์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:  k[์ธ์ฝ”๋”ฉ๋œ_๋ฌธ์ž์—ด], ์—ฌ๊ธฐ์„œ ๋Œ€๊ด„ํ˜ธ ๋‚ด์˜ ์ธ์ฝ”๋”ฉ๋œ ๋ฌธ์ž์—ด์ด k๋ฒˆ ์ •ํ™•ํžˆ ๋ฐ˜๋ณต๋ฉ๋‹ˆ๋‹ค. k๋Š” ์–‘์˜ ์ •์ˆ˜์ž„์ด ๋ณด์žฅ๋ฉ๋‹ˆ๋‹ค.์ž…๋ ฅ ๋ฌธ์ž์—ด์ด ํ•ญ์ƒ ์œ ํšจํ•˜๋‹ค๊ณ  ๊ฐ€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ถ”๊ฐ€์ ์ธ ๊ณต๋ฐฑ์ด๋‚˜ ๋Œ€๊ด„ํ˜ธ๊ฐ€ ์ž˜ ํ˜•์„ฑ๋˜์–ด ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์›๋ณธ ๋ฐ์ดํ„ฐ์—๋Š” ์ˆซ์ž๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š์œผ๋ฉฐ ์ˆซ์ž๋Š” ๋ฐ˜๋ณต ํšŸ์ˆ˜ k๋งŒ์„ ์œ„ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, 3a ๋˜๋Š” 2[4]์™€ ๊ฐ™์€ ์ž…๋ ฅ์€ ์—†์Šต๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ์ถœ๋ ฅ์˜ ๊ธธ์ด๊ฐ€ ํ•ญ์ƒ 10^5๋ฅผ ์ดˆ๊ณผํ•˜์ง€ ์•Š๋„๋ก ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ ์˜ˆ์‹œ Example 1:Input: s = "3[a]2[bc]"Output: "aaabcbc" ๋ฌธ์ œ ์›๋ฌธ: ht..