-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path939. Minimum Area Rectangle.c
114 lines (83 loc) · 2.06 KB
/
939. Minimum Area Rectangle.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
/*
939. Minimum Area Rectangle
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct.
*/
#define HF 1021
typedef struct e_s {
int y1;
int y2;
int x;
struct e_s *shadow;
} e_t;
typedef struct {
e_t *e[HF];
e_t buff[250000];
int n;
} h_t;
int hash_code(int y1, int y2) {
int hc = y1 * 33 + y2;
return hc % HF;
}
e_t *lookup(h_t *ht, int y1, int y2) {
int hc = hash_code(y1, y2);
e_t *e = ht->e[hc];
while (e && (e->y1 != y1 || e->y2 != y2)) e = e->shadow;
return e;
}
void insert(h_t *ht, int y1, int y2, int x) {
e_t *e = &ht->buff[ht->n ++];
int hc = hash_code(y1, y2);
e->y1 = y1;
e->y2 = y2;
e->x = x;
e->shadow = ht->e[hc];
ht->e[hc] = e;
}
int cmp(const void *a, const void *b) {
int *p1 = *(int **)a;
int *p2 = *(int **)b;
if (p1[0] < p2[0]) return -1;
if (p1[0] > p2[0]) return 1;
if (p1[1] < p2[1]) return -1;
return 1;
}
int minAreaRect(int** points, int pointsSize, int* pointsColSize){
int i, j, x, y1, y2, area, ans = 0;
e_t *e;
h_t ht = { 0 };
// x ascending, y ascending
qsort(points, pointsSize, sizeof(int *), cmp);
for (int i = 0; i < pointsSize - 1; i ++) {
x = points[i][0];
y1 = points[i][1];
for (j = i + 1; j < pointsSize && points[j][0] == x; j ++) {
y2 = points[j][1];
e = lookup(&ht, y1, y2);
if (e) {
area = (x - e->x) * (y2 - y1);
if (ans == 0 || ans > area) ans = area;
e->x = x;
} else {
insert(&ht, y1, y2, x);
}
}
}
return ans;
}
/*
Difficulty:Medium
*/