Sub arrays Flashcards

1
Q

Maximum sum in a subarray

A
  1. Can use dp or currSum
  2. dp[i] = currNo + dp[i - 1]
  3. maxSum = max(maxSum, dp[i])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Maximum Size Subarray Sum Equals k

A
  1. HashMap
  2. map.put(currSum, j)
  3. currSum = k, maxLen = j + 1
  4. map.contains(currSum - k) i = currSum - k
    maxLen = max(maxLen, j - i)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Count of Subarray Sum Equals K

A
  1. HashMap
  2. map.contains(currSum - k)
    count += map.get(currSum - k)
  3. map.put(currSum, map.getOrDefault(currSum, 0) + 1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Maximum Product Subarray

A
  1. maintain min and max
  2. find the max = max(currNo, max(currSum * max, currSum * min))
  3. similarly for min
  4. maxProduct = max(maxProduct, max)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly