LeetCode每日一题(1416. Restore The Array)

给定一个没有空格的数字字符串s和一个整数k,你需要推断出所有可能的由[1, k]范围内的整数组成的原数组,使得原数组按特定方式打印后得到字符串s。返回可能的数组数量,对10^9 + 7取模。本文通过动态规划求解此类问题。" 102446958,8805497,JavaEE 实现图书管理系统,"['Java', '数据库管理', '软件工程', '信息系统', '图书管理']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 were in the range [1, k] and there are no leading zeros in the array.

Given the string s and the integer k, return the number of the possible arrays that can be printed as s using the mentioned program. Since the answer may be very large, return it modulo 109 + 7.

Example 1:

Input: s = “1000”, k = 10000
Output: 1

Explanation: The only possible array is [1000]

Example 2:

Input: s = “1000”, k = 10
Output: 0

Explanation: There cannot be an array that was printed this way and has all integer >= 1 and <= 10.

Example 3:

Input: s = “1317”, k = 2000
Output: 8

Explanation: Possible arrays are [1317],[131,7],[13,17],[1,317],[13,1,7],[1,31,7],[1,3,17],[1,3,1,7]

Constraints:

  • 1 <= s.length <= 10^5
  • s consists of only digits and does not contain leading zeros.
  • 1 <= k <= 10^9

遍历 s, 对于每个 s[i], 我们要么让它作为新数字的开头, 要么让它作为前面累积数字的结尾。
如果 dp[i][prev]是我们遍历到 s[i]且当前累积的数字是 prev 的情况, 那 dp[i][prev] = dp[i+1]prev _ 10 + s[i]] + dp[i+1]s[i]], 如果 prev _ 10 + s[i] > k 那说明我们只能把 s[i]作为新数字的开头, 所以 dp[i][prev] = dp[i+1]s[i]]



use std::collections::HashMap;

const M: i64 = 1000000007;
impl Solution {
    fn dp(nums: &Vec<i64>, k: i64, i: usize, prev: i64, cache: &mut HashMap<(usize, i64), i64>) -> i64 {
        if i == nums.len() {
            return 1;
        }
        if let Some(c) = cache.get(&(i, prev)) {
            return *c;
        }
        let curr = nums[i];
        if curr == 0 {
            if prev == 0 || prev * 10 > k {
                return 0;
            }
            let ans = Solution::dp(nums, k, i + 1, prev * 10, cache);
            cache.insert((i, prev), ans);
            return ans;
        }
        if prev == 0 {
            let ans = Solution::dp(nums, k, i + 1, nums[i], cache);
            cache.insert((i, prev), ans);
            return ans;
        }
        let mut ans = 0;
        if prev * 10 + nums[i] <= k {
            ans += Solution::dp(nums, k, i + 1, prev * 10 + nums[i], cache);
            ans %= M;
        }
        ans += Solution::dp(nums, k, i + 1, nums[i], cache);
        ans %= M;
        cache.insert((i, prev), ans);
        ans
    }
    pub fn number_of_arrays(s: String, k: i32) -> i32 {
        let nums: Vec<i64> = s.chars().map(|c| c.to_string().parse::<i64>().unwrap()).collect();
        Solution::dp(&nums, k as i64, 0, 0, &mut HashMap::new()) as i32
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值