| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 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.ContainsDuplicate3; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class ContainsDuplicate3BruteForce : IContainsDuplicate3 |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(n^2) |
| | | 19 | | /// Space complexity - O(1) |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="nums"></param> |
| | | 22 | | /// <param name="indexDiff"></param> |
| | | 23 | | /// <param name="valueDiff"></param> |
| | | 24 | | /// <returns></returns> |
| | | 25 | | public bool ContainsNearbyAlmostDuplicate(int[] nums, int indexDiff, int valueDiff) |
| | 8 | 26 | | { |
| | 48 | 27 | | for (var i = 0; i < nums.Length - 1; i++) |
| | 19 | 28 | | { |
| | 126 | 29 | | for (var j = i + 1; j < nums.Length; j++) |
| | 47 | 30 | | { |
| | 47 | 31 | | if (Math.Abs(i - j) <= indexDiff && Math.Abs(nums[i] - nums[j]) <= valueDiff) |
| | 3 | 32 | | { |
| | 3 | 33 | | return true; |
| | | 34 | | } |
| | 44 | 35 | | } |
| | 16 | 36 | | } |
| | | 37 | | |
| | 5 | 38 | | return false; |
| | 8 | 39 | | } |
| | | 40 | | } |