Skip to content

Commit e75935b

Browse files
committed
leetcode
1 parent 5dd5a53 commit e75935b

File tree

4 files changed

+225
-0
lines changed

4 files changed

+225
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
func containsNearbyDuplicate(nums []int, k int) bool {
4+
seen := map[int]int{}
5+
for idx, val := range nums {
6+
if value, ok := seen[val]; ok && idx-value <= k {
7+
return true
8+
}
9+
seen[val] = idx
10+
}
11+
return false
12+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# 🔥 Contains Duplicate II 🔥 || 3 Solutions || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1
4+
5+
```dart
6+
import 'dart:collection';
7+
8+
class Solution {
9+
// Runtime: 738 ms, faster than 66.67% of Dart online submissions for Contains Duplicate II.
10+
// Memory Usage: 194.5 MB, less than 12.50% of Dart online submissions for Contains Duplicate II.
11+
bool containsNearbyDuplicate(List<int> nums, int k) {
12+
// Base case...
13+
if (nums.length < 2 || k == 0) return false;
14+
int i = 0;
15+
// Create a Hash Set for storing previous of k elements...
16+
HashSet<int> hashSet = HashSet<int>();
17+
// Traverse for all elements of the given array in a for loop...
18+
for (int j = 0; j < nums.length; j++) {
19+
// If duplicate element is present at distance less than equal to k, return true...
20+
if (!hashSet.add(nums[j])) {
21+
return true;
22+
}
23+
// If size of the Hash Set becomes greater than k...
24+
if (hashSet.length >= k + 1) {
25+
// Remove the last visited element from the set...
26+
hashSet.remove(nums[i++]);
27+
}
28+
}
29+
// If no duplicate element is found then return false...
30+
return false;
31+
}
32+
}
33+
```
34+
35+
## Solution - 2
36+
37+
```dart
38+
import 'dart:collection';
39+
40+
class Solution {
41+
// Runtime: 648 ms, faster than 87.50% of Dart online submissions for Contains Duplicate II.
42+
// Memory Usage: 194.4 MB, less than 12.50% of Dart online submissions for Contains Duplicate II
43+
bool containsNearbyDuplicate(List<int> nums, int k) {
44+
int n = nums.length;
45+
HashSet<int> sets = HashSet();
46+
for (int i = 0; i < n; i++) {
47+
if (!sets.add(nums[i])) return true;
48+
if (sets.length > k) sets.remove(nums[i - k]);
49+
}
50+
return false;
51+
}
52+
}
53+
54+
```
55+
56+
## Solution - 3
57+
58+
```dart
59+
import 'dart:collection';
60+
61+
class Solution {
62+
// Runtime: 699 ms, faster than 75.00% of Dart online submissions for Contains Duplicate II.
63+
// Memory Usage: 200.2 MB, less than 8.33% of Dart online submissions for Contains Duplicate II.
64+
bool containsNearbyDuplicate(List<int> nums, int k) {
65+
// HashSet to et unique pair of values
66+
HashSet<int> sets = HashSet();
67+
// looping through each and every element
68+
for (int i = 0; i < nums.length; ++i) {
69+
// if the HashSet have the value two pairs
70+
if (sets.contains(nums[i])) {
71+
// return true
72+
return true;
73+
}
74+
// than add into the set
75+
sets.add(nums[i]);
76+
// IF THE LENGTH IS GREATER THAN DISTINCT INDICES
77+
if (sets.length > k) {
78+
// than we will remove it from the set
79+
sets.remove(nums[i - k]);
80+
}
81+
}
82+
return false;
83+
}
84+
}
85+
```

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
7777
- [My Calendar III](MyCalendar-III/my_calendar_III.dart)
7878
- [Reverse Linked List](ReverseLinkedList/reverse_linked_list.dart)
7979
- [Contains Duplicate](ContainsDuplicate/contains_duplicate.dart)
80+
- [Contains Duplicate II](ContainsDuplicate-II/contains_duplicate_II.dart)
8081

8182
## Reach me via
8283

0 commit comments

Comments
 (0)