Skip to content

Commit ab9c3b6

Browse files
committed
Decode ways
1 parent 77c66c7 commit ab9c3b6

File tree

2 files changed

+125
-1
lines changed

2 files changed

+125
-1
lines changed

โ€Žcombination-sum/haung921209.mdโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
- ๋ฌธ์ œ ์ด๋ฆ„ : Combination Sum
88
- ๋ฌธ์ œ ๋ฒˆํ˜ธ : 39
99
- ๋‚œ์ด๋„ : Medium
10-
- ์นดํ…Œ๊ณ ๋ฆฌ :
10+
- ์นดํ…Œ๊ณ ๋ฆฌ : DFS
1111

1212
# ๋ฌธ์ œ ์„ค๋ช…
1313

โ€Ždecode-ways/haung921209.mdโ€Ž

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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

Comments
ย (0)