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(最大乘积子数组)的更多相关文章

  1. 152. Maximum Product Subarray最大乘积子数组/是否连续

    [抄题]: Given an integer array nums, find the contiguous subarray within an array (containing at least ...

  2. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

  3. [leetcode]152. Maximum Product Subarray最大乘积子数组

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  4. [LC]643题 Maximum Average Subarray I(子数组最大平均数 I)

    ①英文题目 Given an array consisting of n integers, find the contiguous subarray of given length k that h ...

  5. [LeetCode] Subarray Product Less Than K 子数组乘积小于K

    Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...

  6. LeetCode:152_Maximum Product Subarray | 最大乘积连续子数组 | Medium

    题目:Maximum Product Subarray Find the contiguous subarray within an array (containing at least one nu ...

  7. 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray

    题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...

  8. LeetCode Maximum Product Subarray(枚举)

    LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...

  9. LeetCode_Maximum Subarray | Maximum Product Subarray

    Maximum Subarray 一.题目描写叙述 就是求一个数组的最大子序列 二.思路及代码 首先我们想到暴力破解 public class Solution { public int maxSub ...

随机推荐

  1. POJ 3262 Protecting the Flowers 贪心(性价比)

    Protecting the Flowers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7812   Accepted: ...

  2. Asp.net后台创建HTML

    为了使HTML界面中的内容能根据数据库中的内容动态显示用户需要的内容,或者根据权限不同要显示同而实现页面内容的动态创建 使用HtmlGenericControl创建HTML标签 引入命名空间: usi ...

  3. PostgreSQL 务实应用(一/5)树形层级

    项目中,经常会碰到多级的树形结构数据,如地区信息,省.市.区.街道等,或客户关系信息上三级,下三级等. 实际项目中,我们可能碰到以下两种需求: 一条记录中呈现路径:省 - 市 - 区 - 街道 一条记 ...

  4. 详细分享UICollectionView的自定义布局(瀑布流, 线性, 圆形...)

    前言: 本篇文章不是分享collectionView的详细使用教程, 而是属于比较'高级'的collectionView使用技巧, 阅读之前, 我想你已经很熟悉collectionView的基本使用, ...

  5. 336. Palindrome Pairs(can't understand)

    Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that t ...

  6. AVAudioPlayer 如何在页面呈现之后按需初始化

    在页面中按需初始化 AVAudioPlayer 很多时候我们需要根据页面上内容的情况创建 AVAudioPlayer 对象,已达到降低无谓资源占用等目的.下面我们来看一段代码看起来正确的代码: ove ...

  7. 即时通讯新手入门:一文读懂什么是Nginx?它能否实现IM的负载均衡?

    本文引用了“蔷薇Nina”的“Nginx 相关介绍(Nginx是什么?能干嘛?)”一文部分内容,感谢作者的无私分享. 1.引言   Nginx(及其衍生产品)是目前被大量使用的服务端反向代理和负载均衡 ...

  8. 第一篇 Nosql讲解之Redis,Memchche,MongoDb的区别

    本篇文章主要介绍Nosql的一些东西,以及Nosql中比较火的三个数据库Redis.Memchache.MongoDb和他们之间的区别.以下是本文章的阅读目录 一.Nosql介绍 1.Nosql简介 ...

  9. swift SqliteDB使用

    操作步骤: 1,在 Build Phases -> Link Binary With Libraries 中点击加号,添加 libsqlite3.0.tbd 到项目中来   2,创建连接头文件B ...

  10. VLAN-2-私有VLAN

          好的设计方式通常要求工程师为每个vlan使用一个ip子网.然而在有些情况下,将设备分割到许多小VLAN中以增加安全性的需求,与节省可用子网的目标相互冲突.通过使用私有vlan,交换机能够分 ...