Maximum Product Subarray
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的更多相关文章
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关
Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...
- LeetCode Maximum Product Subarray(枚举)
LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...
- LeetCode_Maximum Subarray | Maximum Product Subarray
Maximum Subarray 一.题目描写叙述 就是求一个数组的最大子序列 二.思路及代码 首先我们想到暴力破解 public class Solution { public int maxSub ...
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
- 【刷题-LeetCode】152 Maximum Product Subarray
Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...
- 152. Maximum Product Subarray - LeetCode
Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...
- [LeetCode] Maximum Product Subarray 求最大子数组乘积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode]152. Maximum Product Subarray
This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...
随机推荐
- Servlet获取简单验证码
package com.helloweenvsfei.servlet; import java.awt.Color; import java.awt.Font; import java.awt.Gra ...
- Freemarker 之 Java静态化 实例一
Freemarker是一种强大的web端模板技术,在当前Web开发中,SEO和客户端浏览速度尤为重要,其中将网页静态化是一个很好的解决方案.下面介绍Java中web开发结合Freemarker来实现静 ...
- 德州扑克AI WEB版
继续之前的德州扑克话题,上次的DOS界面确实没法看,我女朋友说这是什么鬼.哈哈,估计只有自己能玩了 这两天重构了一下界面,基于web服务器和浏览器来交互. 服务器和客户端之间用websocket通信, ...
- easyVS
easyVS 详细说明点这里
- CSU 1113 Updating a Dictionary
传送门 Time Limit: 1000MS Memory Limit: 131072KB 64bit IO Format: %lld & %llu Description In th ...
- c++ struct的两个注意点
1.C++的结构体变量在声明的时候可以省略struct,在c中这样是不可以的,例子如下 #include<iostream> #include<string> using na ...
- POJ2823Sliding Window
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 49919 Accepted: 14391 ...
- UILable点击事件
UILabel *lLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 19, 105, 1)]; lLabel.backgroundColor ...
- Linux 磁盘分区
原文地址:http://www.jb51.net/LINUXjishu/57192.html 磁头数(Heads)表示硬盘总共有几个磁头,也就是有几面盘片, 最大为 255 (用 8 个二进制位存储) ...
- phpcms 调取全站文章
路径:phpcms/module/content/classes/content_tag.class.php 添加如下方法 /** * 列表页标签:主要返回的是主表中数据与附表中数据 * @param ...