LeetCode每日一题(1856. Maximum Subarray Min-Product)

The min-product of an array is equal to the minimum value in the array multiplied by the array’s sum.

For example, the array [3,2,5] (minimum value is 2) has a min-product of 2 _ (3+2+5) = 2 _ 10 = 20.
Given an array of integers nums, return the maximum min-product of any non-empty subarray of nums. Since the answer may be large, return it modulo 109 + 7.

Note that the min-product should be maximized before performing the modulo operation. Testcases are generated such that the maximum min-product without modulo will fit in a 64-bit signed integer.

A subarray is a contiguous part of an array.

Example 1:

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

Explanation: The maximum min-product is achieved with the subarray [2,3,2] (minimum value is 2).
2 _ (2+3+2) = 2 _ 7 = 14.

Example 2:

Input: nums = [2,3,3,1,2]
Output: 18

Explanation: The maximum min-product is achieved with the subarray [3,3] (minimum value is 3).
3 _ (3+3) = 3 _ 6 = 18.

Example 3:

Input: nums = [3,1,5,6,4,2]
Output: 60

Explanation: The maximum min-product is achieved with the subarray [5,6,4] (minimum value is 4).
4 _ (5+6+4) = 4 _ 15 = 60.

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 107

  1. 对于包括 nums[i]在内的,并且 nums[i]在其中为最小值的 subarray, 计算 min-product
  2. 用 monostack 来存储已经遍历过的 num 及其 index, 方便查找距离当前 num[i]最近且小于 num[i]的元素的 index, 这样就可以确定 nums[i]为最小值的 subarray 的左右边界

impl Solution {
    pub fn max_sum_min_product(nums: Vec<i32>) -> i32 {
        let mut left_bounds = Vec::new();
        let mut right_bounds = Vec::new();
        let mut stack: Vec<(i32, usize)> = Vec::new();
        for i in 0..nums.len() {
            while !stack.is_empty() {
                let (last, j) = stack.remove(stack.len() - 1);
                if last >= nums[i] {
                    continue;
                }
                stack.push((last, j));
                break;
            }
            if stack.is_empty() {
                left_bounds.push(0);
            } else {
                left_bounds.push(stack.last().unwrap().1 + 1);
            }
            stack.push((nums[i], i));
        }
        stack.clear();
        for i in (0..nums.len()).rev() {
            while !stack.is_empty() {
                let (last, j) = stack.remove(stack.len() - 1);
                if last >= nums[i] {
                    continue;
                }
                stack.push((last, j));
                break;
            }
            if stack.is_empty() {
                right_bounds.push(nums.len() - 1);
            } else {
                right_bounds.push(stack.last().unwrap().1 - 1);
            }
            stack.push((nums[i], i));
        }
        right_bounds.reverse();
        let mut ans = 0i64;
        let mut prefix: Vec<i64> = nums
            .iter()
            .scan(0i64, |s, v| {
                *s += *v as i64;
                Some(*s)
            })
            .collect();
        prefix.insert(0, 0);
        for i in 0..nums.len() {
            let left = left_bounds[i];
            let right = right_bounds[i];
            ans = ans.max((prefix[right + 1] - prefix[left]) * nums[i] as i64);
        }
        (ans % (10i64.pow(9) + 7)) as i32
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值