-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlce_p0217_contains_duplicate.java
More file actions
128 lines (98 loc) · 2.76 KB
/
lce_p0217_contains_duplicate.java
File metadata and controls
128 lines (98 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
LCE 217. Contains Duplicate
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
Constraints:
- 1 <= nums.length <= 10^5
- -10^9 <= nums[i] <= 10^9
Topics:
- Array
- Hash Table
- Sorting
*/
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
class Solution {
// Time Complexity: O(n) - 10 ms -> 89.14%
// Space Complexity: O(n) - 57.7 MB -> 65.77%
public boolean containsDuplicate(int[] nums) {
HashSet<Integer> seen = new HashSet<>();
for (int num : nums) {
if (seen.contains(num)) {
return true;
}
seen.add(num);
}
return false;
}
// Time Complexity: O(n) - 30 ms -> 5.04%
// Space Complexity: O(n) - 57.7 MB -> 69.61%
public boolean containsDuplicateAlt1(int[] nums) {
HashMap<Integer, Integer> freqMap = new HashMap<>();
for (int num : nums) {
freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);
}
for (int num : freqMap.keySet()) {
if (freqMap.get(num) > 1) {
return true;
}
}
return false;
}
// Time Complexity: O(n + max) - MLE
// Space Complexity: O(max) = MLE
public boolean containsDuplicateAlt2(int[] nums) {
int max = 0;
for (int num : nums) {
num = Math.abs(num);
max = (num > max) ? num : max;
}
int[] freqArr = new int[max * 2 + 1];
for (int num : nums) {
freqArr[num + max]++;
}
for (int freq : freqArr) {
if (freq > 1) {
return true;
}
}
return false;
}
// Time Complexity: O(n log n) - 21 ms -> 9.55%
// Space Complexity: O(1) - 55.7 MB 0> 74.54%
public boolean containsDuplicateAlt3(int[] nums) {
Arrays.sort(nums);
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] == nums[i + 1]) {
return true;
}
}
return false;
}
// Time Complexity: O(n^2) - TLE
// Space Complexity: O(1) - TLE
public boolean containsDuplicateAlt4(int[] nums) {
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] == nums[j]) {
return true;
}
}
}
return false;
}
// Time Complexity: O(n) - 24 ms -> 5.41%
// Space Complexity: O(n) - 58.5 MB -> 27.31%
public boolean containsDuplicateAlt5(int[] nums) {
return Arrays.stream(nums).distinct().count() < nums.length;
}
}
/*
methods:
1. hash set (recommended)
2. hash map (overkill due to extra overhead)
3. frequency array (MLE, only works if constraints on nums[i] are minimal)
4. sorting (not recommended due to O(n log n) runtime)
5. brute force (TLE, only works if constraints on nums.length are minimal)
6. streams (compares hash set length and nums.length)
*/