forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 2478.Number-of-Beautiful-Partitions_v2.cpp
- Loading branch information
1 parent
e1a4fc2
commit 14418f2
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
...rogramming/2478.Number-of-Beautiful-Partitions/2478.Number-of-Beautiful-Partitions_v2.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using LL = long long; | ||
class Solution { | ||
LL dp[1005][1005]; | ||
LL M = 1e9+7; | ||
public: | ||
int beautifulPartitions(string s, int K, int minLength) | ||
{ | ||
int n = s.size(); | ||
s = "#"+s; | ||
|
||
dp[0][0] = 1; | ||
for (int j=1; j<=K; j++) | ||
{ | ||
LL sum = 0; | ||
for (int i=1; i<=n; i++) | ||
{ | ||
if (i-minLength>=0 && !isprime(s[i-minLength]) && isprime(s[i-minLength+1])) | ||
{ | ||
sum += dp[i-minLength][j-1]; | ||
sum %= M; | ||
} | ||
if (!isprime(s[i])) { | ||
dp[i][j] = sum; | ||
} | ||
} | ||
} | ||
return dp[n][K]; | ||
} | ||
|
||
bool isprime(char ch) | ||
{ | ||
return ch == '2' || ch == '3' || ch == '5' || ch == '7'; | ||
} | ||
}; |