当前位置:网站首页>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
}
}
边栏推荐
- Hudi integrated spark data analysis example (including code flow and test results)
- 2022-2-13 learning the imitation Niuke project - home page of the development community
- On February 14, 2022, learn the imitation Niuke project - develop the registration function
- 2022-1-6 Niuke net brush sword finger offer
- LeetCode 30. Concatenate substrings of all words
- 【Kotlin学习】类、对象和接口——定义类继承结构
- Recommend a low code open source project of yyds
- IDEA 中使用 Hudi
- Redis learning (I)
- 2022-2-14 learning xiangniuke project - Session Management
猜你喜欢
What are the stages of traditional enterprise digital transformation?
2022-2-13 learning xiangniuke project - version control
On February 14, 2022, learn the imitation Niuke project - develop the registration function
[point cloud processing paper crazy reading frontier version 10] - mvtn: multi view transformation network for 3D shape recognition
2022-2-13 learn the imitation Niuke project - Project debugging skills
2022-2-14 learning xiangniuke project - generate verification code
【点云处理之论文狂读前沿版12】—— Adaptive Graph Convolution for Point Cloud Analysis
Win10 quick screenshot
[kotlin learning] operator overloading and other conventions -- overloading the conventions of arithmetic operators, comparison operators, sets and intervals
IDEA 中使用 Hudi
随机推荐
Spark 集群安装与部署
Common formulas of probability theory
Idea uses the MVN command to package and report an error, which is not available
WARNING: You are using pip ; however. Later, upgrade PIP failed, modulenotfounderror: no module named 'pip‘
Filter comments to filter out uncommented and default values
Flink学习笔记(十一)Table API 和 SQL
LeetCode 30. Concatenate substrings of all words
[point cloud processing paper crazy reading classic version 11] - mining point cloud local structures by kernel correlation and graph pooling
Jenkins learning (II) -- setting up Chinese
[point cloud processing paper crazy reading classic version 10] - pointcnn: revolution on x-transformed points
Database execution error: SQL_ mode only_ full_ group_ by:
Basic knowledge of database design
Notes on numerical analysis (II): numerical solution of linear equations
[point cloud processing paper crazy reading frontier version 10] - mvtn: multi view transformation network for 3D shape recognition
Excel is not as good as jnpf form for 3 minutes in an hour. Leaders must praise it when making reports like this!
[point cloud processing paper crazy reading frontier version 8] - pointview gcn: 3D shape classification with multi view point clouds
Matlab dichotomy to find the optimal solution
Hudi 集成 Spark 数据分析示例(含代码流程与测试结果)
WARNING: You are using pip version 21.3.1; however, version 22.0.3 is available. Prompt to upgrade pip
【Kotlin学习】运算符重载及其他约定——重载算术运算符、比较运算符、集合与区间的约定