반응형
https://leetcode.com/problems/restore-the-array/description/
Restore The Array - LeetCode
Can you solve this real interview question? Restore The Array - A program was supposed to print an array of integers. The program forgot to print whitespaces and the array is printed as a string of digits s and all we know is that all integers in the array
leetcode.com
class Solution {
public:
int dp[100000];
const int MOD = 1e9 + 7;
int numberOfArrays(string s, int k) {
memset(dp, -1, sizeof(dp));
return dfs(s, k, 0);
}
int dfs(string& s, int k, int idx) {
if(dp[idx] != -1) return dp[idx];
if(idx == s.size()) return 1;
dp[idx] = 0;
long long val = 0;
for(int i=idx; i<s.size(); i++) {
val = val * 10 + (s[i] - '0');
if(val < 1 || val > k) break;
dp[idx] += dfs(s, k, i + 1);
dp[idx] %= MOD;
}
return dp[idx];
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 2336. Smallest Number in Infinite Set (0) | 2023.04.27 |
---|---|
LeetCode 1046. Last Stone Weight (0) | 2023.04.24 |
LeetCode 1312. Minimum Insertion Steps to Make a String Palindrome (0) | 2023.04.22 |
LeetCode 662. Maximum Width of Binary Tree (0) | 2023.04.20 |
LeetCode 1372. Longest ZigZag Path in a Binary Tree (0) | 2023.04.19 |