Skip to content

Commit 3fea66c

Browse files
authored
Create 2522.Partition-String-Into-Substrings-With-Values-at-Most-K.cpp
1 parent f9c4c3c commit 3fea66c

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
int dp[100005];
3+
public:
4+
int minimumPartition(string s, int k)
5+
{
6+
int n = s.size();
7+
int m = to_string(k).size();
8+
for (int i=0; i<n; i++)
9+
{
10+
if (m==1 && s[i]-'0'>k)
11+
return -1;
12+
}
13+
14+
s = "#"+s;
15+
dp[0] = 0;
16+
17+
for (int i=1; i<=n; i++)
18+
{
19+
if (i-m+1>=1 && stoi(s.substr(i-m+1, m)) <= k)
20+
dp[i] = dp[i-m] + 1;
21+
else
22+
dp[i] = dp[max(0, i-m+1)] + 1;
23+
}
24+
25+
return dp[n];
26+
27+
}
28+
};

0 commit comments

Comments
 (0)