标签:

动态规划

描述:

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.关于这一题,动态规划定义了两个数组来作为动态规划中来记录状态的数据结构,一个记录之前状态的最大值,另外一个记录之前状态的最小值。

2.对于当前状态,如果当前值为整数,那么最大值就是先前最大值与之相乘,如果当前值为负,那么最大值就是先前最小值与之相乘;

3.对于最小值正好与最大值相反。

相关代码:

public int maxProduct(int[] nums) {
// write your code here
int[] max = new int[nums.length];
int[] min = new int[nums.length];
max[0] = nums[0];
min[0] = nums[0];
int result = nums[0];
for(int i=1; i<nums.length; i++){
max[i] = nums[i];
min[i] = nums[i];
if(nums[i]>0){
max[i] = Math.max(max[i], nums[i]*max[i-1]);
min[i] = Math.min(min[i], nums[i]*min[i-1]);
}else if(nums[i]<0){
max[i] = Math.max(max[i], nums[i]*min[i-1]);
min[i] = Math.min(min[i], nums[i]*max[i-1]);
}
result = Math.max(result, max[i]);
}
return result;
}

LintCode刷题笔记-- Maximum Product Subarray的更多相关文章

  1. lintcode 中等题 :Maximum Product Subarray 最大连续乘积子序列

    题目 乘积最大子序列 找出一个序列中乘积最大的连续子序列(至少包含一个数). 样例 比如, 序列 [2,3,-2,4] 中乘积最大的子序列为 [2,3] ,其乘积为6. 解题  法一:直接暴力求解 时 ...

  2. lintcode刷题笔记(一)

    最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...

  3. LintCode刷题笔记-- LongestCommonSquence

    标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...

  4. LintCode刷题笔记-- PaintHouse 1&2

    标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...

  5. LintCode刷题笔记-- Maximal Square

    标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...

  6. LintCode刷题笔记-- Edit distance

    标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...

  7. LintCode刷题笔记-- Distinct Subsequences

    标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. ...

  8. LintCode刷题笔记-- BackpackIV

    标签: 动态规划 描述: Given an integer array nums with all positive numbers and no duplicates, find the numbe ...

  9. LintCode刷题笔记-- BackpackIII

    标签:动态规划 问题描述: Given n items with size Ai and value Vi, and a backpack with size m. What's the maximu ...

随机推荐

  1. 代码内存泄露检测工具(linux gcc + valrind)

    参考博客: https://www.cnblogs.com/wangkangluo1/archive/2011/07/20/2111248.html linux命令如下:valgrind --tool ...

  2. thinkphp 自动完成

    自动完成是ThinkPHP提供用来完成数据自动处理和过滤的方法,使用create方法创建数据对象的时候会自动完成数据处理. 因此,在ThinkPHP使用create方法来创建数据对象是更加安全的方式, ...

  3. ThinkPHP可以支持直接使用字符串作为查询条件

    ThinkPHP可以支持直接使用字符串作为查询条件,但是大多数情况推荐使用数组或者对象来作为查询条件,因为会更加安全. 大理石平台哪家好 一.使用字符串作为查询条件 这是最传统的方式,但是安全性不高, ...

  4. docker 安装 ElasticSearch

    docker pull docker.elastic.co/elasticsearch/elasticsearch:5.6.9 docker images docker run -e ES_JAVA_ ...

  5. centos6 php7 安装 memcache 和 memcached

    下载安装memcache 注意:官网的memcache包,暂时好像不支持php7.所以到下面地址下载memcache包,切换到php7分支 php7 memcache github 下载地址 官网下载 ...

  6. 19-10-29-Night-X

    布谷. 欢迎大家来不耻下问. 这里是正解不会,暴力写跪,乱搞鸡肋的某虻 . 好想放假啊!!!! 话说猫厂现在产量低迷…… ZJ一下: T1,三维偏序,只码了$\Theta(N^2)$暴力 T2,暴力愉 ...

  7. windows 可执行文件分析

    windows可执行文件是什么? 是具有PE文件格特性的文件,例如:.exe.dll.ocx等文件. 注:(这里只是让大家能明了一些,其实,可执行与否,和后缀没有什么关系,后缀只是windows方便管 ...

  8. Android开发随笔

    1.线性布局LinearLayout时,用到layout_weight权重的使用 控件的宽度(高度)=自身宽度(高度)+剩余空间的所占比例 剩余空间(可以为负值)=屏幕宽-所有控件宽度(高度)< ...

  9. 深入理解JVM(一)类加载器部分:双亲委派模型

    类加载器的父亲委托机制 在父亲委托机制中,各个类加载器按照父子关系形成了树形结构,除了根类加载器之外,其余的类加载器都有且只有一个父加载器. 先让最顶层可以加在的父加载器加栽(所有可加载的加载器中,处 ...

  10. 2006-2007 ACM-ICPC | POJ3380 POJ3384 POJ3385 水题题解

    // CF比赛链接:http://codeforces.com/gym/101650 // POJ链接:http://poj.org/searchproblem?field=source&ke ...