Maximum Prodyct Subarray Leetcode Python
来源:互联网 发布:淘宝上的百度财富 编辑:程序博客网 时间:2024/05/29 11:01
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],
For example, given the array [2,3,-2,4],
the contiguous subarray [2,3] has the largest product = 6.
这道题目的解法是分别从左边和右边依次乘,如果得到的值大于maxproduct就将其赋值给maxproduct.
可以证明最大值不可能在中间产生。
we multiply from the left and right maintain the maximum value. We can see that the maximum value will not come from the middle.
the code is as follow
class Solution: # @param A, a list of integers # @return an integer def maxProduct(self, A): maxp=-100000 val=1 for index in range(len(A)): if val==0: val=1 val=val*A[index] if val>maxp: maxp=val val=1 for index in reversed(range(len(A))): if val==0: val=1 val=val*A[index] if val>maxp: maxp=val return maxp
0 0
- Maximum Prodyct Subarray Leetcode Python
- leetcode:Maximum Subarray---python
- Maximum Subarray Leetcode Python
- Leetcode-Maximum Subarray-Python
- 【Leetcode】【python】Maximum Subarray
- 【LeetCode with Python】 Maximum Subarray
- [leetcode:python]53.Maximum Subarray
- [Leetcode]152. Maximum Product Subarray @python
- 【LeetCode】Maximum Subarray 和 Maximum Product Subarray
- LeetCode: Maximum Subarray
- LeetCode Maximum Subarray
- [Leetcode] Maximum Subarray
- LeetCode: Maximum Subarray
- leetcode 25: Maximum Subarray
- [LeetCode] Maximum Subarray
- [Leetcode] Maximum Subarray
- [LeetCode]Maximum Subarray
- [leetcode]Maximum Subarray
- HTML5学习笔记【3】元素与格式化
- 55555555555
- Unity3D研究:如何更合理的架构你的游戏脚本
- Alex 的 Hadoop 菜鸟教程: 第11课 Hive的Java调用
- 【设计模式】-装饰者模式(Wrapper)
- Maximum Prodyct Subarray Leetcode Python
- 倒计时
- 瞬间击中以至于改变了吾等的价值观的自然科学理论。
- SZUOJ-Problem(J16):Bright stars最小生成树
- POJ 3728 离线 LCA
- hdu-1003-Max Sum
- hdu-2050-折线分割平面
- hdu-2200-Eddy's AC难题
- 我的2014——典型程序员的一年,不想再重来