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 ...
随机推荐
- PTA天梯赛校内模拟
最长对称子串 || 区间dp || 马拉车 dp[i][j]表示区间[i, j]是否为回文串,若是则为1,不是则为0. 边界条件: 1. 区间长度为1,dp为1.(奇数个字符递推的起始情况) 2. 区 ...
- VS Code C++ 项目快速配置模板
两个月前我写过一篇博客 Windows VS Code 配置 C/C++ 开发环境 ,主要介绍了在 VS Code 里跑简单 C 程序的一些方法.不过那篇文章里介绍的方法仅适用于单文件程序,所以稍微大 ...
- BootstrapBlazor 组件库介绍
项目介绍 演示系统地址:https://www.blazor.zone Blazor 是一个使用 .NET 生成交互式客户端 Web UI 的框架: 使用 C# 代替 JavaScript 来创建丰富 ...
- Spring Boot + MongoDB 使用示例
本文分别使用 MongoRepository 和 MongoTemplate 实现 MongoDB 的简单的增删改查 本文使用 docker 安装 MongoDB: 使用示例 application. ...
- DRF的ModelSerializer的使用
在views中添加 from django.shortcuts import render # Create your views here. from rest_framework.views im ...
- 微服务注册到Nacos的IP私网172.x.x.x网段无法访问的问题
解决方案一 显示声明注册服务实例的外网IP,默认就是使用私网的IP造成无法访问的,配置如下: spring: cloud: nacos: discovery: ip: 101.37.6.8 解决方案二 ...
- CentOS下Mysql的操作
重启Mysql的各种方法 1.通过rpm包安装的MySQL service mysqld restart /etc/inint.d/mysqld start 2.从源码包安装的MySQL // lin ...
- 第15.45节、PyQt输入部件:QKeySequenceEdit快捷键输入部件简介和使用案例
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 一.功能简介 Key Sequence Edit输 ...
- 转:Python2字符编码问题汇总
这篇文章的部分问题在Python3以后不再存在,老猿只是觉得文章的部分内容还是有参考价值,因此在此原文转发连接: Python2字符编码问题汇总
- Go语言的context包从放弃到入门
目录 一.Context包到底是干嘛用的 二.主协程退出通知子协程示例演示 主协程通知子协程退出 主协程通知有子协程,子协程又有多个子协程 三.Context包的核心接口和方法 context接口 e ...