|
| 1 | +# ์ฐ๊ด ๋งํฌ |
| 2 | +- [๋ฌธ์ ํ์ด ์ค์ผ์ค](https://github.com/orgs/DaleStudy/projects/6/views/5) |
| 3 | +- [๋ต์ ์ฝ๋ ์ ์ถ๋ฒ](https://github.com/DaleStudy/leetcode-study/wiki/%EB%8B%B5%EC%95%88-%EC%A0%9C%EC%B6%9C-%EA%B0%80%EC%9D%B4%EB%93%9C) |
| 4 | + |
| 5 | +# Problem |
| 6 | +- ๋ฌธ์ ๋งํฌ : http://leetcode.com/problems/decode-ways/ |
| 7 | +- ๋ฌธ์ ์ด๋ฆ : Decode ways |
| 8 | +- ๋ฌธ์ ๋ฒํธ : 91 |
| 9 | +- ๋์ด๋ : medium |
| 10 | +- ์นดํ
๊ณ ๋ฆฌ : DFS, DP |
| 11 | + |
| 12 | +# ๋ฌธ์ ์ค๋ช
|
| 13 | + |
| 14 | + |
| 15 | +# ์์ด๋์ด |
| 16 | +- ์ด๋ค ๋ฐฉ๋ฒ์ผ๋ก ์ ๊ทผํ๋์ง ์์ |
| 17 | +- ํฌ์ค vs ์ต์ ํ ์์ด๋์ด ์ฐจ์ด ๋ฑ |
| 18 | +- ์ก๋์ ๋ํ ๊ณ ๋ ค |
| 19 | + |
| 20 | +# โ
์ฝ๋ (Solution) |
| 21 | + |
| 22 | +## DFS(Memory Limit Exceeded) |
| 23 | +```cpp |
| 24 | +class Solution { |
| 25 | +int cnt; |
| 26 | +public: |
| 27 | + bool canBeDecoded(vector<char> st){ |
| 28 | + if(st.size()>2){ |
| 29 | + return false; |
| 30 | + }else if(st.size()==1){ |
| 31 | + return st[0]!='0'; |
| 32 | + } |
| 33 | + //st.size()==2 |
| 34 | + |
| 35 | + if(st[0]=='1'){ |
| 36 | + return true; |
| 37 | + }else if(st[0]=='2'){ |
| 38 | + return (int)(st[1]-'0') <= 6; |
| 39 | + }else{ |
| 40 | + return false; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + void helper(string s, int idx){ |
| 45 | + if(idx==s.size()){ |
| 46 | + cnt++; |
| 47 | + return; |
| 48 | + }else if(idx>s.size()){ |
| 49 | + return; |
| 50 | + } |
| 51 | + if(canBeDecoded(vector<char>(s.begin()+idx, s.begin()+idx+1))){ |
| 52 | + helper(s, idx+1); |
| 53 | + } |
| 54 | + |
| 55 | + if(idx+2<=s.size() && canBeDecoded(vector<char>(s.begin()+idx, s.begin()+idx+2))){ |
| 56 | + helper(s, idx+2); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + int numDecodings(string s) { |
| 61 | + cnt = 0; |
| 62 | + helper(s, 0); |
| 63 | + return cnt; |
| 64 | + } |
| 65 | +}; |
| 66 | +``` |
| 67 | +- ๋จ์ DFS ํ์ด |
| 68 | +- Memory Limit Exceeded(Heap ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ) ๋ฐ์ |
| 69 | +
|
| 70 | +## DP |
| 71 | +
|
| 72 | +```cpp |
| 73 | +class Solution { |
| 74 | +public: |
| 75 | + int numDecodings(string s) { |
| 76 | + int n = s.size(); |
| 77 | + vector<int> dp(n + 1, -1); // dp[i] = number of ways to decode from index i |
| 78 | +
|
| 79 | + return helper(s, 0, dp); |
| 80 | + } |
| 81 | +
|
| 82 | + int helper(string& s, int idx, vector<int>& dp) { |
| 83 | + if (idx == s.size()) { |
| 84 | + return 1; |
| 85 | + } |
| 86 | + if (s[idx] == '0') { |
| 87 | + return 0; |
| 88 | + } |
| 89 | + if (dp[idx] != -1) { |
| 90 | + return dp[idx]; |
| 91 | + } |
| 92 | +
|
| 93 | + int res = helper(s, idx + 1, dp); |
| 94 | +
|
| 95 | + if (idx + 1 < s.size()) { |
| 96 | + int num = stoi(s.substr(idx, 2)); |
| 97 | + if (num >= 10 && num <= 26) { |
| 98 | + res += helper(s, idx + 2, dp); |
| 99 | + } |
| 100 | + } |
| 101 | +
|
| 102 | + return dp[idx] = res; |
| 103 | + } |
| 104 | +}; |
| 105 | +``` |
| 106 | + |
| 107 | +- dp |
| 108 | +- ๋ฉ๋ชจ์ด์ ์ด์
|
| 109 | +- stoi / substr ๋ฉค๋ฒ ํจ์ ์ฌ์ฉํ์ฌ decode ํจ์ ๊ฐ๋ตํ |
| 110 | + |
| 111 | + |
| 112 | +# ์ต์ ํ ํฌ์ธํธ (Optimality Discussion) |
| 113 | +- ๋ฉ๋ชจ์ด์ ์ด์
|
| 114 | +- stoi / substr ๋ฉค๋ฒ ํจ์ ์ฌ์ฉํ์ฌ decode ํจ์ ๊ฐ๋ตํ |
| 115 | + |
| 116 | +# ๐งช ํ
์คํธ & ์ฃ์ง ์ผ์ด์ค |
| 117 | + |
| 118 | +# ๐ ๊ด๋ จ ์ง์ ๋ณต์ต |
| 119 | +- ๋ฉ๋ชจ์ด์ ์ด์
|
| 120 | +- stl(stoi / substr ๋ฑ) |
| 121 | + |
| 122 | +# ๐ ํ๊ณ |
| 123 | + |
| 124 | + |
0 commit comments