Skip to content

Commit e81f4e5

Browse files
Kevin Naughton JrKevin Naughton Jr
authored andcommitted
added modified solution for LongestConsecutiveSequence.java
1 parent f9c25d6 commit e81f4e5

File tree

3 files changed

+60
-45
lines changed

3 files changed

+60
-45
lines changed

company/facebook/LongestConsecutiveSequence.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,32 @@
66

77
// Your algorithm should run in O(n) complexity.
88

9-
public class LongestConsecutiveSequence {
9+
class LongestConsecutiveSequence {
1010
public int longestConsecutive(int[] nums) {
11-
int res = 0;
12-
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
11+
if(nums == null || nums.length == 0) {
12+
return 0;
13+
}
14+
15+
Set<Integer> set = new HashSet<Integer>();
16+
for(int n: nums) {
17+
set.add(n);
18+
}
1319

14-
for(int n : nums) {
15-
if(!map.containsKey(n)) {
16-
int left = (map.containsKey(n - 1)) ? map.get(n - 1) : 0;
17-
int right = (map.containsKey(n + 1)) ? map.get(n + 1) : 0;
20+
int maxLength = 0;
21+
for(int n: set) {
22+
if(!set.contains(n - 1)) {
23+
int current = n;
24+
int currentMax = 1;
1825

19-
int sum = left + right + 1;
20-
map.put(n, sum);
21-
res = Math.max(res, sum);
26+
while(set.contains(n + 1)) {
27+
currentMax++;
28+
n++;
29+
}
2230

23-
map.put(n - left, sum);
24-
map.put(n + right, sum);
25-
} else {
26-
continue;
31+
maxLength = Math.max(maxLength, currentMax);
2732
}
2833
}
2934

30-
return res;
35+
return maxLength;
3136
}
3237
}

company/google/LongestConsecutiveSequence.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,32 @@
66

77
// Your algorithm should run in O(n) complexity.
88

9-
public class LongestConsecutiveSequence {
9+
class LongestConsecutiveSequence {
1010
public int longestConsecutive(int[] nums) {
11-
int res = 0;
12-
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
11+
if(nums == null || nums.length == 0) {
12+
return 0;
13+
}
14+
15+
Set<Integer> set = new HashSet<Integer>();
16+
for(int n: nums) {
17+
set.add(n);
18+
}
1319

14-
for(int n : nums) {
15-
if(!map.containsKey(n)) {
16-
int left = (map.containsKey(n - 1)) ? map.get(n - 1) : 0;
17-
int right = (map.containsKey(n + 1)) ? map.get(n + 1) : 0;
20+
int maxLength = 0;
21+
for(int n: set) {
22+
if(!set.contains(n - 1)) {
23+
int current = n;
24+
int currentMax = 1;
1825

19-
int sum = left + right + 1;
20-
map.put(n, sum);
21-
res = Math.max(res, sum);
26+
while(set.contains(n + 1)) {
27+
currentMax++;
28+
n++;
29+
}
2230

23-
map.put(n - left, sum);
24-
map.put(n + right, sum);
25-
} else {
26-
continue;
31+
maxLength = Math.max(maxLength, currentMax);
2732
}
2833
}
2934

30-
return res;
35+
return maxLength;
3136
}
3237
}

leetcode/array/LongestConsecutiveSequence.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,32 @@
66

77
// Your algorithm should run in O(n) complexity.
88

9-
public class LongestConsecutiveSequence {
9+
class LongestConsecutiveSequence {
1010
public int longestConsecutive(int[] nums) {
11-
int res = 0;
12-
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
11+
if(nums == null || nums.length == 0) {
12+
return 0;
13+
}
14+
15+
Set<Integer> set = new HashSet<Integer>();
16+
for(int n: nums) {
17+
set.add(n);
18+
}
1319

14-
for(int n : nums) {
15-
if(!map.containsKey(n)) {
16-
int left = (map.containsKey(n - 1)) ? map.get(n - 1) : 0;
17-
int right = (map.containsKey(n + 1)) ? map.get(n + 1) : 0;
20+
int maxLength = 0;
21+
for(int n: set) {
22+
if(!set.contains(n - 1)) {
23+
int current = n;
24+
int currentMax = 1;
1825

19-
int sum = left + right + 1;
20-
map.put(n, sum);
21-
res = Math.max(res, sum);
26+
while(set.contains(n + 1)) {
27+
currentMax++;
28+
n++;
29+
}
2230

23-
map.put(n - left, sum);
24-
map.put(n + right, sum);
25-
} else {
26-
continue;
31+
maxLength = Math.max(maxLength, currentMax);
2732
}
2833
}
2934

30-
return res;
35+
return maxLength;
3136
}
3237
}

0 commit comments

Comments
 (0)