forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0128-longest-consecutive-sequence.c
158 lines (147 loc) · 3.46 KB
/
0128-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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#define INIT_HASH_SIZE 4096
// Represent an element in a hash table.
typedef struct Hash {
int key;
struct Hash *next;
} Hash;
Hash **InitHash() {
Hash **h = (Hash**)calloc(INIT_HASH_SIZE, sizeof(Hash*));
assert(h);
return h;
}
Hash *NewHash(int key) {
Hash *h = (Hash*)malloc(sizeof(Hash));
assert(h);
h->key = key;
h->next = NULL;
return h;
}
int HashKey(int key) {
while(key < 0) key += INIT_HASH_SIZE;
return (key % INIT_HASH_SIZE);
}
void AddHash(Hash **hash, int key) {
assert(hash);
int hash_key = HashKey(key);
Hash **head = hash + hash_key;
if (!*head) {
*head = NewHash(key);
} else {
Hash *h = *head;
while (h) {
if (h->key == key) {
return;
} else if (!h->next) {
h->next = NewHash(key);
return;
}
h = h->next;
}
}
}
// If hash not round return 0 on purpose.
int GetHash(Hash **hash, int key) {
assert(hash);
Hash * h = hash[HashKey(key)];
while(h) {
if (h->key == key) {
return 1;
}
h = h->next;
}
return 0;
}
int longestConsecutive(int* nums, int numsSize){
Hash **hash = InitHash();
int i, len, n, maxLen = 0;
for (i = 0; i < numsSize; i++) {
AddHash(hash, nums[i]);
}
for (i = 0; i < numsSize; i++) {
n = nums[i];
len = 1;
if (GetHash(hash, n - 1) == 0) {
while(GetHash(hash, ++n) == 1) {
len++;
}
}
maxLen = (len > maxLen) ? len : maxLen;
}
return maxLen;
}
//
// According to the official solution, using hash map will be considered as O(n).
//
// Alternative solution 1 - With uthash, the runtime is poorer. It is a little surprise.
//
// typedef struct Hash {
// int num;
// UT_hash_handle hh;
// } Hash;
//
// void AddHash(Hash **hash, int num) {
// Hash *entry;
// HASH_FIND_INT(*hash, &num, entry);
// if (!entry) {
// entry = malloc(sizeof(Hash));
// entry->num = num;
// HASH_ADD_INT(*hash, num, entry);
// }
// }
//
// Hash * GetHash(Hash **hash, int num) {
// Hash *entry = NULL;
// HASH_FIND_INT(*hash, &num, entry);
// return entry;
// }
//
// int longestConsecutive(int* nums, int numsSize){
// Hash *root = NULL;
// int i, len, n, maxLen = 0;
//
// for (i = 0; i < numsSize; i++) {
// AddHash(&root, nums[i]);
// }
// for (i = 0; i < numsSize; i++) {
// n = nums[i];
// len = 1;
// if (!GetHash(&root, n - 1)) {
// while(GetHash(&root, ++n)) {
// len++;
// }
// }
// maxLen = (len > maxLen) ? len : maxLen;
// }
//
// return maxLen;
// }
//
//
// Alternative solution 2 - With sorting, O(nlogn), the runtime is the best.
//
// int compare(const void *a, const void *b) {
// return *(int*)a - *(int*)b;
// }
//
// int longestConsecutive(int* nums, int numsSize){
// int i, len, max;
// if (numsSize <= 0) {
// return 0;
// }
// len = 1;
// max = 1;
// qsort(nums, numsSize, sizeof(int), compare);
// for (i = 0; i < numsSize - 1; i++) {
// if (nums[i] + 1 == nums[i + 1]) {
// len++;
// } else if (nums[i] == nums[i + 1]){
// continue;
// } else {
// len = 1;
// }
// max = (max > len) ? max : len;
// }
//
// return max;
// }
//