bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 689. Maximum Sum of 3 Non-Overlapping Subarrays

689. Maximum Sum of 3 Non-Overlapping Subarrays

Leetcode Solutions

Dynamic Programming with Sliding Window

  1. Calculate the sum of each subarray of length k and store it in array W.
  2. Initialize left, right, and sum arrays of the same length as W.
  3. Fill the left array with the starting index of the maximum subarray sum ending at or before each index.
  4. Fill the right array with the starting index of the maximum subarray sum starting at or after each index.
  5. Iterate through W using j as the middle subarray's starting index, and for each j, calculate the sum of subarrays starting at left[j - k], j, and right[j + k].
  6. Keep track of the maximum sum and the corresponding indices.
  7. Return the indices of the three subarrays with the maximum sum.
UML Thumbnail

Brute Force with Optimization

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...