• 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())
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
  • 相关阅读:
    传输层协议之UDP
    GL-Cache: Group-level learning for efficient and high-performance caching
    level2行情websocket订阅
    MySQL数据库——存储过程-游标(介绍-声明游标、打开游标、获取游标记录、关闭游标,案例)
    联盟链 Hyperledger Fabric 应用场景
    Java成员内部类、局部内部类、匿名内部类
    FileInputStream和FileOutputStream
    Go数据结构队列
    指针和数组试题解析(2)字符数组部分
    VR智慧生活助力千行百业,彰显VR全景制作价值
  • 原文地址:https://blog.csdn.net/wangjun861205/article/details/126889305