반응형

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];
    }
};
반응형

+ Recent posts