-
Notifications
You must be signed in to change notification settings - Fork 154
/
missing-ranges.js
52 lines (42 loc) · 1.08 KB
/
missing-ranges.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
/**
* Missing Ranges
*
* Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, upper],
* return its missing ranges.
*
* Example:
*
* Input: nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99,
* Output: ["2", "4->49", "51->74", "76->99"]
*/
/**
* @param {number[]} nums
* @param {number} lower
* @param {number} upper
* @return {string[]}
*/
const findMissingRanges = (nums, lower, upper) => {
const res = [];
// the next number we need to find
let next = lower;
for (let i = 0; i < nums.length; i++) {
// not within the range yet
if (nums[i] < next) continue;
// continue to find the next one
if (nums[i] === next) {
next++;
continue;
}
// get the missing range string format
res.push(getRange(next, nums[i] - 1));
// now we need to find the next number
next = nums[i] + 1;
}
// do a final check
if (next <= upper) {
res.push(getRange(next, upper));
}
return res;
};
const getRange = (n1, n2) => (n1 === n2 ? `${n1}` : `${n1}->${n2}`);
export { findMissingRanges };