当前位置:网站首页>LeetCode每日一题(1856. Maximum Subarray Min-Product)
LeetCode每日一题(1856. Maximum Subarray Min-Product)
2022-07-03 09:01: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
- 对于包括 nums[i]在内的,并且 nums[i]在其中为最小值的 subarray, 计算 min-product
- 用 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
}
}
边栏推荐
- Run flash demo on ECS
- What are the stages of traditional enterprise digital transformation?
- Jenkins learning (III) -- setting scheduled tasks
- [kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?
- CATIA automation object architecture - detailed explanation of application objects (I) document/settingcontrollers
- LeetCode每日一题(1300. Sum of Mutated Array Closest to Target)
- [point cloud processing paper crazy reading frontier version 8] - pointview gcn: 3D shape classification with multi view point clouds
- Crawler career from scratch (I): crawl the photos of my little sister ① (the website has been disabled)
- Detailed steps of windows installation redis
- We have a common name, XX Gong
猜你喜欢
![[point cloud processing paper crazy reading classic version 8] - o-cnn: octree based revolutionary neural networks for 3D shape analysis](/img/fa/36d28b754a9f380bfd86d4562268c3.png)
[point cloud processing paper crazy reading classic version 8] - o-cnn: octree based revolutionary neural networks for 3D shape analysis

LeetCode 513. Find the value in the lower left corner of the tree

Vs2019 configuration opencv3 detailed graphic tutorial and implementation of test code

Crawler career from scratch (IV): climb the bullet curtain of station B through API

Run flash demo on ECS

2022-2-13 learn the imitation Niuke project - Project debugging skills
![[kotlin learning] operator overloading and other conventions -- overloading the conventions of arithmetic operators, comparison operators, sets and intervals](/img/8d/938e232c1016cabe9ee0f72be87a22.png)
[kotlin learning] operator overloading and other conventions -- overloading the conventions of arithmetic operators, comparison operators, sets and intervals

Liteide is easy to use
[graduation season | advanced technology Er] another graduation season, I change my career as soon as I graduate, from animal science to programmer. Programmers have something to say in 10 years

Redis learning (I)
随机推荐
【点云处理之论文狂读前沿版11】—— Unsupervised Point Cloud Pre-training via Occlusion Completion
[set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu
LeetCode每日一题(1300. Sum of Mutated Array Closest to Target)
Recommend a low code open source project of yyds
There is no open in default browser option in the right click of the vscade editor
Crawler career from scratch (3): crawl the photos of my little sister ③ (the website has been disabled)
[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
【点云处理之论文狂读前沿版10】—— MVTN: Multi-View Transformation Network for 3D Shape Recognition
LeetCode 57. Insert interval
Utilisation de hudi dans idea
Beego learning - Tencent cloud upload pictures
Jenkins learning (II) -- setting up Chinese
Construction of simple database learning environment
Flink学习笔记(十一)Table API 和 SQL
[point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis
On February 14, 2022, learn the imitation Niuke project - develop the registration function
PowerDesigner does not display table fields, only displays table names and references, which can be modified synchronously
State compression DP acwing 91 Shortest Hamilton path
[point cloud processing paper crazy reading classic version 12] - foldingnet: point cloud auto encoder via deep grid deformation
Trial of the combination of RDS and crawler