0
点赞
收藏
分享

微信扫一扫

【LeeCode】152. 乘积最大子数组

秀儿2020 2023-01-04 阅读 127

【题目描述】

给你一个整数数组 ​​nums​​ ,请你找出数组中乘积最大的非空连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。

测试用例的答案是一个 32-位 整数。

子数组 是数组的连续子序列。

​​https://leetcode.cn/problems/maximum-product-subarray/?favorite=2cktkvj​​


【示例】

【LeeCode】152. 乘积最大子数组_Math


【代码】​​画手大鹏​​

【LeeCode】152. 乘积最大子数组_Math_02

package com.company;
// 2023-1-4

class Solution {
public int maxProduct(int[] nums) {
int res = Integer.MIN_VALUE;
int max = 1;
int min = 1;
for (int i = 0; i < nums.length; i++) {
if (nums[i] < 0){
// 如果当前数小于0, 则最大变最小,最小变最大
// 当遇到负数的时候进行交换,因为阶段最小*负数就变阶段最大了,反之同理
int tmp = max;
max = min;
min = tmp;
}
// 对于最小值来说,最小值是本身则说明这个元素值比前面连续子数组的最小值还小。
// 相当于重置了阶段最小值的起始位置
max = Math.max(nums[i], max * nums[i]);
min = Math.min(nums[i], min * nums[i]);
//对比阶段最大值和结果最大值
res = Math.max(max, res);
}
// System.out.println(res);
return res;
}
}

public class Test{
public static void main(String[] args) {
new Solution().maxProduct(new int[]{2,3,-2,4}); // 输出 6
new Solution().maxProduct(new int[]{-2, 0, -1}); // 输出 0
new Solution().maxProduct(new int[]{-4, -3}); // 输出 12
new Solution().maxProduct(new int[]{0, 2}); // 输出 2
new Solution().maxProduct(new int[]{0, -2}); // 输出 0
new Solution().maxProduct(new int[]{3, -1,4}); // 输出 4
new Solution().maxProduct(new int[]{-2, 3, -4}); // 输出 24
new Solution().maxProduct(new int[]{-2, -3, 7}); // 输出 42
new Solution().maxProduct(new int[]{2,-5,-2,-4,3}); // 输出 24 -- 本例代码有误
}
}

【代码】​​钰娘娘​​

【LeeCode】152. 乘积最大子数组_最小值_03


class Solution {
public int maxProduct(int[] nums) {
int ans = Integer.MIN_VALUE;
int max = 1;
int min = 1;
for(int num:nums){
int nextmax = Math.max(min*num,Math.max(num,max*num));
int nextmin = Math.min(min*num,Math.min(num,max*num));
max = nextmax;
min = nextmin;
ans = Math.max(max,ans);
}
return ans;
}
}

作者:钰娘娘🍰
链接:https://leetcode.cn/problems/maximum-product-subarray/solutions/1656442/-by-yu-niang-niang-ub1s/

【代码】admin

通过率  142/189  因为这里有个情况就是存在负数, 这就有可能导致最小的值变成最大的或者最大的变成最小的

一步一步 debug出来的, 代码质量很差, 思路有点乱 保留代码

package com.company;
// 2023-1-4

class Solution {
public int maxProduct(int[] nums) {
if (nums.length == 1) return nums[0];
if (nums.length == 2) return Math.max(Math.max(nums[0], nums[1]), nums[0] * nums[1]);
int max = 0;
for (int i = 0; i < nums.length; i++){
int sum = nums[i];
for (int j = i + 1; j < nums.length; j++) {
int tmp = sum * nums[j];
if (tmp < 0){
if (j + 1 < nums.length && nums[j+1] < 0){
sum = tmp;
continue;
}
}
if (tmp > max){
max = tmp;
sum = tmp;
}else {
tmp = Math.max(nums[i], nums[j]);
max = Math.max(tmp, max);
break;
}
}
}
System.out.println(max);
return max;
}
}

public class Test{
public static void main(String[] args) {
new Solution().maxProduct(new int[]{2,3,-2,4}); // 输出 6
new Solution().maxProduct(new int[]{-2, 0, -1}); // 输出 0
new Solution().maxProduct(new int[]{-4, -3}); // 输出 12
new Solution().maxProduct(new int[]{0, 2}); // 输出 2
new Solution().maxProduct(new int[]{0, -2}); // 输出 0
new Solution().maxProduct(new int[]{3, -1,4}); // 输出 4
new Solution().maxProduct(new int[]{-2, 3, -4}); // 输出 24
new Solution().maxProduct(new int[]{-2, -3, 7}); // 输出 42
new Solution().maxProduct(new int[]{2,-5,-2,-4,3}); // 输出 24 -- 本例代码有误
}
}

【代码】其他

【LeeCode】152. 乘积最大子数组_Math_04

举报

相关推荐

0 条评论