LintCode刷题笔记-- Maximum Product Subarray
标签:
动态规划
描述:
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的更多相关文章
- lintcode 中等题 :Maximum Product Subarray 最大连续乘积子序列
题目 乘积最大子序列 找出一个序列中乘积最大的连续子序列(至少包含一个数). 样例 比如, 序列 [2,3,-2,4] 中乘积最大的子序列为 [2,3] ,其乘积为6. 解题 法一:直接暴力求解 时 ...
- lintcode刷题笔记(一)
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...
- LintCode刷题笔记-- LongestCommonSquence
标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...
- LintCode刷题笔记-- PaintHouse 1&2
标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...
- LintCode刷题笔记-- Maximal Square
标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...
- LintCode刷题笔记-- Edit distance
标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...
- LintCode刷题笔记-- Distinct Subsequences
标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. ...
- LintCode刷题笔记-- BackpackIV
标签: 动态规划 描述: Given an integer array nums with all positive numbers and no duplicates, find the numbe ...
- LintCode刷题笔记-- BackpackIII
标签:动态规划 问题描述: Given n items with size Ai and value Vi, and a backpack with size m. What's the maximu ...
随机推荐
- [转]Java多线程
一.线程的生命周期及五种基本状态 关于Java中线程的生命周期,首先看一下下面这张较为经典的图: 上图中基本上囊括了Java中多线程各重要知识点.掌握了上图中的各知识点,Java中的多线程也就基本上掌 ...
- Joomla - 菜单系统 (从创建到前端页面显示的过程)
在 Joomla 中,菜单是最常用且重要的功能之一,一般用于承载页面内容和各内容间的切换.导航等,演绎着非常重要的角色: 一.新建菜单 进入后台,点击顶栏菜单 -> 菜单管理 -> 点击新 ...
- opencv-Mat数据类型及位数总结
转自:http://blog.sina.com.cn/s/blog_662c7859010105za.html 在OpenCV里面,许多数据结构为了达到內存使用的最优化,通常都会用它最小上限的空间来分 ...
- SPSS分析过程可自动化,你知道吗
SPSS分析过程可自动化,你知道吗 在使用SPSS的过程中,有时候会遇到重复进行相同分析操作的情况,或者分析过程很复杂的情况. 这时候我们多么希望SPSS能够记住上一次的分析步骤,不要让我们重复的去点 ...
- NYOJ--860 又见01背包(01背包)
题目http://acm.nyist.net/JudgeOnline/problem.php?pid=860 分析:题目和普通的01背包问题一样,但是唯一不同的是数据的特殊性. 如果10^9根本就开辟 ...
- ListCtrl使用指南
http://blog.csdn.net/bqw2008/article/details/2047489 Windows ListCtrl使用技巧1. ListCtrl 风格 LVS_IC ...
- hiveUDF的使用
在此自己总结下UDF的用法 1.首先最简单的UDF(普通用java扩充函数的方式,大多数简便函数可以用这个函数来实现,返回单个字段),其加强版UDGF据说对map一类数据类型有更好兼容,实现上略复杂 ...
- Liunx常用命令行(Ubuntu)
关闭防火墙的命令行: 1. 永久性生效 开启:chkconfig iptables on 关闭:chkconfig iptables off 2. 即时生效,重启后失效 开启:service ipta ...
- 嘴巴题7 BZOJ1426: 收集邮票
Time Limit: 1 Sec Memory Limit: 162 MB Submit: 546 Solved: 455 [Submit][Status][Discuss] Description ...
- bash之set命令
set命令是 Bash 脚本的重要环节,却常常被忽视,导致脚本的安全性和可维护性出问题.本文介绍它的基本用法,让你可以更安心地使用 Bash 脚本. 一.简介 我们知道,Bash 执行脚本的时候,会创 ...