imashutosh51

Maximum product subarray

Oct 8th, 2022 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.49 KB | None | 0 0
  1. # Maximum Product Subarray: The Simple Logic
  2. With **sums**, you just keep adding. With **products**, signs matter:
  3.  
  4. - A **negative × negative** becomes **positive** (a tiny number can suddenly become the biggest).
  5. - A **positive × negative** becomes **negative** (your biggest can suddenly become the smallest).
  6. - A **zero** wipes everything out.
  7. So the *smallest* (most negative) product so far can become the *largest* after one more negative number.
  8.  
  9. At every position, track **two** values for subarrays ending there:
  10. - `maxProd`: the biggest product ending here
  11. - `minProd`: the smallest (most negative) product ending here
  12.  
  13. For each new number `x`, there are three choices:
  14.  
  15. 1. Start fresh from `x` itself
  16. 2. Extend the old max: `x * maxProd`
  17. 3. Extend the old min: `x * minProd` (this helps when `x` is negative)
  18.  
  19. ```
  20. newMax = max(x, x * maxProd, x * minProd)
  21. newMin = min(x, x * maxProd, x * minProd)
  22. ```
  23.  
  24. Keep a global `answer` = the largest `maxProd` seen at any step.
  25.  
  26. ## Dry run: `[2, 3, -2, 4]`
  27.  
  28. | x | maxProd | minProd | answer |
  29. |---|---------|---------|--------|
  30. | 2 | 2 | 2 | 2 |
  31. | 3 | 6 | 3 | 6 |
  32. | -2 | -2 | -12 | 6 |
  33. | 4 | 4 | -48 | 6 |
  34.  
  35. Answer: **6** (subarray `[2, 3]`).
  36.  
  37. ## Dry run with two negatives: `[-2, 3, -4]`
  38.  
  39. | x | maxProd | minProd | answer |
  40. |---|---------|---------|--------|
  41. | -2 | -2 | -2 | -2 |
  42. | 3 | 3 | -6 | 3 |
  43. | -4 | 24 | -12 | 24 |
  44.  
  45. At `-4`: `minProd (-6) * -4 = 24`. The earlier "bad" negative became the best.
  46.  
  47.  
  48. Note that both new values are computed from the *old* `max_prod` and `min_prod` (that's why `candidates` is built first).
  49.  
  50. ## How zeros are handled
  51.  
  52. If `x = 0`, all three candidates are `0`, so both `max_prod` and `min_prod` become `0`. The next number then naturally "starts fresh" through choice #1.
  53.  
  54. ## Complexity
  55.  
  56. - **Time:** O(n), one pass
  57. - **Space:** O(1), just three variables
  58.  
  59. ## One-line summary
  60.  
  61. > Track both the biggest and smallest product ending at each position, because a negative number can flip the smallest into the biggest.
  62. class Solution:
  63.    def maxProduct(self, nums: List[int]) -> int:
  64.        cur_max = cur_min = ans = nums[0]
  65.        
  66.        for num in nums[1:]:
  67.            temp_max = max(num, num * cur_max, num * cur_min) #Trick is store it in a seprate variable so the cur_min gets the max_till_previous_index accurate.
  68.            cur_min = min(num, num * cur_max, num * cur_min)
  69.            cur_max = temp_max
  70.            
  71.            ans = max(ans, cur_max)
  72.        
  73.        return ans
Advertisement
Add Comment
Please, Sign In to add comment