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.

题意:给出一个数组,求其最大子数组的乘积

思路:动态规划法,因为在数组中可能存在负数,因为既要计算最小值 ,又要计算最大值。

用minValue(n) 表示从0到n得到的最小子数组乘积,用maxValue(n)表示从0到n得到的最大子数组乘积

状态转移方程为dp(n) = max{minValue(n-1)*array[n], maxValue(n-1)*array[n], array[n]}

代码如下:

class Solution {public int maxProduct(int[] nums){int minValue = 1, maxValue = 1, productValue = Integer.MIN_VALUE;int len = nums.length;for (int i = 0; i < len; i++){int tmpMax = Math.max(minValue * nums[i], Math.max(maxValue * nums[i], nums[i]));int tmpMin  = Math.min(minValue * nums[i], Math.min(maxValue * nums[i], nums[i]));maxValue = tmpMax;minValue = tmpMin;productValue = Math.max(productValue, maxValue);}return productValue;}
}

LeetCode Maximum Product Subarray(最大子数组乘积)相关推荐

  1. LeetCode Maximum Product Subarray 解题报告

    LeetCode 新题又更新了.求:最大子数组乘积. https://oj.leetcode.com/problems/maximum-product-subarray/ 题目分析:求一个数组,连续子 ...

  2. LeetCode: Maximum Product Subarray

    很久没练只能看别人代码了 1 class Solution { 2 public: 3 int maxProduct(int A[], int n) { 4 if (n == 0) return 0; ...

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

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

  4. LeetCode 152. Maximum Product Subarray

    152. Maximum Product Subarray Find the contiguous subarray within an array (containing at least one ...

  5. 子数组的最大乘积 Maximum Product Subarray

    为什么80%的码农都做不了架构师?>>>    问题: Find the contiguous subarray within an array (containing at lea ...

  6. [LeetCode] Maximum Subarray 最大子数组

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  7. 【LeetCode】152. Maximum Product Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  8. LeetCode Maximum Product of Word Lengths(位操作)

    问题:给出一个字符串数组,要求求出两个没有共同字符的字符串的最大积 思路:第一种方法是直接枚举任意两个字符串,看是否有公共字符,如果没有,则计算乘积,并更新最大值. 第二种方法,因为字符范围是a-z, ...

  9. Maximum Product Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

最新文章

  1. 某大龄程序员哀叹:夫妻双失业后还不起房贷,被迫断供!四年还了80万,60多万都是利息!...
  2. [kuangbin] M - Find a way(简单广搜)
  3. 【线段树】海报(loj 3264)
  4. react textarea 空格为什么不换行_你需要的 React + TypeScript 50 条规范和经验
  5. C++学习笔记-利用rapidJSON生成JSON数据
  6. 计算机辅助设计的概念.doc,计算机辅助设计的基础知识.DOC
  7. yum install -y iptraf 显示找不到命令
  8. java excel条件格式_Java 使用条件格式高亮Excel单元格
  9. java爬虫入门--用jsoup爬取汽车之家的新闻
  10. java 有序容器_Java 容器集合框架概览
  11. mysql的匹配模式
  12. 简易计算器--带小数点--单片机课程设计
  13. Python开发基础----数据类型----[列表]
  14. ResourceExhaustedError
  15. 5G核心网技术基础自学系列 | 与EPC互通
  16. Time时间格式化当前时间
  17. 关于网线水晶头的接法详解(2)
  18. 2020年计算机科学与技术学校排名,2020年全国计算机科学与技术专业大学排名
  19. 《MLB棒球创造营》:走近棒球运动·明尼苏达双城队
  20. 手臂疼,竟是它惹的祸?这些征兆你想都想不到

热门文章

  1. Qt全局热键(windows篇)
  2. 根据一个大图片自动生成相应小图片的代码
  3. 没有统计学基础可以学python-机器学习和Python怎样快速掌握?你一定需要这27张清单...
  4. 开课吧python小课值得么-领导想提拔你,从来看的不止努力!
  5. python装饰器-Python——装饰器(Decorator)
  6. python3.6安装scrapy-win7安装python3.6.1及scrapy
  7. python爬虫流程-Python爬虫入门
  8. 0基础学python-看完这篇,零基础也知道怎么学Python
  9. python语言入门教程-Python语言十分钟快速入门
  10. python写小程序-你用python写过那些好玩的微信小程序?