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 ...
随机推荐
- [Xcode 实际操作]八、网络与多线程-(23)多线程的同步与异步的区别
目录:[Swift]Xcode实际操作 本文将演示线程的同步与异步的区别. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] 异步线程的运行,是没有按照顺序执行的. ...
- [Xcode 实际操作]九、实用进阶-(22)Storyboard故事板的常用布局结构
目录:[Swift]Xcode实际操作 本文将演示如在使用故事板搭建项目时,常用的一种故事板布局结构. 在项目导航区,打开故事板文件[Main.storyboard] 当前故事板中只有一个视图控制器控 ...
- Validation(1)
站在巨人的肩膀上 Java Bean Validation 最佳实践 参数校验是我们程序开发中必不可少的过程.用户在前端页面上填写表单时,前端js程序会校验参数的合法性,当数据到了后端,为了防止恶意操 ...
- JQuery获取iframe中window对象的方法-contentWindow
document.getElementsByTagName('iframe')[0].contentWindow 获取到的就是iframe中的window对象.
- Java并发编程面试题1
package com.mozq.thread.producer2; /* * 面试题2:以下代码是否存在错误 class ThreadTest implements Runnable{ public ...
- RDS 导出Mysqlbinlog_二进制日志
1:首先进入RDS后台查看Mysqlbin_log日志,可以通过以下命令进行查看: show master logs; 或 show binary logs; 2:比如我们获取的是:mysql-bin ...
- Codeforces 1142E(图、交互)
题目传送 官方题解说的很好了,剩下的就是读大佬代码了,前面是tarjan求SCC缩点图.我图论没学过,接下来删点是怎么操作看得有点头秃,直到我看到了%%%安德鲁何神仙的代码. 按照题面连通紫线以后,我 ...
- [WOJ3010] 骰子
题目描述: 骰子是一个六面分别刻有一到六点的立方体,每次投掷骰子,理论上得到\(1\)到\(6\)的概率都是\(1/6\). 有骰子一颗,连续投掷\(n\)次,问点数总和大于等于\(X\)的概率是多少 ...
- 095 Unique Binary Search Trees II 不同的二叉查找树 II
给出 n,问由 1...n 为节点组成的不同的二叉查找树有多少种?例如,给出 n = 3,则有 5 种不同形态的二叉查找树: 1 3 3 2 1 ...
- NUP2201MR
NUP2201MR:双总线保护IC(瞬态抑制二极管),常用于USB总线的保护.