Given an integer array nums and an integer k, return the length of the shortest non-empty subarray of nums with a sum of at least k. If there is no such subarray, return -1.
classSolution { public: intshortestSubarray(vector<int>& nums, int k){ deque<pair<long, int>> dq {{0, -1}}; int res = INT_MAX; long sum = 0; for(int i = 0; i < nums.size(); i++) { sum += nums[i];
while(!dq.empty() and dq[0].first <= sum - k) res = min(res, i - dq[0].second), dq.pop_front(); while(!dq.empty() and dq.back().first >= sum) dq.pop_back(); dq.push_back({sum, i}); } return res == INT_MAX ? -1 : res; } };