< Summary

Information
Class: LeetCode.Algorithms.MostStonesRemovedWithSameRowOrColumn.MostStonesRemovedWithSameRowOrColumnUnionFind
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MostStonesRemovedWithSameRowOrColumn\MostStonesRemovedWithSameRowOrColumnUnionFind.cs
Line coverage
100%
Covered lines: 35
Uncovered lines: 0
Coverable lines: 35
Total lines: 81
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
RemoveStones(...)100%22100%
.ctor(...)100%11100%
get_ComponentCount()100%11100%
Find(...)100%44100%
Union(...)100%22100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MostStonesRemovedWithSameRowOrColumn\MostStonesRemovedWithSameRowOrColumnUnionFind.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.MostStonesRemovedWithSameRowOrColumn;
 13
 14/// <inheritdoc />
 15public class MostStonesRemovedWithSameRowOrColumnUnionFind : IMostStonesRemovedWithSameRowOrColumn
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="stones"></param>
 22    /// <returns></returns>
 23    public int RemoveStones(int[][] stones)
 924    {
 925        var unionFind = new UnionFind(20002);
 26
 13927        foreach (var stone in stones)
 5628        {
 5629            unionFind.Union(stone[0], stone[1] + 10001);
 5630        }
 31
 932        return stones.Length - unionFind.ComponentCount;
 933    }
 34
 35    private class UnionFind
 36    {
 37        private readonly int[] _parent;
 938        private readonly HashSet<int> _uniqueNodes = [];
 39
 940        public UnionFind(int n)
 941        {
 942            _parent = new int[n];
 43
 944            Array.Fill(_parent, -1);
 945        }
 46
 24747        public int ComponentCount { get; private set; }
 48
 49        private int Find(int node)
 16950        {
 16951            if (!_uniqueNodes.Contains(node))
 6652            {
 6653                ComponentCount++;
 54
 6655                _uniqueNodes.Add(node);
 6656            }
 57
 16958            if (_parent[node] == -1)
 11259            {
 11260                return node;
 61            }
 62
 5763            return _parent[node] = Find(_parent[node]);
 16964        }
 65
 66        public void Union(int x, int y)
 5667        {
 5668            var rootX = Find(x);
 5669            var rootY = Find(y);
 70
 5671            if (rootX == rootY)
 372            {
 373                return;
 74            }
 75
 5376            _parent[rootX] = rootY;
 77
 5378            ComponentCount--;
 5679        }
 80    }
 81}