1856:子数组最小乘积的最大值
This commit is contained in:
parent
45a49d65cc
commit
e4e1e9c26f
104
src/main/java/leetcode/editor/cn/MaximumSubarrayMinProduct.java
Normal file
104
src/main/java/leetcode/editor/cn/MaximumSubarrayMinProduct.java
Normal file
@ -0,0 +1,104 @@
|
||||
//一个数组的 最小乘积 定义为这个数组中 最小值 乘以 数组的 和 。
|
||||
//
|
||||
//
|
||||
// 比方说,数组 [3,2,5] (最小值是 2)的最小乘积为 2 * (3+2+5) = 2 * 10 = 20 。
|
||||
//
|
||||
//
|
||||
// 给你一个正整数数组 nums ,请你返回 nums 任意 非空子数组 的最小乘积 的 最大值 。由于答案可能很大,请你返回答案对 109 + 7 取余 的
|
||||
//结果。
|
||||
//
|
||||
// 请注意,最小乘积的最大值考虑的是取余操作 之前 的结果。题目保证最小乘积的最大值在 不取余 的情况下可以用 64 位有符号整数 保存。
|
||||
//
|
||||
// 子数组 定义为一个数组的 连续 部分。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:nums = [1,2,3,2]
|
||||
//输出:14
|
||||
//解释:最小乘积的最大值由子数组 [2,3,2] (最小值是 2)得到。
|
||||
//2 * (2+3+2) = 2 * 7 = 14 。
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:nums = [2,3,3,1,2]
|
||||
//输出:18
|
||||
//解释:最小乘积的最大值由子数组 [3,3] (最小值是 3)得到。
|
||||
//3 * (3+3) = 3 * 6 = 18 。
|
||||
//
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
//
|
||||
//输入:nums = [3,1,5,6,4,2]
|
||||
//输出:60
|
||||
//解释:最小乘积的最大值由子数组 [5,6,4] (最小值是 4)得到。
|
||||
//4 * (5+6+4) = 4 * 15 = 60 。
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= nums.length <= 105
|
||||
// 1 <= nums[i] <= 107
|
||||
//
|
||||
// Related Topics 排序 并查集 队列 二分查找 动态规划
|
||||
// 👍 22 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Stack;
|
||||
|
||||
//1856:子数组最小乘积的最大值
|
||||
public class MaximumSubarrayMinProduct {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new MaximumSubarrayMinProduct().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int maxSumMinProduct(int[] nums) {
|
||||
int length = nums.length;
|
||||
long[] sum = new long[length];
|
||||
sum[0] = nums[0];
|
||||
for (int i = 1; i < length; i++) {
|
||||
sum[i] = sum[i - 1] + nums[i];
|
||||
}
|
||||
|
||||
int[] start = new int[length];
|
||||
Arrays.fill(start, -1);
|
||||
int[] end = new int[length];
|
||||
Arrays.fill(end, length);
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
while (!stack.isEmpty() && nums[stack.peek()] >= nums[i]) {
|
||||
end[stack.peek()] = i;
|
||||
stack.pop();
|
||||
}
|
||||
if (!stack.isEmpty()) {
|
||||
start[i] = stack.peek();
|
||||
}
|
||||
stack.push(i);
|
||||
}
|
||||
|
||||
long max = 0;
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
max = Math.max(nums[i] * (sum[end[i] - 1] - (start[i] == -1 ? 0 : sum[start[i]])), max);
|
||||
}
|
||||
return (int) (max % ((int)Math.pow(10, 9) + 7));
|
||||
// long mod = 1000000000+7;
|
||||
// return (int) (max % mod);
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
<p>一个数组的 <strong>最小乘积</strong> 定义为这个数组中 <strong>最小值</strong> <strong>乘以 </strong>数组的 <strong>和</strong> 。</p>
|
||||
|
||||
<ul>
|
||||
<li>比方说,数组 <code>[3,2,5]</code> (最小值是 <code>2</code>)的最小乘积为 <code>2 * (3+2+5) = 2 * 10 = 20</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>给你一个正整数数组 <code>nums</code> ,请你返回 <code>nums</code> 任意 <strong>非空子数组</strong> 的<strong>最小乘积</strong> 的 <strong>最大值</strong> 。由于答案可能很大,请你返回答案对 <code>10<sup>9</sup> + 7</code> <strong>取余 </strong>的结果。</p>
|
||||
|
||||
<p>请注意,最小乘积的最大值考虑的是取余操作 <strong>之前</strong> 的结果。题目保证最小乘积的最大值在 <strong>不取余</strong> 的情况下可以用 <strong>64 位有符号整数</strong> 保存。</p>
|
||||
|
||||
<p><strong>子数组</strong> 定义为一个数组的 <strong>连续</strong> 部分。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,<strong>2,3,2</strong>]
|
||||
<b>输出:</b>14
|
||||
<b>解释:</b>最小乘积的最大值由子数组 [2,3,2] (最小值是 2)得到。
|
||||
2 * (2+3+2) = 2 * 7 = 14 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [2,<strong>3,3</strong>,1,2]
|
||||
<b>输出:</b>18
|
||||
<b>解释:</b>最小乘积的最大值由子数组 [3,3] (最小值是 3)得到。
|
||||
3 * (3+3) = 3 * 6 = 18 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [3,1,<strong>5,6,4</strong>,2]
|
||||
<b>输出:</b>60
|
||||
<b>解释:</b>最小乘积的最大值由子数组 [5,6,4] (最小值是 4)得到。
|
||||
4 * (5+6+4) = 4 * 15 = 60 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>7</sup></code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>排序</li><li>并查集</li><li>队列</li><li>二分查找</li><li>动态规划</li></div></div>\n<div><li>👍 22</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user