LeetCode每日一题(1770. Maximum Score from Performing Multiplication Operations)

You are given two integer arrays nums and multipliers of size n and m respectively, where n >= m. The arrays are 1-indexed.

You begin with a score of 0. You want to perform exactly m operations. On the ith operation (1-indexed), you will:

Choose one integer x from either the start or the end of the array nums.
Add multipliers[i] * x to your score.
Remove x from the array nums.
Return the maximum score after performing m operations.

Example 1:

Input: nums = [1,2,3], multipliers = [3,2,1]
Output: 14

Explanation: An optimal solution is as follows:

  • Choose from the end, [1,2,3], adding 3 * 3 = 9 to the score.
  • Choose from the end, [1,2], adding 2 * 2 = 4 to the score.
  • Choose from the end, [1], adding 1 * 1 = 1 to the score.

The total score is 9 + 4 + 1 = 14.

Example 2:

Input: nums = [-5,-3,-3,-2,7,1], multipliers = [-10,-5,3,4,6]
Output: 102

Explanation: An optimal solution is as follows:

  • Choose from the start, [-5,-3,-3,-2,7,1], adding -5 * -10 = 50 to the score.
  • Choose from the start, [-3,-3,-2,7,1], adding -3 * -5 = 15 to the score.
  • Choose from the start, [-3,-2,7,1], adding -3 * 3 = -9 to the score.
  • Choose from the end, [-2,7,1], adding 1 * 4 = 4 to the score.
  • Choose from the end, [-2,7], adding 7 * 6 = 42 to the score.

The total score is 50 + 15 - 9 + 4 + 42 = 102.

Constraints:

  • n == nums.length
  • m == multipliers.length
  • 1 <= m <= 103
  • m <= n <= 105
  • -1000 <= nums[i], multipliers[i] <= 1000

基本就是 dp 的思路, start, end 分别是 nums 从左到右和从右到左的 index, i 是 multipliers 从左到右的 index, dp[start][end][i]=max(nums[start] _ multipliers[i] + dp[start+1][end][i+1], nums[end] _ multipliers[i] + dp[start][end-1][i+1]). 这里的 end 其实可以通过 start 和 i 计算出来, end = nums.len() - 1 - (i - s)



use std::collections::HashMap;

impl Solution {
    fn dp(
        nums: &Vec<i32>,
        multipliers: &Vec<i32>,
        s: usize,
        i: usize,
        cache: &mut HashMap<(usize, usize), i32>,
    ) -> i32 {
        let e = nums.len() - 1 - (i - s);
        if i == multipliers.len() - 1 {
            if multipliers[i] > 0 {
                let ans = nums[s].max(nums[e]) * multipliers[i];
                cache.insert((s, i), ans);
                return ans;
            }
            let ans = nums[s].min(nums[e]) * multipliers[i];
            cache.insert((s, i), ans);
            return ans;
        }
        if let Some(c) = cache.get(&(s, i)) {
            return *c;
        }
        let start = nums[s] * multipliers[i]
            + if let Some(c) = cache.get(&(s + 1, i + 1)) {
                *c
            } else {
                Solution::dp(nums, multipliers, s + 1, i + 1, cache)
            };
        let end = nums[e] * multipliers[i]
            + if let Some(c) = cache.get(&(s, i + 1)) {
                *c
            } else {
                Solution::dp(nums, multipliers, s, i + 1, cache)
            };
        let ans = start.max(end);
        cache.insert((s, i), ans);
        ans
    }
    pub fn maximum_score(nums: Vec<i32>, multipliers: Vec<i32>) -> i32 {
        Solution::dp(&nums, &multipliers, 0, 0, &mut HashMap::new())
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值