-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.cpp
123 lines (98 loc) · 2.64 KB
/
container.cpp
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
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <vector>
using namespace std;
struct point{
int x;
int y;
point(int a, int b) : x(a), y(b){}
};
class Solution {
public:
int maxArea(vector<int>& height) {
int length = height.size();
vector<point*> points;
for(int i = 0; i != length; i ++){
point* thisPoint = new point(i, height[i]);
points.push_back(thisPoint);
}
quicksort(points, 0, points.size() - 1);
int maxArea = 0;
int area = 0;
int width = 0;
int min[50000];
int max[50000];
memset(min, 0, sizeof(min));
memset(max, 0, sizeof(max));
int minInt = points[length - 1] -> x;
int maxInt = points[length - 1] -> x;
for(int i = length - 1; i != -1; i --){
if(minInt > points[i] -> x){
minInt = points[i] -> x;
} else if(maxInt < points[i] -> y){
maxInt = points[i] -> x;
}
min[i] = minInt;
max[i] = maxInt;
}
for(int i = 0; i != length - 1; i ++){
width = abs(min[i + 1] - points[i] -> x) > abs(max[i + 1] - points[i] -> x) ? abs(min[i + 1] - points[i] -> x):abs(max[i + 1] - points[i] -> x);
area = points[i]->y * width;
maxArea = maxArea < area ? area : maxArea;
}
return maxArea;
}
void quicksort(vector<point*> arr, int p, int r){
if(p < r){
int q = partition(arr, p, r);
quicksort(arr, p, q - 1);
quicksort(arr, q + 1, r);
}
}
int partition(vector<point*> arr, int p, int r){
// int pivot = arr[r]->y;
int center = (p + r)/2;
int pivot = 0;
if(arr[center ] < arr[p]){
swap(*arr[center], *arr[p]);
}
if(arr[r] < arr[p]){
swap(*arr[p], *arr[r]);
}
if(arr[center] > arr[r]){
swap(*arr[center], *arr[r]);
}
swap(*arr[center], *arr[r - 1]);
pivot = arr[r - 1] -> y;
int i = p - 1;
for(int j = p; j != r - 1; j ++){
if(arr[j] -> y <= pivot){
i = i + 1;
swap(*arr[i], *arr[j]);
}
}
i ++ ;
swap(*arr[i], *arr[r - 1]);
return i;
}
void swap(point &a, point &b){
point temp = b;
b = a;
a = temp;
}
};
int main(int argc, char const *argv[]) {
Solution test;
vector<int> lines;
lines.push_back(1);
lines.push_back(1);
lines.push_back(2);
lines.push_back(3);
lines.push_back(5);
lines.push_back(4);
int result = test.maxArea(lines);
cout << result << endl;
return 0;
}