forked from zeplios/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
149-Max Points on a Line.c
93 lines (88 loc) · 2.58 KB
/
149-Max Points on a Line.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
/**
* Definition for a point.
* struct Point {
* int x;
* int y;
* }
*/
struct HashNode {
double key;
int value;
struct HashNode* next;
};
struct HashTable {
struct HashNode** buckets;
int bucketSize;
};
struct HashTable* hashmap_create(int size) {
struct HashTable * table = malloc(sizeof(struct HashTable));
table->bucketSize = size;
table->buckets = malloc(sizeof(struct HashTable *) * size);
memset(table->buckets, 0, sizeof(struct HashTable *) * size);
return table;
}
struct HashNode* hashmap_get(struct HashTable* table, double key) {
int hash = (int)key % table->bucketSize;
while (hash < 0) hash += table->bucketSize;
struct HashNode* node = table->buckets[hash];
while(node != NULL && node->key <= key) {
if(node->key == key) {
return node;
}
node = node->next;
}
return NULL;
}
struct HashNode* hashmap_set(struct HashTable* table, double key, int value) {
int hash = (int)key % table->bucketSize;
while (hash < 0) hash += table->bucketSize;
struct HashNode* node = table->buckets[hash];
if (node == NULL || node->key > key) {
struct HashNode * tmp = malloc(sizeof(struct HashNode));
tmp->key = key;
tmp->value = value;
tmp->next = node; // node != NULL ? node : NULL
table->buckets[hash] = tmp;
return tmp;
}
if (node->key == key) {
node->value = value;
return node;
}
while (node->next != NULL && node->next->key < key) {
node = node->next;
}
if (node->next != NULL && node->next->key == key) {
node->next->value = value;
return node->next;
}
struct HashNode * tmp = malloc(sizeof(struct HashNode));
tmp->key = key;
tmp->value = value;
tmp->next = node->next;
node->next = tmp;
return tmp;
}
int maxPoints(struct Point* points, int pointsSize) {
int maxp = 0, i, j;
for (i = 0 ; i < pointsSize ; i++) {
struct HashTable * table = hashmap_create(pointsSize);
int dup = 0;
int max = 0;
for (j = i+1 ; j < pointsSize ; j++) {
if (points[i].x == points[j].x && points[i].y == points[j].y) {
dup++;
continue;
}
int dx = points[j].x - points[i].x;
int dy = points[j].y - points[i].y;
double k = (dx != 0 ? dy * 1.0 / dx : INT_MAX);
struct HashNode * node = hashmap_get(table, k);
if (node != NULL) node->value++;
else node = hashmap_set(table, k, 1);
max = max > node->value ? max : node->value;
}
maxp = maxp > 1 + max + dup ? maxp : 1 + max + dup;
}
return maxp;
}