Question 3: Given an array, please get the length of the longest consecutive sequence. A consecutive sequence is an arithmetic sequence with common difference 1. The element order in the consecutive sequence is not necessarily same as the element order in the array. The solution should not cost more than O(n) time and space if the length of the input array is n. For example, in the array {1, 6, 3, 5, 9, 7}, the longest consecutive sequence is 5, 6, and 7 whose length is 3.
参考:http://codercareer.blogspot.com/2014/03/no-53-longest-arithmetic-sequence.html
/* Copyleft: Ming Lin <minggr@gmail.com> */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct element {
int key;
struct element *next;
struct element *tail;
};
struct hash {
int size;
struct element **head;
int first;
};
struct hash *hash_init(int n)
{
struct hash *h;
int size;
h = malloc(sizeof(struct hash));
h->size = n;
size = n * sizeof(struct element *);
h->head = malloc(size);
memset(h->head, 0, size);
h->first = -1;
return h;
}
int hash_index(struct hash *h, int key)
{
if (key < 0)
key = -key;
return key % h->size;
}
void hash_add(struct hash *h, struct element *e)
{
int i = hash_index(h, e->key);
if (h->head[i]) {
h->head[i]->tail->next = e;
h->head[i]->tail = e;
} else {
h->head[i] = e;
h->head[i]->tail = e;
}
}
int hash_del(struct hash *h, int key)
{
struct element *e, *prev_e;
int i = hash_index(h, key);
prev_e = NULL;
e = h->head[i];
while (e) {
if (e->key == key) {
/* e is header */
if (!prev_e) {
if (!e->next) /* e is the only element */
h->head[i] = NULL;
else
h->head[i] = e->next;
} else if (!e->next) { /* e is tail */
h->head[i]->tail = prev_e;
prev_e->next = NULL;
} else {
prev_e->next = e->next;
}
free(e);
return 1;
}
prev_e = e;
e = e->next;
}
return 0;
}
struct element *hash_first(struct hash *h)
{
int i;
for (i = 0; i < h->size; i++) {
if (h->head[i])
return h->head[i];
}
return NULL;
}
void hash_build(struct hash *h, int *data, int n)
{
struct element *e;
int i;
for (i = 0; i < n; i++) {
e = malloc(sizeof(struct element));
e->key = data[i];
e->next = e->tail = NULL;
hash_add(h, e);
}
}
void hash_dump_one(struct element *e) {
printf(" ");
while (e) {
printf("%d ", e->key);
e = e->next;
}
printf("\n");
}
void hash_dump(struct hash *h)
{
int i;
for (i = 0; i < h->size; i++) {
if (h->head[i]) {
printf("Index %d:\n", i);
hash_dump_one(h->head[i]);
}
}
}
int longest_consecutive_sequence(struct hash *h)
{
struct element *e;
int max = -1;
int count;
int tmp;
int key;
while (e = hash_first(h)) {
count = 0;
tmp = e->key-1;
while (hash_del(h, tmp)) {
tmp--;
count++;
}
tmp = e->key+1;
while (hash_del(h, tmp)) {
tmp++;
count++;
}
hash_del(h, e->key);
count++;
if (count > max)
max = count;
}
return max;
}
int main()
{
struct hash *h;
//int data[] = {1, 6, 3, 5, 9, 7};
//int n = 6;
int data[] = { 1, 300, 3, 2, 101, 4, 102 };
int n = 7;
h = hash_init(n);
hash_build(h, data, n);
hash_dump(h);
printf("longest_consecutive_sequence: %d\n", longest_consecutive_sequence(h));
return 0;
}