-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path11.cpp
More file actions
97 lines (80 loc) · 2.25 KB
/
11.cpp
File metadata and controls
97 lines (80 loc) · 2.25 KB
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
// Author : Accagain
// Date : 17/2/14
// Email : chenmaosen0@gmail.com
/***************************************************************************************
*
* Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).
*
* n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0).
*
* Find two lines, which together with x-axis forms a container, such that the container contains the most water.
*
* Note: You may not slant the container and n is at least 2.
*
* O(nlog(n))
****************************************************************************************/
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
struct Info
{
int height, pos;
//Info(int x, int y): height(x), pos(y) {}
};
static bool cmp(Info a, Info b)
{
if(a.height != b.height)
return a.height > b.height;
return a.pos < b.pos;
}
int maxArea(vector<int>& height) {
Info save[height.size()];
for(int i=0; i<height.size(); i++)
{
save[i].height = height[i];
save[i].pos = i;
}
sort(save, save+height.size(), cmp);
//printf("%d\n", save[0].height);
int ans = 0;
int Min = save[0].pos;
int Max = save[0].pos;
for(int i=1; i<height.size(); i++)
{
int temp = max(abs(save[i].pos - Min), abs(save[i].pos - Max)) * save[i].height;
ans = max(ans, temp);
Min = min(Min, save[i].pos);
Max = max(Max, save[i].pos);
}
/*
* O(n^2) TLE
int ans = 0;
for(int i=0; i<height.size(); i++)
{
int left = height[i];
for(int j=i+1; j<height.size(); j++)
{
int right = height[j];
ans = max(ans, (j - i) * min(left, right));
}
}
*/
return ans;
}
};
int main() {
Solution * test = new Solution();
vector<int> data;
data.push_back(1);
data.push_back(2);
data.push_back(1);
printf("%d\n", test->maxArea(data));
return 0;
}
//
// Created by cms on 17/2/14.
//