|
| 1 | +/* |
| 2 | +
|
| 3 | + -* Contains Duplicate II *- |
| 4 | +
|
| 5 | +Given an integer array nums and an integer k, return true if there are two distinct |
| 6 | +indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k. |
| 7 | +
|
| 8 | +
|
| 9 | +
|
| 10 | +Example 1: |
| 11 | +
|
| 12 | +Input: nums = [1,2,3,1], k = 3 |
| 13 | +Output: true |
| 14 | +Example 2: |
| 15 | +
|
| 16 | +Input: nums = [1,0,1,1], k = 1 |
| 17 | +Output: true |
| 18 | +Example 3: |
| 19 | +
|
| 20 | +Input: nums = [1,2,3,1,2,3], k = 2 |
| 21 | +Output: false |
| 22 | +
|
| 23 | +
|
| 24 | +Constraints: |
| 25 | +
|
| 26 | +1 <= nums.length <= 105 |
| 27 | +-109 <= nums[i] <= 109 |
| 28 | +0 <= k <= 105 |
| 29 | +
|
| 30 | +
|
| 31 | +*/ |
| 32 | +import 'dart:collection'; |
| 33 | +import 'dart:math'; |
| 34 | + |
| 35 | +class A { |
| 36 | + bool containsNearbyDuplicate(List<int> nums, int k) { |
| 37 | + // error |
| 38 | + //sliding window with a hash-set |
| 39 | + Queue<int> q = Queue(); |
| 40 | + HashSet<int> sets = HashSet(); |
| 41 | + //preprocessing for 1st k elements |
| 42 | + for (int i = 0; i <= min(k, nums.length - 1); i++) { |
| 43 | + q.add(nums[i]); |
| 44 | + if (sets.contains(nums[i])) { |
| 45 | + return true; |
| 46 | + } else { |
| 47 | + sets.add(nums[i]); |
| 48 | + } |
| 49 | + } |
| 50 | + //process rest of the elements |
| 51 | + for (int i = k + 1; i < nums.length; i++) { |
| 52 | + sets.remove(q.remove); |
| 53 | + q.add(nums[i]); |
| 54 | + if (sets.contains(nums[i])) { |
| 55 | + return true; |
| 56 | + } else { |
| 57 | + sets.add(nums[i]); |
| 58 | + } |
| 59 | + } |
| 60 | + return false; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +class B { |
| 65 | +// Runtime: 738 ms, faster than 66.67% of Dart online submissions for Contains Duplicate II. |
| 66 | +// Memory Usage: 194.5 MB, less than 12.50% of Dart online submissions for Contains Duplicate II. |
| 67 | + bool containsNearbyDuplicate(List<int> nums, int k) { |
| 68 | + // Base case... |
| 69 | + if (nums.length < 2 || k == 0) return false; |
| 70 | + int i = 0; |
| 71 | + // Create a Hash Set for storing previous of k elements... |
| 72 | + HashSet<int> hashSet = HashSet<int>(); |
| 73 | + // Traverse for all elements of the given array in a for loop... |
| 74 | + for (int j = 0; j < nums.length; j++) { |
| 75 | + // If duplicate element is present at distance less than equal to k, return true... |
| 76 | + if (!hashSet.add(nums[j])) { |
| 77 | + return true; |
| 78 | + } |
| 79 | + // If size of the Hash Set becomes greater than k... |
| 80 | + if (hashSet.length >= k + 1) { |
| 81 | + // Remove the last visited element from the set... |
| 82 | + hashSet.remove(nums[i++]); |
| 83 | + } |
| 84 | + } |
| 85 | + // If no duplicate element is found then return false... |
| 86 | + return false; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +class C { |
| 91 | +// Runtime: 699 ms, faster than 75.00% of Dart online submissions for Contains Duplicate II. |
| 92 | +// Memory Usage: 200.2 MB, less than 8.33% of Dart online submissions for Contains Duplicate II. |
| 93 | + bool containsNearbyDuplicate(List<int> nums, int k) { |
| 94 | + // HashSet to et unique pair of values |
| 95 | + HashSet<int> sets = HashSet(); |
| 96 | + // looping through each and every element |
| 97 | + for (int i = 0; i < nums.length; ++i) { |
| 98 | + // if the HashSet have the value two pairs |
| 99 | + if (sets.contains(nums[i])) { |
| 100 | + // return true |
| 101 | + return true; |
| 102 | + } |
| 103 | + // than add into the set |
| 104 | + sets.add(nums[i]); |
| 105 | + // IF THE LENGTH IS GREATER THAN DISTINCT INDICES |
| 106 | + if (sets.length > k) { |
| 107 | + // than we will remove it from the set |
| 108 | + sets.remove(nums[i - k]); |
| 109 | + } |
| 110 | + } |
| 111 | + return false; |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +class D { |
| 116 | +// Runtime: 648 ms, faster than 87.50% of Dart online submissions for Contains Duplicate II. |
| 117 | +// Memory Usage: 194.4 MB, less than 12.50% of Dart online submissions for Contains Duplicate II |
| 118 | + bool containsNearbyDuplicate(List<int> nums, int k) { |
| 119 | + int n = nums.length; |
| 120 | + HashSet<int> sets = HashSet(); |
| 121 | + for (int i = 0; i < n; i++) { |
| 122 | + if (!sets.add(nums[i])) return true; |
| 123 | + if (sets.length > k) sets.remove(nums[i - k]); |
| 124 | + } |
| 125 | + return false; |
| 126 | + } |
| 127 | +} |
0 commit comments