< Summary

Information
Class: LeetCode.Algorithms.FindingPairsWithCertainSum.FindingPairsWithCertainSumDictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindingPairsWithCertainSum\FindingPairsWithCertainSumDictionary.cs
Line coverage
100%
Covered lines: 36
Uncovered lines: 0
Coverable lines: 36
Total lines: 85
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%44100%
Add(...)100%44100%
Count(...)100%44100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindingPairsWithCertainSum\FindingPairsWithCertainSumDictionary.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.FindingPairsWithCertainSum;
 13
 14/// <inheritdoc />
 15public class FindingPairsWithCertainSumDictionary : IFindingPairsWithCertainSum
 16{
 17    private readonly int[] _nums1;
 18    private readonly int[] _nums2;
 19    private readonly Dictionary<int, int> _nums2FrequencyDictionary;
 20
 21    /// <summary>
 22    ///     Time complexity - O(n)
 23    ///     Space complexity - O(n)
 24    /// </summary>
 25    /// <param name="nums1"></param>
 26    /// <param name="nums2"></param>
 127    public FindingPairsWithCertainSumDictionary(int[] nums1, int[] nums2)
 128    {
 129        _nums1 = nums1;
 130        _nums2 = nums2;
 131        _nums2FrequencyDictionary = new Dictionary<int, int>();
 32
 1533        foreach (var num2 in nums2)
 634        {
 635            if (!_nums2FrequencyDictionary.TryAdd(num2, 1))
 236            {
 237                _nums2FrequencyDictionary[num2]++;
 238            }
 639        }
 140    }
 41
 42    /// <summary>
 43    ///     Time complexity - O(1)
 44    ///     Space complexity - O(1)
 45    /// </summary>
 46    /// <param name="index"></param>
 47    /// <param name="val"></param>
 48    public void Add(int index, int val)
 349    {
 350        _nums2FrequencyDictionary[_nums2[index]]--;
 51
 352        if (_nums2FrequencyDictionary[_nums2[index]] == 0)
 253        {
 254            _nums2FrequencyDictionary.Remove(_nums2[index]);
 255        }
 56
 357        _nums2[index] += val;
 58
 359        if (!_nums2FrequencyDictionary.TryAdd(_nums2[index], 1))
 260        {
 261            _nums2FrequencyDictionary[_nums2[index]]++;
 262        }
 363    }
 64
 65    /// <summary>
 66    ///     Time complexity - O(n)
 67    ///     Space complexity - O(1)
 68    /// </summary>
 69    /// <param name="tot"></param>
 70    /// <returns></returns>
 71    public int Count(int tot)
 472    {
 473        var count = 0;
 74
 6075        foreach (var num1 in _nums1)
 2476        {
 2477            if (_nums2FrequencyDictionary.TryGetValue(tot - num1, out var value))
 1078            {
 1079                count += value;
 1080            }
 2481        }
 82
 483        return count;
 484    }
 85}