Skip to content

Commit 0298884

Browse files
committed
update: 164 & 287
1 parent 85eedb0 commit 0298884

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
5858
|137|[Single Number II](https://leetcode.com/problems/single-number-ii/) | [JavaScript](./src/single-number-ii/res.js)|Medium|
5959
|151|[Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [JavaScript](./src/reverse-words-in-a-string/res.js)|Medium|
6060
|152|[Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) | [JavaScript](./src/maximum-product-subarray/res.js)|Medium|
61+
|164|[Maximum Gap](https://leetcode.com/problems/maximum-gap/) | [JavaScript](./src/maximum-gap/res.js)|Hard|
6162
|165|[Compare Version Numbers](https://leetcode.com/problems/compare-version-numbers/) | [JavaScript](./src/compare-version-numbers/res.js)|Medium|
6263
|175|[Combine Two Tables](https://leetcode.com/problems/combine-two-tables/)| [SQL](./src/combine-two-tables/res.txt)|Easy|
6364
|176|[Second Highest Salary](https://leetcode.com/problems/second-highest-salary/)| [SQL](./src/second-highest-salary/res.txt)|Easy|
@@ -80,6 +81,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
8081
|240|[Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/) | [JavaScript](./src/search-a-2d-matrix-ii/res.js)|Medium|
8182
|274|[H-Index](https://leetcode.com/problems/h-index/) | [JavaScript](./src/h-index/res.js)|Medium|
8283
|275|[H-Index II](https://leetcode.com/problems/h-index-ii/) | [JavaScript](./src/h-index-ii/res.js)|Medium|
84+
|287|[Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | [JavaScript](./src/find-the-duplicate-number/res.js)|Medium|
8385
|299|[Bulls and Cows](https://leetcode.com/problems/bulls-and-cows/) | [JavaScript](./src/bulls-and-cows/res.js)|Medium|
8486
|307|[Range Sum Query - Mutable](https://leetcode.com/problems/range-sum-query-mutable/) | [JavaScript](./src/range-sum-query-mutable/res.js)|Medium|
8587
|309|[Best Time to Buy and Sell Stock with Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/) | [JavaScript](./src/best-time-to-buy-and-sell-stock-with-cooldown/res.js)|Medium|

src/find-the-duplicate-number/res.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Floyd判圈算法
3+
* @param {number[]} nums
4+
* @return {number}
5+
*/
6+
var findDuplicate = function(nums) {
7+
let tortoise = nums[0];
8+
let hare = nums[0];
9+
10+
do {
11+
tortoise = nums[tortoise];
12+
hare = nums[nums[hare]];
13+
} while (tortoise !== hare);
14+
15+
hare = nums[0]
16+
while (tortoise !== hare) {
17+
hare = nums[hare]
18+
tortoise = nums[tortoise]
19+
}
20+
21+
return tortoise;
22+
};

src/maximum-gap/res.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* https://leetcode-cn.com/problems/maximum-gap/solution/zui-da-jian-ju-by-leetcode/
3+
* @param {number[]} nums
4+
* @return {number}
5+
*/
6+
var maximumGap = function(nums) {
7+
const len = nums.length;
8+
if (len < 2) return 0;
9+
10+
let mini = Number.MAX_SAFE_INTEGER,
11+
maxi = Number.MIN_SAFE_INTEGER;
12+
13+
nums.forEach(e => {
14+
if (e < mini) {
15+
mini = e;
16+
}
17+
if (e > maxi) {
18+
maxi = e;
19+
}
20+
})
21+
22+
23+
let bucketSize = Math.max(1, (maxi - mini) / (nums.length - 1)); // bucket size or capacity
24+
let bucketNum = parseInt((maxi - mini) / bucketSize) + 1; // number of buckets
25+
const buckets = [];
26+
for (let index = 0; index < bucketNum; index++) {
27+
buckets.push({});
28+
}
29+
30+
// console.log(buckets);
31+
nums.forEach(num => {
32+
let bucketIdx = parseInt((num - mini) / bucketSize); // locating correct bucket
33+
// console.log('bucketIdx is ', bucketIdx);
34+
buckets[bucketIdx].used = true;
35+
buckets[bucketIdx].minval = Math.min(num, buckets[bucketIdx].minval || Number.MAX_SAFE_INTEGER);
36+
buckets[bucketIdx].maxval = Math.max(num, buckets[bucketIdx].maxval || Number.MIN_SAFE_INTEGER);
37+
});
38+
39+
let prevBucketMax = mini, maxGap = 0;
40+
buckets.forEach(bucket => {
41+
if (bucket.used) {
42+
maxGap = Math.max(maxGap, bucket.minval - prevBucketMax);
43+
prevBucketMax = bucket.maxval;
44+
}
45+
});
46+
47+
return maxGap;
48+
};

0 commit comments

Comments
 (0)