Find the contiguous subarray within an array (containing at least one number) which has the largest product.

Example

For example, given the array [2,3,-2,4], the contiguous subarray[2,3] has the largest product = 6.

分析:

因为这题要求乘积最大,而负数和负数相乘可是是正数,所以,我们必须得保存两个变量,即当到达A[i]时,前一个数A[i - 1]所对应的最大值和最小值max[i - 1] 和 min[i - 1]. max[i] 和min[i]和这些值有如下关系:

min[i] = min(nums[i], min[i - 1] * nums[i], max[i - 1] * nums[i]);
max[i] = max(nums[i], min[i - 1] * nums[i], max[i - 1] * nums[i]);

 public class Solution {
/**
* @param nums: an array of integers
* @return: an integer
* cnblogs.com/beiyeqingteng/
*/
public int maxProduct(int[] nums) {
if (nums == null || nums.length == ) return ;
if (nums.length == ) return nums[]; int[] min = new int[nums.length];
int[] max = new int[nums.length]; min[] = nums[];
max[] = nums[]; for (int i = ; i < nums.length; i++) {
min[i] = min(nums[i], min[i - ] * nums[i], max[i - ] * nums[i]);
max[i] = max(nums[i], min[i - ] * nums[i], max[i - ] * nums[i]);
} int tempMax = max[];
for (int i = ; i < max.length; i++) {
if (tempMax < max[i]) {
tempMax = max[i];
}
}
return tempMax; } public int max(int a, int b, int c) {
return Math.max(a, Math.max(b, c));
} public int min(int a, int b, int c) {
return Math.min(a, Math.min(b, c));
}
}

更简洁的做法:

public class Solution {
/**
* @param nums: an array of integers
* @return: an integer
* cnblogs.com/beiyeqingteng/
*/
public int maxProduct(int[] nums) {
if (nums == null || nums.length == ) return ;
if (nums.length == ) return nums[]; int pre_temp_min = nums[];
int pre_temp_max = nums[];
int global_max = nums[]; for (int i = ; i < nums.length; i++) {
int temp_min = min(nums[i], pre_temp_min * nums[i], pre_temp_max * nums[i]);
int temp_max = max(nums[i], pre_temp_min * nums[i], pre_temp_max * nums[i]);
pre_temp_min = temp_min;
pre_temp_max = temp_max;
global_max = Math.max(global_max, temp_max);
}
return global_max;
} public int max(int a, int b, int c) {
return Math.max(a, Math.max(b, c));
} public int min(int a, int b, int c) {
return Math.min(a, Math.min(b, c));
}
}

转载请注明出处:cnblogs.com/beiyeqingteng/

Maximum Product Subarray的更多相关文章

  1. 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray

    题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...

  2. LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关

    Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...

  3. LeetCode Maximum Product Subarray(枚举)

    LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...

  4. LeetCode_Maximum Subarray | Maximum Product Subarray

    Maximum Subarray 一.题目描写叙述 就是求一个数组的最大子序列 二.思路及代码 首先我们想到暴力破解 public class Solution { public int maxSub ...

  5. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

  6. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

  7. 【刷题-LeetCode】152 Maximum Product Subarray

    Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...

  8. 152. Maximum Product Subarray - LeetCode

    Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...

  9. [LeetCode] Maximum Product Subarray 求最大子数组乘积

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  10. [LeetCode]152. Maximum Product Subarray

    This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...

随机推荐

  1. win8 配 jdk

    Win8配置jdk 1.7环境变量  环境:win8(32位)64位差不多       jdk1.7    1.右击计算机-属性-高级系统设置-高级-环境变量,弹出“环境变量”对话框,主要是改下面的环 ...

  2. oracle练习题

    题干:设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher). 建表后数据如下: SQL> select * from ...

  3. bootstrap fileinput添加上传成功回调事件

    国外牛人做的bootstrap fileinput挺酷的,但是可惜没有提供自定义上传成功回调事件的接口,因此感到非常头疼,但是很幸运的是,我在网上搜索到一个提问帖子,它问到使用Jquery的on函数绑 ...

  4. Java-ArrayList和Vector的区别

    这两个类都实现了List接口(List接口继承了Collection接口),他们都是有序集合,即存储在这两个集合中的元素的位置都是有顺序的,相当于一种动态的数组,我们以后可以按位置索引号取出某个元素, ...

  5. 【 CodeForces 604A】B - 特别水的题2-Uncowed Forces

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102271#problem/B Description Kevin Sun has jus ...

  6. 【CodeForces 618B】Guess the Permutation

    题 题意 有个1到n的一个全排列,告诉你第i个数和全部n个数相比的较小的是哪个,和自己相比时为0,于是有个主对角线为0的矩阵,求原数列 分析 我的想法是,给我们的每一行之和按大小排一下,就知道第i个数 ...

  7. 直接运行可执行文件linux终端一闪而过

    运行elasticsearch的时候进入bin目录,ela 然后tab提示的内容中没有e..s..,很奇怪,然后我直接双击运行es,终端一闪而过,我就手动打开终端, ./elasticsearch 这 ...

  8. OpenJudge 7627 鸡蛋的硬度

    描述 最近XX公司举办了一个奇怪的比赛:鸡蛋硬度之王争霸赛.参赛者是来自世 界各地的母鸡,比赛的内容是看谁下的蛋最硬,更奇怪的是XX公司并不使用什么精密仪器来测量蛋的硬度,他们采用了一种最老土的办法- ...

  9. TYVJ P2002 扑克牌

    背景 Admin生日那天,Rainbow来找Admin玩扑克牌……玩着玩着Rainbow觉得太没意思了,于是决定给Admin一个考验~~~ 描述 Rainbow把一副扑克牌(54张)随机洗开,倒扣着放 ...

  10. 在excel批量更改单元格类型后的批量刷新显示

    把E的东西变成完整显示 解决办法: 选中所需要更改的整列数据------>菜单栏的数据选项------>分列