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

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

(126)
[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๋น„ํŠธ ์ •์ˆ˜์— ๋งž๋„..
์‹œ๊ทธ๋‹ˆ์—˜ ์„œ์šธ ๋ฆฌ๋ทฐ feat. ์กฐ์‹, ๋Ÿฐ๋˜ ๋ฒ ์ด๊ธ€, ๋นŒ์ฆˆ ์ž ์‹ค ์•ˆ๋…•ํ•˜์„ธ์š”. ์˜ค๋Š˜์€ ์‹œ๊ทธ๋‹ˆ์—˜ ๋ฐฉ๋ฌธ ํ›„๊ธฐ๋ฅผ ๋‚จ๊ฒจ๋ณผ๊นŒ ํ•ฉ๋‹ˆ๋‹ค. 4์›” ๋ง๊ฒฝ ์ฃผ์ค‘์— ๋ฐฉ๋ฌธํ–ˆ์–ด์š”. ํŠน๋ณ„ํ•œ ์ด๋ฒคํŠธ๊ฐ€ ์žˆ์–ด ๊ฐ”๋˜ ๊ฑด ์•„๋‹ˆ๊ณ  ์ฃผ์ค‘์˜ ์—ฌ์œ ๋กœ์›€์„ ๋ˆ„๋ ค๋ณด์ž ์‹ถ์–ด์„œ ํ˜ธ์บ‰์Šค ๊ฒธ ๋ฐฉ๋ฌธํ–ˆ์Šต๋‹ˆ๋‹ค.  (์ฐธ๊ณ ๋กœ,  ์š•์‹ฌ๋‚ด๋‹ค๋ณด๋‹ˆ ์‚ฌ์ง„์ด ๋„ˆ๋ฌด ๋งŽ์•„์š”. ์ฐธ๊ณ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค)๊ธ€์ด ๊ธด ํŽธ์ด๋‹ˆ ์›ํ•˜์‹œ๋Š” ์ •๋ณด๋Š” ์˜†์˜ Contents์—์„œ ๋›ฐ์–ด๋„˜๊ธฐํ•˜์…”์„œ ๋ณด์„ธ์š” :)    ๐Ÿ“ ํˆฌ์ˆ™ ์ •๋ณด ์ฃผ์ค‘ 1๋ฐ• 2์ผํˆฌ์ˆ™ ์ธ์› 3๋ช…์—‘์ŠคํŠธ๋ผ๋ฒ ๋“œ ์ถ”๊ฐ€๋ฆฌ๋ฒ„ ๋ทฐ ์—…๊ทธ๋ ˆ์ด๋“œ์กฐ์‹ 3์ธ ํˆฌ์ˆ™ ์ธ์›์€ 3๋ช…์ด์—ˆ์–ด์š”. ์•Œ์•„๋ณด๋‹ˆ ์ธ์› ์ถ”๊ฐ€ ๋น„์šฉ์˜ ๋‘๋ฐฐ๋ฅผ ๋‚ด๋ฉด ์นจ๋Œ€์ถ”๊ฐ€๊ฐ€ ๋˜๊ธธ๋ž˜ (์นจ๋Œ€ ์ถ”๊ฐ€ ์‹œ ์ธ์› ์ถ”๊ฐ€ ๋น„์šฉ ์—†์Œ) 12๋งŒ 1์ฒœ์› ๋‚ด๊ณ  ์—‘์ŠคํŠธ๋ผ๋ฒ ๋“œ ์‹ ์ฒญํ–ˆ๊ตฌ์š”. ๋ฆฌ๋ฒ„๋ทฐ ์ „ํ™˜์œผ๋กœ 6๋งŒ 500์› ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค.์ฐธ๊ณ ๋กœ, ์ „๋ง ์š•์กฐ ๋น„์šฉ๋„ 6๋งŒ 500์› ์ถ”๊ฐ€ํ•˜๋ฉด ์—…๊ทธ๋ ˆ์ด๋“œ๊ฐ€ ๋˜๋Š”๋ฐ ๋‹ค..
[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..