-
Notifications
You must be signed in to change notification settings - Fork 0
/
LargestRange.java
40 lines (39 loc) · 1018 Bytes
/
LargestRange.java
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
import java.util.*;
class Program {
// O(n) time | O(n) space
public static int[] largestRange(int[] array) {
// Write your code here.
// record each number in a hash table
Map<Integer, Boolean> records = new HashMap<>();
for (int num : array) {
records.put(num, true);
}
// explore possible ranges
int longestLength = 0;
int[] largestRange = new int[] { -1, -1 };
for (int num : array) {
if (!records.get(num)) { // skip those visited ones
continue;
}
records.put(num, false); // mark as visited
int length = 1;
int left = num - 1;
while (records.containsKey(left)) {
records.put(left, false); // mark as visited
left--;
length++;
}
int right = num + 1;
while (records.containsKey(right)) {
records.put(right, false); // mark as visited
right++;
length++;
}
if (length > longestLength) { // the longer range wins
longestLength = length;
largestRange = new int[] { left + 1, right - 1 };
}
}
return largestRange;
}
}