|
5 | 5 | import java.util.Map; |
6 | 6 | import java.util.Set; |
7 | 7 |
|
8 | | -/** |
9 | | - * 128. Longest Consecutive Sequence |
10 | | -
|
11 | | - Given an unsorted array of integers, find the length of the longest consecutive elements sequence. |
12 | | -
|
13 | | - For example, |
14 | | - Given [100, 4, 200, 1, 3, 2], |
15 | | - The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. |
16 | | -
|
17 | | - Your algorithm should run in O(n) complexity. |
18 | | - */ |
19 | 8 | public class _128 { |
20 | | - public static class Solution1 { |
21 | | - public int longestConsecutive(int[] nums) { |
22 | | - Map<Integer, Integer> map = new HashMap(); |
23 | | - //<value, index> |
24 | | - UnionFind uf = new UnionFind(nums); |
25 | | - for (int i = 0; i < nums.length; i++) { |
26 | | - if (map.containsKey(nums[i])) { |
27 | | - continue; |
| 9 | + public static class Solution1 { |
| 10 | + public int longestConsecutive(int[] nums) { |
| 11 | + Map<Integer, Integer> map = new HashMap(); |
| 12 | + //<value, index> |
| 13 | + UnionFind uf = new UnionFind(nums); |
| 14 | + for (int i = 0; i < nums.length; i++) { |
| 15 | + if (map.containsKey(nums[i])) { |
| 16 | + continue; |
| 17 | + } |
| 18 | + map.put(nums[i], i); |
| 19 | + if (map.containsKey(nums[i] - 1)) { |
| 20 | + uf.union(i, map.get(nums[i] - 1)); |
| 21 | + //note: we want to union this index and nums[i]-1's root index which we can get from the map |
| 22 | + } |
| 23 | + if (map.containsKey(nums[i] + 1)) { |
| 24 | + uf.union(i, map.get(nums[i] + 1)); |
| 25 | + } |
| 26 | + } |
| 27 | + return uf.maxUnion(); |
28 | 28 | } |
29 | | - map.put(nums[i], i); |
30 | | - if (map.containsKey(nums[i] - 1)) { |
31 | | - uf.union(i, map.get(nums[i] - 1)); |
32 | | - //note: we want to union this index and nums[i]-1's root index which we can get from the map |
33 | | - } |
34 | | - if (map.containsKey(nums[i] + 1)) { |
35 | | - uf.union(i, map.get(nums[i] + 1)); |
36 | | - } |
37 | | - } |
38 | | - return uf.maxUnion(); |
39 | | - } |
40 | 29 |
|
41 | | - class UnionFind { |
42 | | - int[] ids; |
43 | | - |
44 | | - public UnionFind(int[] nums) { |
45 | | - ids = new int[nums.length]; |
46 | | - for (int i = 0; i < nums.length; i++) { |
47 | | - ids[i] = i; |
48 | | - } |
49 | | - } |
50 | | - |
51 | | - public void union(int i, int j) { |
52 | | - int x = find(ids, i); |
53 | | - int y = find(ids, j); |
54 | | - ids[x] = y; |
55 | | - } |
56 | | - |
57 | | - public int find(int[] ids, int i) { |
58 | | - while (i != ids[i]) { |
59 | | - ids[i] = ids[ids[i]]; |
60 | | - i = ids[i]; |
61 | | - } |
62 | | - return i; |
63 | | - } |
64 | | - |
65 | | - public boolean connected(int i, int j) { |
66 | | - return find(ids, i) == find(ids, j); |
67 | | - } |
68 | | - |
69 | | - public int maxUnion() { |
70 | | - //this is O(n) |
71 | | - int max = 0; |
72 | | - int[] count = new int[ids.length]; |
73 | | - for (int i = 0; i < ids.length; i++) { |
74 | | - count[find(ids, i)]++; |
75 | | - max = max < count[find(ids, i)] ? count[find(ids, i)] : max; |
| 30 | + class UnionFind { |
| 31 | + int[] ids; |
| 32 | + |
| 33 | + public UnionFind(int[] nums) { |
| 34 | + ids = new int[nums.length]; |
| 35 | + for (int i = 0; i < nums.length; i++) { |
| 36 | + ids[i] = i; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public void union(int i, int j) { |
| 41 | + int x = find(ids, i); |
| 42 | + int y = find(ids, j); |
| 43 | + ids[x] = y; |
| 44 | + } |
| 45 | + |
| 46 | + public int find(int[] ids, int i) { |
| 47 | + while (i != ids[i]) { |
| 48 | + ids[i] = ids[ids[i]]; |
| 49 | + i = ids[i]; |
| 50 | + } |
| 51 | + return i; |
| 52 | + } |
| 53 | + |
| 54 | + public boolean connected(int i, int j) { |
| 55 | + return find(ids, i) == find(ids, j); |
| 56 | + } |
| 57 | + |
| 58 | + public int maxUnion() { |
| 59 | + //this is O(n) |
| 60 | + int max = 0; |
| 61 | + int[] count = new int[ids.length]; |
| 62 | + for (int i = 0; i < ids.length; i++) { |
| 63 | + count[find(ids, i)]++; |
| 64 | + max = max < count[find(ids, i)] ? count[find(ids, i)] : max; |
| 65 | + } |
| 66 | + return max; |
| 67 | + } |
76 | 68 | } |
77 | | - return max; |
78 | | - } |
79 | 69 | } |
80 | | - } |
81 | | - |
82 | | - public static class Solution2 { |
83 | | - //inspired by this solution: https://discuss.leetcode.com/topic/25493/simple-fast-java-solution-using-set |
84 | | - public int longestConsecutive(int[] nums) { |
85 | | - if (nums == null || nums.length == 0) { |
86 | | - return 0; |
87 | | - } |
88 | | - |
89 | | - Set<Integer> set = new HashSet(); |
90 | | - for (int i : nums) { |
91 | | - set.add(i); |
92 | | - } |
93 | | - int max = 1; |
94 | | - |
95 | | - for (int num : nums) { |
96 | | - if (set.remove(num)) { |
97 | | - int val = num; |
98 | | - int count = 1; |
99 | | - while (set.remove(val - 1)) { |
100 | | - val--;//we find all numbers that are smaller than num and remove them from the set |
101 | | - } |
102 | | - count += num - val; |
103 | | - |
104 | | - val = num; |
105 | | - while (set.remove(val + 1)) { |
106 | | - val++;//then we find all numbers that are bigger than num and also remove them from the set |
107 | | - } |
108 | | - count += val - num; |
109 | | - |
110 | | - max = Math.max(max, count); |
| 70 | + |
| 71 | + public static class Solution2 { |
| 72 | + //inspired by this solution: https://discuss.leetcode.com/topic/25493/simple-fast-java-solution-using-set |
| 73 | + public int longestConsecutive(int[] nums) { |
| 74 | + if (nums == null || nums.length == 0) { |
| 75 | + return 0; |
| 76 | + } |
| 77 | + |
| 78 | + Set<Integer> set = new HashSet(); |
| 79 | + for (int i : nums) { |
| 80 | + set.add(i); |
| 81 | + } |
| 82 | + int max = 1; |
| 83 | + |
| 84 | + for (int num : nums) { |
| 85 | + if (set.remove(num)) { |
| 86 | + int val = num; |
| 87 | + int count = 1; |
| 88 | + while (set.remove(val - 1)) { |
| 89 | + val--;//we find all numbers that are smaller than num and remove them from the set |
| 90 | + } |
| 91 | + count += num - val; |
| 92 | + |
| 93 | + val = num; |
| 94 | + while (set.remove(val + 1)) { |
| 95 | + val++;//then we find all numbers that are bigger than num and also remove them from the set |
| 96 | + } |
| 97 | + count += val - num; |
| 98 | + |
| 99 | + max = Math.max(max, count); |
| 100 | + } |
| 101 | + } |
| 102 | + return max; |
111 | 103 | } |
112 | | - } |
113 | | - return max; |
114 | 104 | } |
115 | | - } |
116 | | - |
117 | | - public static class Solution3 { |
118 | | - public int longestConsecutive(int[] nums) { |
119 | | - HashSet<Integer> numSet = new HashSet<>(); |
120 | | - for (int num : nums) { |
121 | | - numSet.add(num); |
122 | | - } |
123 | | - |
124 | | - int longestStreak = 0; |
125 | | - for (int num : nums) { |
126 | | - if (!numSet.contains(num - 1)) { |
127 | | - int currentNum = num; |
128 | | - int currentStreak = 1; |
129 | | - |
130 | | - while (numSet.contains(currentNum + 1)) { |
131 | | - currentNum += 1; |
132 | | - currentStreak += 1; |
133 | | - } |
134 | | - longestStreak = Math.max(longestStreak, currentStreak); |
| 105 | + |
| 106 | + public static class Solution3 { |
| 107 | + public int longestConsecutive(int[] nums) { |
| 108 | + HashSet<Integer> numSet = new HashSet<>(); |
| 109 | + for (int num : nums) { |
| 110 | + numSet.add(num); |
| 111 | + } |
| 112 | + |
| 113 | + int longestStreak = 0; |
| 114 | + for (int num : nums) { |
| 115 | + if (!numSet.contains(num - 1)) { |
| 116 | + int currentNum = num; |
| 117 | + int currentStreak = 1; |
| 118 | + |
| 119 | + while (numSet.contains(currentNum + 1)) { |
| 120 | + currentNum += 1; |
| 121 | + currentStreak += 1; |
| 122 | + } |
| 123 | + longestStreak = Math.max(longestStreak, currentStreak); |
| 124 | + } |
| 125 | + } |
| 126 | + return longestStreak; |
135 | 127 | } |
136 | | - } |
137 | | - return longestStreak; |
138 | 128 | } |
139 | | - } |
140 | 129 | } |
0 commit comments