< Summary

Information
Class: LeetCode.Algorithms.MinimumSwapsToGroupAll1Together2.MinimumSwapsToGroupAll1Together2SlidingWindow
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumSwapsToGroupAll1Together2\MinimumSwapsToGroupAll1Together2SlidingWindow.cs
Line coverage
93%
Covered lines: 27
Uncovered lines: 2
Coverable lines: 29
Total lines: 63
Line coverage: 93.1%
Branch coverage
91%
Covered branches: 11
Total branches: 12
Branch coverage: 91.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MinSwaps(...)91.66%121293.1%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumSwapsToGroupAll1Together2\MinimumSwapsToGroupAll1Together2SlidingWindow.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.MinimumSwapsToGroupAll1Together2;
 13
 14/// <inheritdoc />
 15public class MinimumSwapsToGroupAll1Together2SlidingWindow : IMinimumSwapsToGroupAll1Together2
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="nums"></param>
 22    /// <returns></returns>
 23    public int MinSwaps(int[] nums)
 324    {
 2425        var totalOnes = nums.Count(num => num == 1);
 26
 327        if (totalOnes == 0)
 028        {
 029            return 0;
 30        }
 31
 332        var windowedArray = nums.Concat(nums.Take(totalOnes)).ToArray();
 33
 334        var currentZeroCount = 0;
 35
 2836        for (var i = 0; i < totalOnes; i++)
 1137        {
 1138            if (windowedArray[i] == 0)
 539            {
 540                currentZeroCount++;
 541            }
 1142        }
 43
 344        var minSwaps = currentZeroCount;
 45
 4246        for (var i = 1; i < nums.Length; i++)
 1847        {
 1848            if (windowedArray[i - 1] == 0)
 849            {
 850                currentZeroCount--;
 851            }
 52
 1853            if (windowedArray[i + totalOnes - 1] == 0)
 754            {
 755                currentZeroCount++;
 756            }
 57
 1858            minSwaps = Math.Min(minSwaps, currentZeroCount);
 1859        }
 60
 361        return minSwaps;
 362    }
 63}

Methods/Properties

MinSwaps(System.Int32[])