-
Notifications
You must be signed in to change notification settings - Fork 27
/
128. Longest Consecutive Sequence.c
86 lines (65 loc) · 1.92 KB
/
128. Longest Consecutive Sequence.c
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
128. Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
For example,
Given [100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.
Your algorithm should run in O(n) complexity.
*/
typedef struct e_s {
int key;
int len;
struct e_s *shadow;
} e_t;
#define HSZ 1000
void put(e_t **htable, int key, int len) {
e_t *e = malloc(sizeof(e_t));
//assert(e);
e->key = key;
e->len = len;
e->shadow = htable[key % HSZ];
htable[key % HSZ] = e;
}
int lookup(e_t **htable, int key, int **lp) {
e_t *e = htable[key % HSZ];
// Runtime Error Message:
// Line 20: member access within misaligned address 0x000001ea62f4 for type 'struct e_t',
// which requires 8 byte alignment
while (e && e->key != key) {
e = e->shadow;
}
if (e) {
*lp = &e->len;
return e->len;
}
return 0;
}
int longestConsecutive(int* nums, int numsSize) {
int i, l, r, x, len = 0;
e_t *htable[HSZ] = { 0 };
int *llp, *rlp;
// make a hash table to store the length of consecutive of current number
// and update the length of consecutive of left and right boundary
for (i = 0; i < numsSize; i ++) {
if (lookup(htable, nums[i], &llp) == 0) {
l = lookup(htable, nums[i] - 1, &llp);
r = lookup(htable, nums[i] + 1, &rlp);
x = l + r + 1;
put(htable, nums[i], x);
if (l) *llp = x;
if (r) *rlp = x;
if (len < x) len = x;
}
}
// TODO: clean up memory
return len;
}
/*
Difficulty:Hard
Total Accepted:109K
Total Submissions:296.1K
Companies Google Facebook
Related Topics Array Union Find
Similar Questions
Binary Tree Longest Consecutive Sequence
*/