Find Minimum in Rotated Sorted Array II LeetCode solution in JavaScript

1
/**
2
* @param {number[]} nums
3
* @return {number}
4
*/
5
var findMin = function (nums) {
6
let min = Infinity;
7
let l = 0;
8
let r = nums.length - 1;
9
while (l <= r) {
10
const m = Math.floor((l + r) / 2);
11

12
// Eliminate dupes ......................... only difference from #153
13
while (l < m && nums[l] === nums[m]) l++;
14
while (r > m && nums[r] === nums[m]) r--;
15
// .........................................
16

17
// locate the sorted side, the opposite side has the break point
18
// min should be on the side that has the break point, so continue search on that side
19
if (nums[l] <= nums[m]) {
20
// left side is sorted (or l & m are the same index)
21
// brake is on right side
22
min = Math.min(min, nums[l]);
23
l = m + 1;
24
} else {
25
// right side is sorted
26
// break is on left side
27
min = Math.min(min, nums[m]);
28
r = m - 1;
29
}
30
}
31
return min;
32
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0