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

IN DEPTH CAKE/Supercoder

[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๋น„ํŠธ ์ •์ˆ˜์— ๋งž๋„๋ก ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

 

๋ฌธ์ œ ์›๋ฌธ: https://leetcode.com/problems/decode-ways/description/

 

๐Ÿ“ ์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

Example 1:

Input: s = "12"
Output: 2
Explanation: "12" could be decoded as "AB" (1 2) or "L" (12).

 

Example 2:

Input: s = "226"
Output: 3
Explanation: "226" could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).

 

Example 3:

Input: s = "06"
Output: 0
Explanation: "06" cannot be mapped to "F" because of the leading zero ("6" is different from "06").

 

 

 

๐Ÿ“ ๋ฌธ์ œ ํ’€์ด

 

ํ•ด๋‹น ๋ฌธ์ œ๋Š” string์˜ n ๋ฒˆ์งธ ์ˆซ์ž๊นŒ์ง€์˜ decoding ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐ์— ์•ž์„  n-1, n-2 ๊นŒ์ง€์˜  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์—์„œ dynamic programming (DP)๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด, a[n]์„ n ๋ฒˆ์งธ ์ž๋ฆฌ์˜ string ๊นŒ์ง€์˜ decoding ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ผ๊ณ  ์ •์˜ํ•œ๋‹ค๊ณ  ํ•˜๋ฉด a[n]์€ ์กฐ๊ฑด๋ถ€์ ์œผ๋กœ a[n-2] + a[n-1]๋กœ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๋•Œ, ๊ฐ ์กฐ๊ฑด์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. code[n-1], code[n]์œผ๋กœ ๋‘ ์ž๋ฆฌ ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค๊ณ ํ•œ๋‹ค๋ฉด n-2๊นŒ์ง€๋งŒ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ ๊ฒฝ์šฐ์˜  ์ˆ˜๋ฅผ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ๋˜ ํ•œํŽธ์œผ๋กœ code[n]์ด 0์ด ์•„๋‹ˆ๋ผ๋ฉด ํ•œ ์ž๋ฆฌ ์ˆ˜๋งŒ์œผ๋กœ๋„ ์ฝ”๋“œ๊ฐ€ ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—  a[n-1] ๊นŒ์ง€ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์ฝ”๋‘๋“œ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์˜ฌ ์ˆ˜  ์žˆ๋‹ค.

 

 

 

์ด๋ฅผ ์ฝ”๋“œ๋กœ ์˜ฎ๊ธฐ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

Runtime 0 ms: Beats 100.00 % of users with c++

Memory: 7.37 MB Beats 96.92 % of users with c++

#define num(x) (int(x)-48)
#define nums(x,y) num(x)*10+num(y)

class Solution {
public:
    int numDecodings(string s) {

        const int N = s.size();
        vector<int> a(N, 0);

        if(num(s[0])== 0)
            return 0;
        if(N == 1)
            return 1;

        // baseline initialization
        // a[0]
        a[0] = 1;
        // a[1]
        if(num(s[1]) == 0)
            a[1] = ((nums(s[0],s[1]) >= 10) &&  (nums(s[0],s[1])<= 26))? a[0] : 0;
        else
            a[1] = ((nums(s[0],s[1]) >= 10) &&  (nums(s[0],s[1])<= 26))? a[0] + 1: a[0];

        // a[n]
        for(int n = 2; n < N; ++n){
            a[n] = a[n-2] * (int)((nums(s[n-1],s[n]) >= 10 && nums(s[n-1],s[n]) <= 26) && num(s[n-1] != 0)) + a[n-1] * (int)(num(s[n]) != 0);
        }

        return a[N-1];
        
    }
};

 

 

 

 

๋ฐ˜์‘ํ˜•