-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
""" | ||
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. | ||
""" | ||
__author__ = 'Daniel' | ||
|
||
|
||
class Solution(object): | ||
def maxProduct(self, nums): | ||
if not nums: | ||
return 0 | ||
|
||
n = len(nums) | ||
smallest = list(nums) | ||
largest = list(nums) | ||
maxa = nums[0] | ||
for i in xrange(1, n): | ||
v = nums[i] | ||
smallest[i] = min(v, smallest[i-1]*v, largest[i-1]*v) | ||
largest[i] = max(v, smallest[i-1]*v, largest[i-1]*v) | ||
maxa = max(maxa, largest[i]) | ||
|
||
return maxa | ||
|
||
|
||
if __name__ == "__main__": | ||
assert Solution().maxProduct([2, 3, -2, 4]) == 6 |