152. Maximum Product Subarray
2016-02-27 03:02
295 查看
Find the contiguous subarray within an array (containing at least one number) which has the largest product.
For example, given the array
the contiguous subarray
Solution 1
public static int maxProduct(int[] nums) {
if (nums.length == 0) {
return 0;
}
int maxherepre = nums[0];
int minherepre = nums[0];
int maxsofar = nums[0];
int maxhere, minhere;
for (int i = 1; i < nums.length; i++) {
maxhere = Math.max(Math.max(maxherepre * nums[i], minherepre * nums[i]), nums[i]);
minhere = Math.min(Math.min(maxherepre * nums[i], minherepre * nums[i]), nums[i]);
maxsofar = Math.max(maxhere, maxsofar);
maxherepre = maxhere;
minherepre = minhere;
}
return maxsofar;
}
Solution 2 Same as above
public int maxProduct3(int[] nums) {
int result = nums[0];
int max = nums[0];
int min = nums[0];
for(int i = 1; i < nums.length; i++){
int temp = max;
max = Math.max(Math.max(max * nums[i], min * nums[i]), nums[i]);
min = Math.min(Math.min(temp * nums[i], min * nums[i]), nums[i]);
if(max > result){
result = max;
}
}
return result;
}
Solution 3
public static int maxProduct2(int[] nums) {
int n = nums.length;
int r = nums[0];
for (int i = 1, imax = r, imin = r; i < n; i++) {
if(nums[i] < 0){
int temp = imax;
imax = imin;
imin = temp;
}
imax = Math.max(nums[i], imax * nums[i]);
imin = Math.min(nums[i], imin * nums[i]);
r = Math.max(r, imax);
}
return r;
}
For example, given the array
[2,3,-2,4],
the contiguous subarray
[2,3]has the largest product =
6.
Solution 1
public static int maxProduct(int[] nums) {
if (nums.length == 0) {
return 0;
}
int maxherepre = nums[0];
int minherepre = nums[0];
int maxsofar = nums[0];
int maxhere, minhere;
for (int i = 1; i < nums.length; i++) {
maxhere = Math.max(Math.max(maxherepre * nums[i], minherepre * nums[i]), nums[i]);
minhere = Math.min(Math.min(maxherepre * nums[i], minherepre * nums[i]), nums[i]);
maxsofar = Math.max(maxhere, maxsofar);
maxherepre = maxhere;
minherepre = minhere;
}
return maxsofar;
}
Solution 2 Same as above
public int maxProduct3(int[] nums) {
int result = nums[0];
int max = nums[0];
int min = nums[0];
for(int i = 1; i < nums.length; i++){
int temp = max;
max = Math.max(Math.max(max * nums[i], min * nums[i]), nums[i]);
min = Math.min(Math.min(temp * nums[i], min * nums[i]), nums[i]);
if(max > result){
result = max;
}
}
return result;
}
Solution 3
public static int maxProduct2(int[] nums) {
int n = nums.length;
int r = nums[0];
for (int i = 1, imax = r, imin = r; i < n; i++) {
if(nums[i] < 0){
int temp = imax;
imax = imin;
imin = temp;
}
imax = Math.max(nums[i], imax * nums[i]);
imin = Math.min(nums[i], imin * nums[i]);
r = Math.max(r, imax);
}
return r;
}
相关文章推荐
- java 利用*号打印三角形
- 产品前端重构(TypeScript、MVC框架设计)
- GlobalValue
- Simulator,Scheduler和时间
- Simulator相关
- 《大话数据结构》之快速排序
- Scheduler相关
- SimulatorImpl
- 《UNIX 高手的 10 个习惯》学习记录整理
- 《UNIX 高手的 10 个习惯》学习记录整理
- IEEE font, matlab eps图片 font Helvetica, not embedded
- 碰撞检测之Ray-Capsule检测
- [kaggle实战] Digit Recognizer -- 从KNN,LR,SVM,RF到深度学习
- C#进行AutoCAD二次开发实例之倒角功能实现原理介绍
- cordova开发ios时候getScript函数无效的解决办法
- 设计模式——抽象工厂
- zookeeper的学习小结
- Python基础0227
- POJ 3637 解题报告
- Spring AOP学习笔记(2):AOP前置通知&后置通知