152. Maximum Product Subarray动态规划连乘最大子串
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动态规划连乘最大子串的更多相关文章
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
- 152. Maximum Product Subarray - LeetCode
Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...
- 【刷题-LeetCode】152 Maximum Product Subarray
Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- [LeetCode]152. Maximum Product Subarray
This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...
- LeetCode 152. Maximum Product Subarray (最大乘积子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- Leetcode#152 Maximum Product Subarray
原题地址 简单动态规划,跟最大子串和类似. 一维状态空间可以经过压缩变成常数空间. 代码: int maxProduct(int A[], int n) { ) ; ]; ]; ]; ; i > ...
- 152. Maximum Product Subarray(动态规划)
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
随机推荐
- Spring Boot + Elasticsearch 使用示例
本文分别使用 Elasticsearch Repository 和 ElasticsearchTemplate 实现 Elasticsearch 的简单的增删改查 一.Elastic Stack El ...
- 在django中使用原生sql语句
raw # row方法:(掺杂着原生sql和orm来执行的操作) res = CookBook.objects.raw('select id as nid from epos_cookbook whe ...
- vue上传视屏或者图片到七牛云
首先下载七牛云的JavaScript-SDK npm install qiniu-js 下载完成JavaScript-SDK以后就可以上传图片信息了 <template> <div& ...
- Python正则表达式re.findall一个有趣的现象
下面通过几个案例来分析一下, 注意:本节的parsematch函数请参考<妙用re.sub分析正则表达式解析匹配过程> 案例一: >>> re.findall(r&quo ...
- 第十五章、Model/View架构中Item Views部件的父类
老猿Python博文目录 老猿Python博客地址 引言:本章早就写好了,其简版<第15.18节 PyQt(Python+Qt)入门学习:Model/View架构中视图Item Views父类详 ...
- swpuCTF2019 web1 无列名注入
上周参加的swpuctf比赛第一道web题做了好久,在最后一个小时用非预期的方法做出来了,看了官方题解之后记录一下wp里面的无列名注入. 关于无列名注入可以看一下这篇链接 https://www.ch ...
- 一文看懂 Kubernetes 服务发现: Service
Service 简介 K8s 中提供微服务的实体是 Pod,Pod 在创建时 docker engine 会为 pod 分配 ip,"外部"流量通过访问该 ip 获取微服务.但 ...
- CF1147F Zigzag Game & 稳定婚姻问题学习笔记
CF1147F Zigzag Game 这题太神仙了,不得不记录一下. 我网络流做不动了,DS做不动了,DP做不动了,特别自闭.于是博弈论之神(就是随手切3500博弈的那种) \(\color{bla ...
- window+nginx+php
今天在Windows上配置了下nginx,看了不少其他大牛们记录的博客,自己也操作了一番,记录一下备忘. nginx download: http://nginx.org/en/download.ht ...
- celery 原理和组件
Celery介绍 https://www.cnblogs.com/xiaonq/p/11166235.html#i1 1.1 celery应用举例 Celery 是一个 基于python开发的分布式异 ...