< Summary

Information
Class: LeetCode.Algorithms.ContainsDuplicate3.ContainsDuplicate3Dictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ContainsDuplicate3\ContainsDuplicate3Dictionary.cs
Line coverage
100%
Covered lines: 26
Uncovered lines: 0
Coverable lines: 26
Total lines: 65
Line coverage: 100%
Branch coverage
100%
Covered branches: 20
Total branches: 20
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ContainsNearbyAlmostDuplicate(...)100%1818100%
GetId(...)100%22100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ContainsDuplicate3\ContainsDuplicate3Dictionary.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.ContainsDuplicate3;
 13
 14/// <inheritdoc />
 15public class ContainsDuplicate3Dictionary : IContainsDuplicate3
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(min(n, k)),where k is the maximum number of elements the dictionary can hold at any one
 20    ///     based on the indexDiff
 21    /// </summary>
 22    /// <param name="nums"></param>
 23    /// <param name="indexDiff"></param>
 24    /// <param name="valueDiff"></param>
 25    /// <returns></returns>
 26    public bool ContainsNearbyAlmostDuplicate(int[] nums, int indexDiff, int valueDiff)
 927    {
 928        if (indexDiff <= 0 || valueDiff < 0)
 229        {
 230            return false;
 31        }
 32
 733        var numsDictionary = new Dictionary<long, long>();
 34
 735        var w = (long)valueDiff + 1;
 36
 6037        for (var i = 0; i < nums.Length; i++)
 2638        {
 2639            var m = GetId(nums[i], w);
 40
 2641            if (numsDictionary.ContainsKey(m) ||
 2642                (numsDictionary.ContainsKey(m - 1) && Math.Abs(nums[i] - numsDictionary[m - 1]) < w) ||
 2643                (numsDictionary.ContainsKey(m + 1) && Math.Abs(nums[i] - numsDictionary[m + 1]) < w))
 344            {
 345                return true;
 46            }
 47
 2348            if (i >= indexDiff)
 1349            {
 1350                var oldBucket = GetId(nums[i - indexDiff], w);
 51
 1352                numsDictionary.Remove(oldBucket);
 1353            }
 54
 2355            numsDictionary[m] = nums[i];
 2356        }
 57
 458        return false;
 959    }
 60
 61    private static long GetId(long x, long w)
 3962    {
 3963        return x < 0 ? ((x + 1) / w) - 1 : x / w;
 3964    }
 65}