标签:

动态规划

描述:

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. shell 版本号比较_用shell如何比较软件版本号的大小

    比如你想写个脚本来比较两个版本号 (如"1.2.30" 和"1.3.0")来辨认哪个版本是最新的,有可以比较两个版本号字符串的shell脚本吗? 当你写了一个s ...

  2. git归纳总结

    1,git 是分布式版本控制:单个电脑就是一个完整的版本库,只需向中央服务器(远程服务器)提交修改的部分.在没有网络情况下也能正常想本机服务器提交代码管理版本,有网时再推送到远程服务器.   svn是 ...

  3. ActiveMQ 传输协议

    配置 ActiveMQ安装目录的conf/activemq.xml中的<transportConnectors>标签之内. 配置示例 TCP(默认协议,性能相对可靠) Transmissi ...

  4. WebService Exceptions

    一. Exception in thread "main" java.lang.ExceptionInInitializerError at com.sun.xml.interna ...

  5. PAT甲级——A1088 Rational Arithmetic

    For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...

  6. Axure中表格使用的技巧

    对于新手来说,用Axure做一个表格是一件麻烦的事情.本文教你如何快速学会Axure表格的基础应用. (1)Axure制作基本表格的使用 可以使用“线框图”中的“表格”控件来制作一些简单的表格,同时A ...

  7. [原创]Java调用PageOffice给Word中的Table赋值

    Word中的table操作需要借助数据区域(DataRegion)实现的,要求数据区域完整的包含了整个Table的内容,这样才可以通过数据区域控制和操作table.因此,要想使用table,则必须在w ...

  8. Java - 关于覆盖和重写的总结

    公众号偶然看到的一个帖子,构造方法,类方法,final方法,哪些能覆盖,哪些能重载,初学时也是被这些术语搞的很迷糊 现在有时间了对这些做一个总结.全是自己的语言,可能不是很全面,表达意思应该够清楚 一 ...

  9. jmeter命令行压测

    简介:使用非GUI模式,即命令行模式运行jmeter测试脚本能够大大缩减系统资源 1.配置jdk及添加环境变量 变量名:JAVA_HOME 变量值: C:\Program Files\Java\jdk ...

  10. MFC编译Freetype2.3.7

    从http://www.freetype.org下载源代码. FreeType2库源码包中包含多种环境与编译器下的make文件,其中还包含vc的项目文件. 我用的是VC,所以首先找到VC环境的项目文件 ...