Skip to content

862. Shortest Subarray with Sum at Least K #843

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to use a sliding window approach combined with prefix sums and a monotonic queue. Here's the step-by-step approach:

Steps:

  1. Prefix Sum:

    • First, calculate the prefix sum array, where each element at index i represents the sum of the elements from the start of the array to i. The prefix sum allows us to compute the sum of any subarray in constant time.
  2. Monotonic Queue:

    • We use a deque (double-ended queue) to maintain the indices of the prefix_sum array. The deque will be maintained in an increasing order of prefix sums.
    • This helps us efficiently find subarrays with the sum greater than or equal to k by comparing the current prefix sum with earlier prefix sums.
  3. Sliding Window …

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Nov 17, 2024
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested hard Difficulty
2 participants