< Summary

Information
Class: LeetCode.Algorithms.ShortestSubarrayWithSumAtLeastK.ShortestSubarrayWithSumAtLeastKLinkedList
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ShortestSubarrayWithSumAtLeastK\ShortestSubarrayWithSumAtLeastKLinkedList.cs
Line coverage
100%
Covered lines: 23
Uncovered lines: 0
Coverable lines: 23
Total lines: 56
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ShortestSubarray(...)100%1414100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ShortestSubarrayWithSumAtLeastK\ShortestSubarrayWithSumAtLeastKLinkedList.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12namespace LeetCode.Algorithms.ShortestSubarrayWithSumAtLeastK;
 13
 14/// <inheritdoc />
 15public class ShortestSubarrayWithSumAtLeastKLinkedList : IShortestSubarrayWithSumAtLeastK
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="nums"></param>
 22    /// <param name="k"></param>
 23    /// <returns></returns>
 24    public int ShortestSubarray(int[] nums, int k)
 525    {
 526        var prefixSum = new long[nums.Length + 1];
 27
 4228        for (var i = 0; i < nums.Length; i++)
 1629        {
 1630            prefixSum[i + 1] = prefixSum[i] + nums[i];
 1631        }
 32
 533        var linkedList = new LinkedList<int>();
 34
 535        var minLength = nums.Length + 1;
 36
 5237        for (var i = 0; i < prefixSum.Length; i++)
 2138        {
 2639            while (linkedList.First is not null && prefixSum[i] - prefixSum[linkedList.First.Value] >= k)
 540            {
 541                minLength = Math.Min(minLength, i - linkedList.First.Value);
 42
 543                linkedList.RemoveFirst();
 544            }
 45
 2746            while (linkedList.Last is not null && prefixSum[i] <= prefixSum[linkedList.Last.Value])
 647            {
 648                linkedList.RemoveLast();
 649            }
 50
 2151            linkedList.AddLast(i);
 2152        }
 53
 554        return minLength <= nums.Length ? minLength : -1;
 555    }
 56}