46.Maximum Product Subarray(最大乘积子数组)
Level:
Medium
题目描述:
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product.
Example 1:
Input: [2,3,-2,4]
Output: 6
Explanation: [2,3] has the largest product 6.
Example 2:
Input: [-2,0,-1]
Output: 0
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
思路分析:
因为是乘积的最大值,可能是若干个正数相乘得到,也可能是若干个负数相乘得到;因此,必须保留前一阶段的最大值和最小值k+1在内结尾的最小乘积序列为:
min(k+1) = min(min(k) * a[k+1], max(k) * a[k+1], a[k+1])
代码:
public class Solution{
public int maxProduct(int []nums){
int dp=nums[0];
int maxk=nums[0]; //第K阶段的最大值
int mink=nums[0]; //第K阶段的最小值
int maxcur=maxk;
int mincur=mink;
for(int i=1;i<nums.length;i++){
maxk=Math.max(Math.max(maxcur*nums[i],mincur*nums[i]),nums[i]);
mink=Math.min(Math.min(maxcur*nums[i],mincur*nums[i]),nums[i]);
maxcur=maxk;
mincur=mink;
dp=Math.max(dp,maxcur);
}
return dp;
}
}
46.Maximum Product Subarray(最大乘积子数组)的更多相关文章
- 152. Maximum Product Subarray最大乘积子数组/是否连续
[抄题]: Given an integer array nums, find the contiguous subarray within an array (containing at least ...
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- [leetcode]152. Maximum Product Subarray最大乘积子数组
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- [LC]643题 Maximum Average Subarray I(子数组最大平均数 I)
①英文题目 Given an array consisting of n integers, find the contiguous subarray of given length k that h ...
- [LeetCode] Subarray Product Less Than K 子数组乘积小于K
Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...
- LeetCode:152_Maximum Product Subarray | 最大乘积连续子数组 | Medium
题目:Maximum Product Subarray Find the contiguous subarray within an array (containing at least one nu ...
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- LeetCode Maximum Product Subarray(枚举)
LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...
- LeetCode_Maximum Subarray | Maximum Product Subarray
Maximum Subarray 一.题目描写叙述 就是求一个数组的最大子序列 二.思路及代码 首先我们想到暴力破解 public class Solution { public int maxSub ...
随机推荐
- Asp.net后台创建HTML
为了使HTML界面中的内容能根据数据库中的内容动态显示用户需要的内容,或者根据权限不同要显示同而实现页面内容的动态创建 使用HtmlGenericControl创建HTML标签 引入命名空间: usi ...
- 洛谷 - P2158 - 仪仗队 - 欧拉函数
https://www.luogu.org/problemnew/show/P2158 好像以前有个妹子收割铲也是欧拉函数. 因为格点直线上的点,dx与dy的gcd相同,画个图就觉得是欧拉函数.但是要 ...
- 阻塞调用ShellExecute函数
SHELLEXECUTEINFO si;ZeroMemory(&si, sizeof(si));si.cbSize = sizeof(si);si.fMask = SEE_MASK_NOCLO ...
- C# interface 的特性 无法被implement class继承
最近做interface添加特性后,implement class 无法继承. 微软要求class是实现Interface而不是继承,所以我们必须手动添加特性,而不能自动继承. 对于abstract ...
- Mysql 到 Hbase 数据如何实时同步,强大的 Streamsets 告诉你
很多情况大数据集群需要获取业务数据,用于分析.通常有两种方式: 业务直接或间接写入的方式 业务的关系型数据库同步到大数据集群的方式 第一种可以是在业务中编写代码,将觉得需要发送的数据发送到消息队列,最 ...
- 如何利用python制作微信好友头像照片墙?
这个不难,主要用到itchat和pillow这2个库,其中itchat用于获取微信好友头像照片,pillow用于拼接头像生成一个照片墙,下面我简单介绍一下实现过程,代码量不多,也很好理解,实验环境wi ...
- spring boot 配置https 报这个错误:java.lang.IllegalArgumentException: Private key must be accompanied by certificate chain
找了接近半天的时间,原来是那么小的问题 server.ssl.key-store=test.jksserver.ssl.key-store-password=123456server.ssl.key- ...
- Verify the Developer App certificate for youraccount is trusted on your device
运行时报错-Verify the Developer App certificate for youraccountis trusted on your device. Open Settings ...
- [题解](次短路)luogu_P2865路障(未)
好像是个不需要vis数组的次短路,跑到收敛,然而给我脑袋弄炸了......到现在还没懂.......究竟次短路应该怎么求a...... 抄题解: #include<bits/stdc++.h&g ...
- matplotlib 学习笔记02:marker标记详解
本文内容来自于matplotlib官网:matplotlib官网markers资料 This module contains functions to handle markers. Used by ...