Find the contiguous subarray within an array (containing at least one number)
which has the largest product.
For example, given the array [2,3,-2,4],
the contiguous subarray [2,3] has the largest product = 6.
/*首先想到了和最大相加子串问题,但是不同的是以下两点:
1.任何数*0=0,解决方法有两种:
1.根据0的分布把数组分为几部分,每部分都不含0,分别求最大值,最后选最大的(若数组有0,且各部分比较结果是负数时,结果要取0)
2.每乘一个数都要保存最大值,当遇到0时,记录当前状态的变量置0
由于1需要存储0的位置,给程序带来额外开销,所以2较好
2.负数的存在会导致较小(大)的局部解后边可能会成为较大(小)的解,解决方法有两种(由于遇上0的问题已经解决,所以这里的算法都是在没有0的情况下):
1.配合1.1使用,统计负数个数,双数时直接把这部分全部相乘,单数时取【最后一个负数前所有值相乘结果】
和【第一个负数之后所有值相乘结果】这两个值的较大者
2.*动态规划的方法:设置最终解变量res和局部解变量max和min,局部变量设置两个的原因是负数的存在,动态方程:
当前值是正数时,max(i) = max(max(i-1)* nums(i),nums(i)),min(i) = min(min(i-1)* nums(i),nums(i))
当前值是负数时,max(i) = max(min(i-1)* nums(i),nums(i)) ,min(i) = min(max(i-1)* nums(i),nums(i))
比较之后可以发现,只要当遇上负数时,将max和min两个变量交换,则状态方程可以统一*/
动态规划:
public int maxProduct1(int[] nums) {
if (nums == null || nums.length == 0) return 0;
//全局解
int res = nums[0];
for (int i = 1, max = res, min = res; i < nums.length; i++) {
if (nums[i] < 0) {
int temp = max;
max = min;
min = temp;
}
//状态方程
max = Math.max(max * nums[i], nums[i]);
min = Math.min(min * nums[i], nums[i]); if (max > res) res = max;
} return res;
}

操作数组方法:

public int maxProduct2(int[] nums) {
if (nums.length == 1)
return nums[0];
int res = 0;
//数组记录0的位置
List<Integer> l = new ArrayList<>();
for (int i = 0;i < nums.length;i++)
{
if (nums[i] == 0)
l.add(i);
}
//没有0的情况
if (l.size() == 0)
return product(0,nums.length,nums);
//有0的情况
else
{
//分为几部分求解
res = Math.max(res,product(0,l.get(0),nums));
for (int i = 1; i < l.size(); i++) {
res = Math.max(res,product(l.get(i-1)+1,l.get(i),nums));
}
res = Math.max(res,product(l.get(l.size()-1)+1,nums.length,nums));
return Math.max(res,0);
} }
public int product(int sta,int end,int[] nums)
{
if (sta > nums.length-1)
return 0;
if (end - sta <= 1)
return nums[sta];
int loc = 1;
int num = 0;
int index = 0;
//数组记录第一个负数和最后一个负数的位置
List<Integer> l = new ArrayList<>();
for (int i = sta;i < end;i++)
{
if (nums[i] < 0)
{
num++;
l.add(i);
} }
//双数情况
if (num%2 == 0)
{
for (int i = sta;i < end;i++) {
loc *= nums[i];
}
return loc;
}
//单数情况
else
{
int loc1 = 1;
int loc2 = 1;
for (int i = sta;i < l.get(l.size()-1);i++ )
{
loc1 *= nums[i];
}
for (int i = l.get(0)+1;i < end;i++)
{
loc2 *= nums[i];
}
return Math.max(loc1,loc2);
}
}

152. Maximum Product Subarray动态规划连乘最大子串的更多相关文章

  1. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

  2. 152. Maximum Product Subarray - LeetCode

    Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...

  3. 【刷题-LeetCode】152 Maximum Product Subarray

    Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...

  4. 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray

    题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...

  5. [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  6. [LeetCode]152. Maximum Product Subarray

    This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...

  7. LeetCode 152. Maximum Product Subarray (最大乘积子数组)

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  8. Leetcode#152 Maximum Product Subarray

    原题地址 简单动态规划,跟最大子串和类似. 一维状态空间可以经过压缩变成常数空间. 代码: int maxProduct(int A[], int n) { ) ; ]; ]; ]; ; i > ...

  9. 152. Maximum Product Subarray(动态规划)

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

随机推荐

  1. XOR性质

    异或XOR的性质: 1. 交换律 2. 结合律 3. x^x = 0 -> 偶数个异或为0 4. x^0 = x -> 奇数个异或为本身 5. 自反性:a^b^b = a^0 =a

  2. 记安装Wampsever

    遇到的问题: Wampsever 启动所有服务后图标为黄色 localhost 问题:显示 IIS Windows 在用 localhost 访问本机的php文件和用ip地址(不是127.0.0.1) ...

  3. B. Irreducible Anagrams【CF 1290B】

    思路: 设tx为t类别字符的个数. ①对于长度小于2的t明显是"YES"②对于字符类别只有1个的t明显是"YES"③对于字符类别有2个的t,如左上图:如果str ...

  4. Python【集合】、【函数】、【三目运算】、【lambda】、【文件操作】

    set集合: •集合的创建; set_1 = set() #方法一 set_1 = {''} #方法二 •set是无序,不重复的集合; set_1 = {'k1','k2','k3'} set_1.a ...

  5. Shiro remeberMe反序列化漏洞复现(Shiro-550)

    Apache Shiro是一个强大易用的Java安全框架,提供了认证.授权.加密和会话管理等功能.Shiro框架直观.易用,同时也能提供健壮的安全性.在Apache Shiro编号为550的 issu ...

  6. 敏捷开发(Scrum)与敏捷测试

    1.敏捷测试流程和传统测试流程 软件测试是贯穿整个软件开发生命周期.对软件产品(包括阶段性产品)进行验证和确认的活动过程,也是对软件产品质量持续的评估过程,其目的是尽快尽早地发现在软件产品(包括阶段性 ...

  7. 【题解】The Great Divide [Uva10256]

    [题解]The Great Divide [Uva10256] 传送门:\(\text{The Great Divide [Uva10256]}\) [题目描述] 输入多组数据,每组数据给定 \(n\ ...

  8. 【题解】「CF1352A」Sum of Round Numbers

    应该是纯模拟吧. 直接输入一个字符串,然后一位一位看,如果不是0,就 k++,并计算这个数的真实的值,最后输出就行了. #include<iostream> #include<cst ...

  9. js--数组的map()方法的使用

    javaScript中Array.map()的用法 前言 作为一个刚刚踏入前端世界的小白,工作中看到身边同事大佬写的代码就像古诗一样简介整齐,而我的代码如同一堆散沙,看上去毫无段落感,而且简单的功能需 ...

  10. Eureka系列(三)获取服务Client端具体实现

    获取服务Client 端流程   我们先看下面这张图片,这张图片简单描述了下我们Client是如何获取到Server已续约实例信息的流程:  从图片中我们可以知晓大致流程就是Client会自己开启一个 ...