-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlargest-range.js
125 lines (94 loc) · 2.64 KB
/
largest-range.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
const assert = require('assert');
/**
* Largest Range
* Write a function that takes in an array of integers and returns an array of length 2 representing the largest range of numbers contained in that array. The first number in the output array should be the first number in the range while the second number should be the last number in the range. A range of numbers is defined as a set of numbers that come right after each other in the set of real integers. For instance, the output array [2, 6] represents the range {2, 3, 4, 5, 6}, which is a range of length 5. Note that numbers do not need to be ordered or adjacent in the array in order to form a range. Assume that there will only be one largest range.
*/
function largestRange(array) {
const nums = {};
let ans = [];
let largest = 0;
for (let n of array) {
nums[n] = true;
}
for (let n of array) {
let len = 1;
let min = n - 1;
let max = n + 1;
nums[n] = false;
while (nums[min] === true) {
nums[min] = false;
len++;
min--;
}
while (nums[max] === true) {
nums[max] = false;
len++;
max++;
}
if (len > largest) {
largest = len;
ans = [min + 1, max - 1];
}
}
return ans;
}
function largestRange2(array) {
const nums = {};
let ans = [];
let largest = 0;
for (let n of array) {
nums[n] = true;
}
for (let n of array) {
if (nums[n] === false) {
continue;
}
nums[n] = false;
let currLen = 1;
let left = n - 1;
let right = n + 1;
while (nums[left] === true) {
nums[left] = false;
currLen++;
left--;
}
while (nums[right] === true) {
nums[right] = false;
currLen++;
right++;
}
if (currLen > largest) {
largest = currLen;
ans = [left + 1, right - 1];
}
}
return ans;
}
function largestRangeBasic(array) {
if (array.length === 1) {
return [array[0], array[0]];
}
array.sort((a, b) => a - b);
let largest = 0;
let ans = [];
let i = 0;
let j = 0;
for (let l = array.length; j < l; j++) {
if (j === l - 1 || array[j] !== array[j + 1] - 1) {
let curr = j - i + 1;
if (curr > largest) {
largest = curr;
ans = [array[i], array[j]];
i = j + 1;
}
}
}
return ans;
}
console.time('Run Time');
assert.deepStrictEqual(largestRange([1]), [1, 1]);
assert.deepStrictEqual(largestRange([1, 2]), [1, 2]);
assert.deepStrictEqual(largestRange([4, 2, 1, 3]), [1, 4]);
assert.deepStrictEqual(largestRange([4, 2, 1, 3, 6]), [1, 4]);
assert.deepStrictEqual(largestRange([8, 4, 2, 10, 3, 6, 7, 9, 1]), [6, 10]);
console.timeEnd('Run Time');