标签:

动态规划

描述:

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. Ubuntu 安装gnome桌面及vnc远程连接

    安装gnome桌面 sudo apt-get install gnome-core 安装vnc sudo apt-get install vnc4server 启动vnc vncserver 设置一下 ...

  2. PAT甲级——A1006 Sign In and Sign Out

    At the beginning of every day, the first person who signs in the computer room will unlock the door, ...

  3. 如何学习AxureRP Axure学习方法

    从作者最初接触的5.5版本,到5.6版本,到后来6.0的多个迭代版本,直到现在的6.5版本,AxureRP每次的版本升级都伴随着新功能的增 加,也解决了原型设计上的一些难题.这也从另一个方面诠释了“学 ...

  4. 2019-8-31-dotnet-手动解决-json-解析中不合法字符串

    title author date CreateTime categories dotnet 手动解决 json 解析中不合法字符串 lindexi 2019-08-31 16:55:58 +0800 ...

  5. 关于CoCreateInstance的0x800401f0问题

    hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IGraphBuilder, (void **)&g_pGr ...

  6. 跟我一起在ubuntu中安装docker

    卸载旧版本 $ sudo apt-get remove docker docker-engine docker.io 查看ubuntu版本 设置安装源 通过如下步骤,设置安装源仓库,这里我们使用阿里源 ...

  7. Elasticsearch系列(一)--入门

    Elasticsearch基于Lucene构建的开源搜索引擎,Java编写,提供restful API,支持横向拓展,能够完成海量数据处理. 应用场景: 1.海量数据分析引擎 2.站内搜索引擎 3.数 ...

  8. vue.js_09_vue-父子组件的传值方法

    1.父向子传递数据 1>定义一个父组件和一个子组件 2>父组件通过v-bind绑定传递的数据  :parentmsg="msg" 3>子组件需要通过 props: ...

  9. JS的同步和异步加载

    引言 JS的“加载”不能理解为下载,它是分为两个部分:下载,执行.默认的JS加载是同步的,因为浏览器需要一个稳定的DOM结构,而执行JS时可能会对DOM造成改变,所以在执行JS时一定会阻塞HTML的渲 ...

  10. js获取url链接中的域名部分

    用js提取出url中的域名(domain)部分,用split()函数就可以了. 因为一个正确的url必定是由http://或者是https://.domain.路径/参数组成,所以可以用split以/ ...