< Summary

Information
Class: LeetCode.Algorithms.DesignNumberContainerSystem.DesignNumberContainerSystemDictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DesignNumberContainerSystem\DesignNumberContainerSystemDictionary.cs
Line coverage
93%
Covered lines: 29
Uncovered lines: 2
Coverable lines: 31
Total lines: 73
Line coverage: 93.5%
Branch coverage
92%
Covered branches: 13
Total branches: 14
Branch coverage: 92.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
Change(...)90%101091.3%
Find(...)100%44100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DesignNumberContainerSystem\DesignNumberContainerSystemDictionary.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2026 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.DesignNumberContainerSystem;
 13
 14/// <inheritdoc />
 15public sealed class DesignNumberContainerSystemDictionary : IDesignNumberContainerSystem
 16{
 417    private readonly Dictionary<int, int> _indexToNumber = [];
 418    private readonly Dictionary<int, SortedSet<int>> _numberToIndices = [];
 19
 20    /// <summary>
 21    ///     Time complexity - O(log n)
 22    ///     Space complexity - O(n)
 23    /// </summary>
 24    /// <param name="index"></param>
 25    /// <param name="number"></param>
 26    public void Change(int index, int number)
 1027    {
 1028        if (_indexToNumber.TryGetValue(index, out var existingNumber))
 229        {
 230            if (existingNumber == number)
 031            {
 032                return;
 33            }
 34
 235            if (_numberToIndices.TryGetValue(existingNumber, out var existingNumberIndices))
 236            {
 237                existingNumberIndices.Remove(index);
 38
 239                if (existingNumberIndices.Count == 0)
 140                {
 141                    _numberToIndices.Remove(existingNumber);
 142                }
 243            }
 244        }
 45
 1046        _indexToNumber[index] = number;
 47
 1048        if (!_numberToIndices.TryGetValue(number, out var numberIndices))
 549        {
 550            numberIndices = [];
 51
 552            _numberToIndices[number] = numberIndices;
 553        }
 54
 1055        numberIndices.Add(index);
 1056    }
 57
 58    /// <summary>
 59    ///     Time complexity - O(1)
 60    ///     Space complexity - O(n)
 61    /// </summary>
 62    /// <param name="number"></param>
 63    /// <returns></returns>
 64    public int Find(int number)
 965    {
 966        if (_numberToIndices.TryGetValue(number, out var numberIndices) && numberIndices.Count > 0)
 567        {
 568            return numberIndices.Min;
 69        }
 70
 471        return -1;
 972    }
 73}