LeetCode 862: Shortest Subarray with Sum at Least K I'm posting my code for a LeetCode problem copied here. If you would like to review, please do so. Thank you for your time!
Problem


Return the length of the shortest, non-empty, contiguous subarray of nums with sum at least k.
If there is no non-empty subarray with sum at least k, return -1.

Example 1:
Input: nums = [1], k = 1 Output: 1
Example 2:
Input: nums = [1,2], k = 4 Output: -1
Example 3:
Input: nums = [2,-1,2], k = 3 Output: 3
Note:

\$1 &lt;= \text{nums.length} &lt;= 50000\$
\$-10 ^ 5 &lt;= \text{nums}[i] &lt;= 10 ^ 5\$
\$1 &lt;= k &lt;= 10 ^ 9\$


Code
#include &lt;vector&gt;
#include &lt;algorithm&gt;
#include &lt;deque&gt;

class Solution {
public:
    static int shortestSubarray(std::vector&lt;int&gt; nums, const int k) {
        const int length = nums.size();
        int shortest = length + 1;

        std::deque&lt;int&gt; deque_indicies;

        for (int index = 0; index &lt; length; index++) {
            if (index) {
                nums[index] += nums[index - 1];
            }

            if (nums[index] &gt;= k) {
                shortest = std::min(shortest, index + 1);
            }

            while (!deque_indicies.empty() &amp;&amp; nums[index] - nums[deque_indicies.front()] &gt;= k) {
                shortest = std::min(shortest, index - deque_indicies.front());
                deque_indicies.pop_front();
            }

            while (!deque_indicies.empty() &amp;&amp; nums[index] &lt;= nums[deque_indicies.back()]) {
                deque_indicies.pop_back();
            }

            deque_indicies.emplace_back(index);
        }

        return shortest &lt;= length ? shortest : -1;
    }
};


Problem

Solution

Discuss
Passing by reference or value
Currently,
std::vector&lt;int&gt; nums

forces callers to pass a copy of the entire vector by value. You could argue that this is actually useful, since your algorithm needs to mutate it (or a copy of it) in-place. My preference is usually to make this copy-step explicit instead:

Pass a const &amp; reference, not a mutable copy
Make a local copy of the vector explicitly, invoking the copy constructor
Mutate your copy.

Spelling
deque_indicies -&gt; deque_indices