标签:

动态规划

描述:

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. C++ 系列:基础知识储备

    Copyright © 2000-2017, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ ----------------- ...

  2. LoadRunner中字符串的操作

    LoadRunner中字符串的操作 LoadRunner中常用的字符串操作函数有:                strcpy(destination_string, source_string); ...

  3. 基于MFC的实时可视化项目中视图刷新消息的严谨使用

    在实时可视项目中,视图的实时刷新显示对软件的体验感来说非常重要,当算法的效率达到实时,比如一秒40帧,如果实时显示帧率更不上,则体验感将大大折扣,让用户感觉你的算法并没有40帧,当然最关键的是解决显示 ...

  4. 前端面试题之一JAVASCRIPT(算法类)

    一.JS操作获取和设置cookie //创建cookie function setcookie(name, value, expires, path, domain, secure) { var co ...

  5. iOS之CGcontext.h方法和属性简介

    /* CoreGraphics - CGContext.h Copyright (c) 2000-2012 Apple Inc. All rights reserved. */ #ifndef CGC ...

  6. 机器学习(二)数据处理&相似/异性度量

    机器学习(二)数据处理&相似/异性度量 https://woaielf.github.io/2017/03/17/dm-2/ 2017-03-17 ZOE    数据科学  机器学习/数据挖掘 ...

  7. Java对象的存活判断

    Java对象的回收是有依据的,普通的方式是计算法,比如被引用,对象计数器➕1,不被引用,➖1,回收的时候,发现是0则清除,但是不能解决两个对象互相被引用的情况,Java采用可达性分析,通过某个对象作为 ...

  8. Spring注解驱动(下)

    9.@PropertySource 加载配置文件 在xml中 我们加载property配置文件,是使用 <context:property-placeholder location=" ...

  9. MySQL中修改多个数据表的字段拼接问题

    错误1: 异常:Truncated incorrect DOUBLE value: 'lili' 问题分析:我的修改sql语句是:update video set vname='汉字' and vdi ...

  10. linux为内核新增系统调用

    1.编写hello.c文件,实现系统调用执行函数,如: asmlinkage long sys_helloworld(void){ printk( "helloworld!"); ...