forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0287-find-the-duplicate-number.js
257 lines (200 loc) · 6.46 KB
/
0287-find-the-duplicate-number.js
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
* https://leetcode.com/problems/find-the-duplicate-number/
* Time O(N * log(N)) | Space O(1)
* @param {number[]} nums
* @return {number}
*/
var findDuplicate = function(nums) {
nums.sort((a, b) => a - b);/* Time O(N * log(N)) | HeapSort Space O(1) | QuickSort Space O(log(N)) */
for (let i = 1; i < nums.length; i++) {/* Time O(N) */
const isPrevDuplicate = nums[i - 1] === nums[i]
if (isPrevDuplicate) return nums[i];
}
return -1;
}
/**
* https://leetcode.com/problems/find-the-duplicate-number/
* Time O(N * log(N)) | Space O(1)
* @param {number[]} nums
* @return {number}
*/
var findDuplicate = function(nums) {
let [ left, right, duplicate ] = [ 1, (nums.length - 1), -1 ];
while (left <= right) {/* Time O(log(N)) */
const mid = (left + right) >> 1;
const count = getCount(mid, nums);/* Time O(N) */
const isMidGreater = count <= mid
if (isMidGreater) left = mid + 1;
const isMidLess = mid < count
if (isMidLess) {
duplicate = mid;
right = mid - 1;
}
}
return duplicate;
}
const getCount = (mid, nums, count = 0) => {
for (const num of nums) {/* Time O(N) */
const isMidGreater = num <= mid
if (isMidGreater) count++;
}
return count;
}
/**
* https://leetcode.com/problems/find-the-duplicate-number/
* Time O(N * log(N)) | Space O(1)
* @param {number[]} nums
* @return {number}
*/
var findDuplicate = function(nums, duplicate = 0) {
const mostSignificantBit = calcMaxBit(nums); /* Time O(N) */
for (let bit = 0; bit < mostSignificantBit; bit++) {/* Time O(log(N)) */
const [ baseCount, numsCount, mask ] = count(nums, bit);/* Time O(N) */
const isMoreFrequentlySet = baseCount < numsCount
if (isMoreFrequentlySet) duplicate |= mask;
}
return duplicate;
}
const calcMaxBit = (nums, bits = 0) => {
let max = Math.max(0, ...nums);/* Time O(N) */
while (max) {/* Time O(log(MAX)) */
max >>= 1;
bits++;
}
return bits;
}
const count = (nums, bit) => {
let [ baseCount, numsCount, mask ] = [ 0, 0, (1 << bit) ];
for (let i = 0; i < nums.length; i++) {/* Time O(N) */
const isBaseBitSet = 0 < (i & mask);
if (isBaseBitSet) baseCount++;
const isNumBitSet = 0 < (nums[i] & mask);
if (isNumBitSet) numsCount++;
}
return [ baseCount, numsCount, mask ];
}
/**
* https://leetcode.com/problems/find-the-duplicate-number/
* Time O(N) | Space O(N)
* @param {number[]} nums
* @return {number}
*/
var findDuplicate = function(nums, curr = 0) {
const isBaseCase = curr === nums[curr]
if (isBaseCase) return curr;
const next = nums[curr];
nums[curr] = curr;
return findDuplicate(nums, next);/* Time O(N) | Space O(N) */
}
/**
* https://leetcode.com/problems/find-the-duplicate-number/
* Time O(N) | Space O(N)
* @param {number[]} nums
* @return {number}
*/
var findDuplicate = function(nums, seen = new Set()) {
for (const num of nums) {/* Time O(N) */
if (seen.has(num)) return num;
seen.add(num); /* Space O(N) */
}
return -1;
}
/**
* https://leetcode.com/problems/find-the-duplicate-number/
* Time O(N) | Space O(1)
* @param {number[]} nums
* @return {number}
*/
var findDuplicate = function(nums) {
cyclicSort(nums); /* Time O(N) */
return search(nums); /* Time O(N) */
}
const cyclicSort = (nums, index = 0) => {
const swap = (arr, a, b) => [arr[a], arr[b]] = [arr[b], arr[a]];
while (index < nums.length) { /* Time O(N) */
const [ num, arrayIndex, arrayNum ] = [ nums[index], (nums[index] - 1), nums[(nums[index] - 1)] ];
const canSwap = !isSame(num, arrayNum);
if (canSwap) {
swap(nums, index, arrayIndex);
continue;
}
index++;
}
}
const isSame = (a, b) => a === b;
const search = (nums) => {
for (let index = 0; index < nums.length; index++) {/* Time O(N) */
const [ num, arrayIndex ] = [ nums[index], (index + 1) ];
if (!isSame(num, arrayIndex)) return num;
}
return nums.length;
}
/**
* https://leetcode.com/problems/find-the-duplicate-number/
* Time O(N) | Space O(1)
* @param {number[]} nums
* @return {number}
*/
var findDuplicate = function(nums) {
const duplicate = negativeMarking(nums);/* Time O(N) */
restoreToPositiveNumbers(nums); /* Time O(N) */
return duplicate;
}
const negativeMarking = (nums) => {
for (let i = 0; i < nums.length; i++) {/* Time O(N) */
const curr = Math.abs(nums[i]);
const isNegative = nums[curr] < 0;
if (isNegative) return curr;
nums[curr] *= -1;
}
return -1
}
const restoreToPositiveNumbers = (nums) => {
for (let i = 0; i < nums.length; i++) {/* Time O(N) */
nums[i] = Math.abs(nums[i]);
}
}
/**
* https://leetcode.com/problems/find-the-duplicate-number/
* Time O(N) | Space O(1)
* @param {number[]} nums
* @return {number}
*/
var findDuplicate = function(nums, start = 0) {
const swap = (arr, a, b) => [arr[a], arr[b]] = [arr[b], arr[a]];
const isSame = () => nums[start] === nums[nums[start]];
while (!isSame()) {/* Time O(N) */
swap(nums, start, nums[start]);
}
return nums[start];
}
/**
* https://leetcode.com/problems/find-the-duplicate-number/
* Time O(N) | Space O(1)
* @param {number[]} nums
* @return {number}
*/
var findDuplicate = function(nums) {
if (!nums.length) return -1
let [ slow, fast ] = moveFast(nums); /* Time O(N) */
[ slow, fast ] = moveSlow(nums, slow, fast);/* Time O(N) */
return slow;
};
const moveFast = (nums, start = 0) => {
let [ slow, fast ] = [ nums[start], nums[nums[start]] ];
const isSame = () => slow === fast;
while (!isSame()) { /* Time O(N) */
slow = nums[slow];
fast = nums[nums[fast]];
}
fast = start;
return [ slow, fast ];
}
const moveSlow = (nums, slow, fast) => {
const isSame = () => slow === fast;
while (!isSame()) { /* Time O(N) */
slow = nums[slow];
fast = nums[fast];
}
return [ slow, fast ];
}