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. Alpha冲刺-第四次冲刺笔记

    Alpha冲刺-冲刺笔记 这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2018SE2 这个作业要求在哪里 https://edu.cnblogs. ...

  2. Beta冲刺随笔——Day_Seven

    这个作业属于哪个课程 软件工程 (福州大学至诚学院 - 计算机工程系) 这个作业要求在哪里 Beta 冲刺 这个作业的目标 团队进行Beta冲刺 作业正文 正文 其他参考文献 无 今日事今日毕 林涛: ...

  3. 第9.9节 Python文件随机读写定位操作方法seek

    类似于C语言,Python也提供了文件位置定位的操作方法seek. 一. 语法 seek(offset, whence=SEEK_SET) 语法释义: 1)offset :将文件当前操作位置移动偏移量 ...

  4. .NET 开源导入导出库 Magicodes.IE 2.5发布

    今天我们发布了2.5版本,这当然也离不开大家对Magicodes.IE的支持,今天我也是跟往常一样列举了该版本一些重要的更新内容. 当然也要说一下,在这个版本中我们设计了全新的LOGO Excel导出 ...

  5. Day5 【Scrum 冲刺博客】

    每日会议总结 昨天已完成的工作 方晓莹(PIPIYing) 搭建与后台对接的代理服务器 对接个人中心接口 方子茵(Laa-L):暂无 黄芯悦(Sheaxx) 完善投诉反馈页面 完善车位管理页面 舒雯钰 ...

  6. FHQ简要笔记

    前言 原文写于 XJ 集训day2 2020.1.19. 现在想想那时候连模板都还没写,只是刚刚理解就在那里瞎yy--之前果然还是太幼稚了. 今天刷训练指南发现全是 Treap 和 Splay ,不想 ...

  7. 【Codeforces 809E】Surprise me!(莫比乌斯反演 & 虚树)

    Description 给定一颗 \(n\) 个顶点的树,顶点 \(i\) 的权值为 \(a_i\).求: \[\frac{1}{n(n-1)}\sum_{i=1}^n\sum_{j=1}^n\var ...

  8. day106:MoFang:BUG:获取数据验证token是否过期&相册/相机取消头像无法显示&MongoDB

    目录 BUG1:前端在获取数据时,要检验token是否过期 BUG2:相册/相机取消后设置页面头像无法显示 MongoDB 1.MongoDB基本介绍 2.MongoDB安装 3.MongoDB:通用 ...

  9. asp.net-ajax使用-WebMethod使用

    1.js $.ajax({ type: "POST", contentType: "application/json", url: "activity ...

  10. .Net Core Excel导入导出神器Npoi.Mapper

    前言 我们在日常开发中对Excel的操作可能会比较频繁,好多功能都会涉及到Excel的操作.在.Net Core中大家可能使用Npoi比较多,这款软件功能也十分强大,而且接近原始编程.但是直接使用Np ...