| | 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 | |
|
| | 12 | | namespace LeetCode.Algorithms.CountGoodTriplets; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class CountGoodTripletsBruteForce : ICountGoodTriplets |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n^3) |
| | 19 | | /// Space complexity - O(1) |
| | 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) |
| 2 | 27 | | { |
| 2 | 28 | | var result = 0; |
| | 29 | |
|
| 18 | 30 | | for (var i = 0; i < arr.Length - 2; i++) |
| 7 | 31 | | { |
| 46 | 32 | | for (var j = i + 1; j < arr.Length - 1; j++) |
| 16 | 33 | | { |
| 16 | 34 | | if (Math.Abs(arr[i] - arr[j]) > a) |
| 7 | 35 | | { |
| 7 | 36 | | continue; |
| | 37 | | } |
| | 38 | |
|
| 60 | 39 | | for (var k = j + 1; k < arr.Length; k++) |
| 21 | 40 | | { |
| 21 | 41 | | if (Math.Abs(arr[j] - arr[k]) <= b && |
| 21 | 42 | | Math.Abs(arr[i] - arr[k]) <= c) |
| 4 | 43 | | { |
| 4 | 44 | | result++; |
| 4 | 45 | | } |
| 21 | 46 | | } |
| 9 | 47 | | } |
| 7 | 48 | | } |
| | 49 | |
|
| 2 | 50 | | return result; |
| 2 | 51 | | } |
| | 52 | | } |