122. Shortest Subarray with Sum at Least K

HardArrayArray

Given an integer array nums (may contain negatives) and integer k, return the length of the shortest non-empty contiguous subarray with sum at least k. If none exists, return -1. Input: '[nums], k'.

Input: '[nums], k'.

Output: Integer length or -1.

Examples

Example 1
Input: [2,-1,2], 3
Output: 3
Explanation: Whole array sums to 3.
Example 2
Input: [1], 1
Output: 1
Explanation: Single element.
Example 3
Input: [1,2], 4
Output: -1
Explanation: Max sum 3 < 4.

Constraints

Asked by

AmazonGoogleMicrosoftMetaBloomberg
Solve this problem in the editor →