Leetcode 152 Maximum Product Subarray
来源:互联网 发布:mac aecc2017切换中文 编辑:程序博客网 时间:2024/06/06 18:13
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
.
同样使dp的问题,但是值得注意的是,负数和负数相乘有可能是是个大的结果
于是min和max都要存下来
public class Solution { public int maxProduct(int[] nums) { if(nums == null){ return 0; } int result = nums[0]; int min = nums[0]; int max = nums[0]; for(int i = 1; i < nums.length; i++){ int temp = min; min = Math.min(nums[i], Math.min(nums[i] * min, nums[i]*max)); max = Math.max(nums[i], Math.max(nums[i] * temp, nums[i]*max)); result = result > max ? result : max; } return result; } }
阅读全文
0 0
- Leetcode(152) Maximum Product Subarray
- leetcode 152: Maximum Product Subarray
- [leetcode 152] Maximum Product Subarray
- [leetcode] 152 Maximum Product Subarray
- leetcode-152 Maximum Product Subarray
- Maximum Product Subarray - LeetCode 152
- LeetCode---(152)Maximum Product Subarray
- Leetcode 152 Maximum Product Subarray
- LeetCode(152) Maximum Product Subarray
- leetcode 152: Maximum Product Subarray
- leetcode 152 Maximum Product Subarray
- [leetcode] 152 Maximum Product Subarray
- LeetCode 152: Maximum Product Subarray
- LeetCode 152 Maximum Product Subarray
- LeetCode 152 Maximum Product Subarray
- LeetCode(152) Maximum Product Subarray
- Leetcode 152 Maximum Product Subarray
- leetcode 152 Maximum Product Subarray
- 通过adb与python结合创建的设备驱动脚本deviceDriver.py
- [转]有return的情况下try catch finally的执行顺序(最有说服力的总结)
- 重建二叉树
- 使用多张图片做帧动画的性能优化
- Mac构建Protobuf
- Leetcode 152 Maximum Product Subarray
- 贪心+并查集
- Fork and Join: Java也可以轻松地编写并发程序
- 170609 逆向-VB的自然编译和伪编译
- 用两个栈实现队列
- Spring_Demo1 使用XMl配置IOC
- 猫猫学swift之基础(一)
- BZOJ 4669 抢夺
- 旋转数组的最小数字