MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / Solution

Class Solution

LeetCode_problems/Maximum Product Subarray/solution.py:6–35  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4# continuous.
5
6class Solution:
7 def maxProduct(self, nums: List[int]) -> int:
8
9 if len(nums) == 1:
10 return nums[0]
11
12 max_product = nums[0]
13 current_max = nums[0]
14 current_min = nums[0]
15
16 # For every number in list from 1st index
17 for num in nums[1:]:
18
19 # We update the current_max and current_min value as below
20 # This checks all combinations of products of sub-array.
21
22 # We store the value of current_max in a temporary variable
23 # as current_max would have to be updated
24 temp_max = current_max
25
26 current_max = max(num, current_max*num, current_min*num)
27 current_min = min(num, temp_max*num, current_min*num)
28
29 # If the value of current_max is more that max_product
30 # we update the value of max_product
31 if current_max > max_product:
32 max_product = current_max
33
34 # At the end we return the value max_product
35 return max_product

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected