< Summary

Information
Class: LeetCode.Algorithms.CountGoodTriplets.CountGoodTripletsPrefixSum
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CountGoodTriplets\CountGoodTripletsPrefixSum.cs
Line coverage
93%
Covered lines: 28
Uncovered lines: 2
Coverable lines: 30
Total lines: 66
Line coverage: 93.3%
Branch coverage
91%
Covered branches: 11
Total branches: 12
Branch coverage: 91.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CountGoodTriplets(...)91.66%121293.33%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CountGoodTriplets\CountGoodTripletsPrefixSum.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.CountGoodTriplets;
 13
 14/// <inheritdoc />
 15public class CountGoodTripletsPrefixSum : ICountGoodTriplets
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n^2 + n * M)
 19    ///     Space complexity - O(M)
 20    /// </summary>
 21    /// <param name="arr"></param>
 22    /// <param name="a"></param>
 23    /// <param name="b"></param>
 24    /// <param name="c"></param>
 25    /// <returns></returns>
 26    public int CountGoodTriplets(int[] arr, int a, int b, int c)
 227    {
 228        var result = 0;
 229        var sum = new int[1001];
 30
 2231        for (var j = 0; j < arr.Length - 1; ++j)
 932        {
 6833            for (var k = j + 1; k < arr.Length; ++k)
 2534            {
 2535                if (Math.Abs(arr[j] - arr[k]) > b)
 1736                {
 1737                    continue;
 38                }
 39
 840                var left = Math.Max(0, Math.Max(arr[j] - a, arr[k] - c));
 841                var right = Math.Min(1000, Math.Min(arr[j] + a, arr[k] + c));
 42
 843                if (left > right)
 044                {
 045                    continue;
 46                }
 47
 848                if (left == 0)
 549                {
 550                    result += sum[right];
 551                }
 52                else
 353                {
 354                    result += sum[right] - sum[left - 1];
 355                }
 856            }
 57
 1799658            for (var k = arr[j]; k <= 1000; ++k)
 898959            {
 898960                sum[k]++;
 898961            }
 962        }
 63
 264        return result;
 265    }
 66}