My solution looks at each input only once, and uses a HashTable to store the boundaries of the Longest Common Subsequence.

- If the current num[i] is in the hash table, return the value
- if the previous number is in the hash table, remember it's value
- if the next number is in the hash table, remember it's value
- update the current number by summing the previous and next values and adding 1
- update the values at the boundary

After submitting and having my solution accepted, I'm wondering what I could do to speed it up?

```
public int longestConsecutive(int[] num) {
if (num.length < 2) {
return num.length;
}
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
int ans = 0;
for (int i = 0; i < num.length; i++){
if (map.containsKey(num[i])) {
continue;
}
int before = 0;
if (map.containsKey(num[i]-1)) {
before = map.get(num[i]-1);
}
int after = 0;
if (map.containsKey(num[i]+1)) {
after = map.get(num[i]+1);
}
// get the current value
int current = before + after + 1;
// update the current value
map.put(num[i], current);
// update the edges
if (before != 0) {
map.put(num[i]-before, current);
}
if (after != 0) {
map.put(num[i]+after, current);
}
ans = Math.max(ans, current);
}
return ans;
}
```