< 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
83%
Covered lines: 26
Uncovered lines: 5
Coverable lines: 31
Total lines: 73
Line coverage: 83.8%
Branch coverage
85%
Covered branches: 12
Total branches: 14
Branch coverage: 85.7%
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(...)80%111078.26%
Find(...)100%44100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DesignNumberContainerSystem\DesignNumberContainerSystemDictionary.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.DesignNumberContainerSystem;
 13
 14/// <inheritdoc />
 15public class DesignNumberContainerSystemDictionary : IDesignNumberContainerSystem
 16{
 117    private readonly Dictionary<int, int> _indexToNumber = [];
 118    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)
 527    {
 528        if (_indexToNumber.TryGetValue(index, out var existingNumber))
 129        {
 130            if (existingNumber == number)
 031            {
 032                return;
 33            }
 34
 135            if (_numberToIndices.TryGetValue(existingNumber, out var existingNumberIndices))
 136            {
 137                existingNumberIndices.Remove(index);
 38
 139                if (existingNumberIndices.Count == 0)
 040                {
 041                    _numberToIndices.Remove(existingNumber);
 042                }
 143            }
 144        }
 45
 546        _indexToNumber[index] = number;
 47
 548        if (!_numberToIndices.TryGetValue(number, out var numberIndices))
 249        {
 250            numberIndices = [];
 51
 252            _numberToIndices[number] = numberIndices;
 253        }
 54
 555        numberIndices.Add(index);
 556    }
 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)
 365    {
 366        if (_numberToIndices.TryGetValue(number, out var numberIndices) && numberIndices.Count > 0)
 267        {
 268            return numberIndices.Min;
 69        }
 70
 171        return -1;
 372    }
 73}