LeetCode 862: Shortest Subarray with Sum at Least K Solution
Master LeetCode problem 862 (Shortest Subarray with Sum at Least K), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
862. Shortest Subarray with Sum at Least K
Problem Explanation
Explanation:
To solve this problem, we can use a deque (double-ended queue) to maintain a monotonic increasing prefix sum array. We iterate over the input array nums, compute the prefix sums, and store them along with their corresponding indices in the deque. At each step, we check if the current prefix sum minus the smallest prefix sum in the deque is greater than or equal to k. If it is, we update the minimum length of subarray found so far. If not, we continue iterating and updating the deque.
Algorithm:
- Initialize variables:
nas the length ofnums,minLento store the minimum subarray length found so far (initially set ton + 1),prefixSumarray to store the prefix sums, and a dequedeqto maintain a monotonic increasing prefix sum array. - Initialize the deque
deqwith a tuple(0, -1)to represent the initial prefix sum of 0 at index -1. - Iterate over the input array
numsand compute the prefix sums, storing each prefix sum and its index in the deque. - At each step, check if the current prefix sum minus the smallest prefix sum in the deque is greater than or equal to
k. - If yes, update
minLenwith the minimum length of subarray found so far. - If not, continue iterating and updating the deque.
- Return
minLenif a valid subarray is found; otherwise, return -1.
Time Complexity:
The time complexity of this algorithm is O(n) where n is the length of the input array nums.
Space Complexity:
The space complexity of this algorithm is O(n) to store the prefix sum array and the deque.
:
Solution Code
class Solution {
public int shortestSubarray(int[] nums, int k) {
int n = nums.length;
int minLen = n + 1;
int[] prefixSum = new int[n + 1];
Deque<Integer> deq = new LinkedList<>();
deq.addLast(0);
for (int i = 0; i < n; i++) {
prefixSum[i + 1] = prefixSum[i] + nums[i];
while (!deq.isEmpty() && prefixSum[i + 1] - prefixSum[deq.peekFirst()] >= k) {
minLen = Math.min(minLen, i - deq.pollFirst() + 1);
}
while (!deq.isEmpty() && prefixSum[i + 1] <= prefixSum[deq.peekLast()]) {
deq.pollLast();
}
deq.addLast(i + 1);
}
return minLen == n + 1 ? -1 : minLen;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 862 (Shortest Subarray with Sum at Least K)?
This page provides optimized solutions for LeetCode problem 862 (Shortest Subarray with Sum at Least K) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 862 (Shortest Subarray with Sum at Least K)?
The time complexity for LeetCode 862 (Shortest Subarray with Sum at Least K) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 862 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 862 in Java, C++, or Python.