当前位置:网站首页>Leetcode daily question (1856. maximum subarray min product)
Leetcode daily question (1856. maximum subarray min product)
2022-07-03 09:33:00 【wangjun861205】
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
- For including nums[i] Inside , also nums[i] Which is the minimum subarray, Calculation min-product
- use monostack To store the traversed num And its index, Convenient to find the current distance num[i] Recent and less than num[i] The elements of the index, So you can make sure nums[i] Is the minimum value of subarray The left and right borders of
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
}
}
边栏推荐
- 全球KYC服务商ADVANCE.AI 活体检测产品通过ISO国际安全认证 产品能力再上一新台阶
- Beego learning - JWT realizes user login and registration
- Build a solo blog from scratch
- Filter comments to filter out uncommented and default values
- LeetCode每日一题(1362. Closest Divisors)
- Numerical analysis notes (I): equation root
- Learning C language from scratch -- installation and configuration of 01 MinGW
- [kotlin learning] control flow of higher-order functions -- lambda return statements and anonymous functions
- 【Kotlin学习】类、对象和接口——定义类继承结构
- Notes on numerical analysis (II): numerical solution of linear equations
猜你喜欢

Jenkins learning (I) -- Jenkins installation

PolyWorks script development learning notes (4) - data import and alignment using file import

文件系统中的目录与切换操作

Hudi integrated spark data analysis example (including code flow and test results)

Go language - Reflection

CATIA automation object architecture - detailed explanation of application objects (I) document/settingcontrollers

2022-2-13 learning the imitation Niuke project - home page of the development community

CATIA automation object architecture - detailed explanation of application objects (III) systemservice

Jenkins learning (II) -- setting up Chinese

There is no open in default browser option in the right click of the vscade editor
随机推荐
Jenkins learning (I) -- Jenkins installation
Construction and test of TFTP server under unbuntu (Debian)
Django operates Excel files through openpyxl to import data into the database in batches.
PolyWorks script development learning notes (I) - script development environment
Solve the problem of disordered code in vscode development, output Chinese and open source code
Powerdesign reverse wizard such as SQL and generates name and comment
Build a solo blog from scratch
Hudi 快速体验使用(含操作详细步骤及截图)
LeetCode每日一题(1856. Maximum Subarray Min-Product)
Overview of database system
Jestson nano custom root file system creation (supports the smallest root file system of NVIDIA Graphics Library)
Principles of computer composition - cache, connection mapping, learning experience
【Kotlin学习】类、对象和接口——定义类继承结构
Hudi 数据管理和存储概述
用Redis实现分布式锁
Detailed steps of windows installation redis
Idea uses the MVN command to package and report an error, which is not available
解决Editor.md上传图片获取不到图片地址问题
[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords
LeetCode每日一题(1300. Sum of Mutated Array Closest to Target)