Skip to content

Commit 0f70dc8

Browse files
solves #128 in java
1 parent 2ea90cd commit 0f70dc8

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
| 121 | [Best Time to Buy and Sell Stocks](https://leetcode.com/problems/best-time-to-buy-and-sell-stock) | [![Java](assets/java.png)](src/BestTimeToBuyAndSellStock.java) [![Python](assets/python.png)](python/best_time_to_buy_and_sell_stock.py) | |
116116
| 122 | [Best Time to Buy and Sell Stocks II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii) | [![Java](assets/java.png)](src/BestTimeToBuyAndSellStockII.java) [![Python](assets/python.png)](python/best_time_to_buy_and_sell_stock_ii.py) | |
117117
| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome) | [![Java](assets/java.png)](src/ValidPalindrome.java) [![Python](assets/python.png)](python/valid_palindrome.py) | |
118-
| 128 | [Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence) | | |
118+
| 128 | [Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence) | [![Java](assets/java.png)](src/LongestConsecutiveSequence.java) | |
119119
| 129 | [Sum Root to Leaf Numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers) | | |
120120
| 130 | [Surrounded Regions](https://leetcode.com/problems/surrounded-regions) | | |
121121
| 131 | [Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning) | | |

src/LongestConsecutiveSequence.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// https://leetcode.com/problems/longest-consecutive-sequence
2+
// T: O(n)
3+
// S: O(n)
4+
5+
import java.util.HashMap;
6+
import java.util.HashSet;
7+
import java.util.Map;
8+
import java.util.Set;
9+
10+
public class LongestConsecutiveSequence {
11+
private static class Node {
12+
Node next;
13+
Node previous;
14+
}
15+
16+
public int longestConsecutive(int[] nums) {
17+
final Map<Integer, Node> nodes = createDisjointSets(nums);
18+
return longestConsecutiveSequenceLength(nodes);
19+
}
20+
21+
private Map<Integer, Node> createDisjointSets(int[] array) {
22+
final Map<Integer, Node> result = new HashMap<>();
23+
for (int element : array) {
24+
if (result.containsKey(element)) continue;
25+
26+
Node current = new Node();
27+
28+
if (result.containsKey(element - 1)) {
29+
Node left = result.get(element - 1);
30+
left.next = current;
31+
current.previous = left;
32+
}
33+
34+
if (result.containsKey(element + 1)) {
35+
Node right = result.get(element + 1);
36+
current.next = right;
37+
right.previous = current;
38+
}
39+
40+
result.put(element, current);
41+
}
42+
43+
return result;
44+
}
45+
46+
private int longestConsecutiveSequenceLength(Map<Integer, Node> nodes) {
47+
final Set<Node> processedNodes = new HashSet<>();
48+
int maxLength = 0;
49+
50+
for (Node node : nodes.values()) {
51+
int length = getLength(node, processedNodes);
52+
maxLength = Math.max(maxLength, length);
53+
}
54+
55+
return maxLength;
56+
}
57+
58+
private int getLength(Node node, Set<Node> processedNodes) {
59+
if (node == null) return 0;
60+
if (processedNodes.contains(node)) return 0;
61+
processedNodes.add(node);
62+
return 1 + getLength(node.previous, processedNodes) + getLength(node.next, processedNodes);
63+
}
64+
}

0 commit comments

Comments
 (0)