Maximum Product Subarray
来源:互联网 发布:java内部类 编辑:程序博客网 时间:2024/06/06 17:38
Q:
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
.
Solution:
public class Solution { public int maxProduct(int[] A) { if(A.length == 1) return A[0]; int min = A[0]; int max = A[0]; int globalMax = max; for (int i = 1; i < A.length; i++) { int copy = max; max = Math.max(Math.max(A[i]*max, A[i]*min), A[i]); min = Math.min(Math.min(A[i]*copy, A[i]*min), A[i]); globalMax = Math.max(globalMax, max); } return globalMax; }}
0 0
- LeetCode_Maximum Subarray | Maximum Product Subarray
- Maximum Product Subarray
- Maximum Product Subarray
- Maximum Product Subarray 【LeetCode】
- [LeetCode] Maximum Product Subarray
- leetcode-Maximum Product Subarray
- Maximum Product Subarray -- LeetCode
- LeetCode-Maximum Product Subarray
- [LeetCode]Maximum Product Subarray
- [LeetCode]Maximum Product Subarray
- 【LeetCode】Maximum Product Subarray
- leetcode:Maximum Product Subarray
- leetcode: Maximum Product Subarray
- Leetcode: Maximum Product Subarray
- Maximum Product Subarray
- Maximum Product Subarray
- [LeetCode]Maximum Product Subarray
- Leetcode Maximum Product Subarray
- FragmentTransaction add 和 replace 区别
- JavaWeb下载文件
- android textview no focus
- Android Studio如何设置代码自动提示
- SpringSecurity3.2y启动报Exception starting filter springSecurityFilter错误解决办法
- Maximum Product Subarray
- ADB
- hdu_1698Just a Hook线段树_区间修改
- 虚析构函数作用(代码)
- vmware10中开启Intel VT-x
- slidingMenu使用API
- EJB实体Bean如何和数据库中表关联?
- xcode搜索方法调用者快捷键
- java反射自我总结