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

Leetcode Problem 862. Shortest Subarray with Sum at Least K

862. Shortest Subarray with Sum at Least K

Leetcode Solutions

Key approach of the solution: Sliding Window with Monoqueue

  1. Initialize a deque to store indices of prefix sums in increasing order.
  2. Initialize variables for the prefix sum and the minimum length found so far.
  3. Iterate through the array, updating the prefix sum.
  4. While the current prefix sum minus the prefix sum at the deque's front index is at least k, update the minimum length and pop from the front of the deque.
  5. Maintain the monoqueue by popping from the back while the current prefix sum is less than or equal to the prefix sums at the indices stored in the deque.
  6. Add the current index to the back of the deque.
  7. After iterating through the array, check if the minimum length was updated and return it, or return -1 if no such subarray was found.
UML Thumbnail

Key approach of the solution: Brute Force

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR