-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11.cpp
More file actions
46 lines (42 loc) · 1.11 KB
/
11.cpp
File metadata and controls
46 lines (42 loc) · 1.11 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
//
// 11.cpp
// LeetCode
//
// Created by 张佐玮 on 15/6/1.
// Copyright (c) 2015年 JarvisZhang. All rights reserved.
//
//
//
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int maxArea(vector<int>& height) {
int front = 0, rear = (int) height.size() - 1, maxVol = 0, currentHeight = 0;
while (front < rear) {
if (height[front] <= currentHeight) {
front++;
continue;
}
if (height[rear] <= currentHeight) {
rear--;
continue;
}
maxVol = max(min(height[rear],height[front]) * abs(rear - front), maxVol);
if (height[front] < height[rear]) {
currentHeight = height[front];
front++;
}
else if (height[front] > height[rear]) {
currentHeight = height[rear];
rear--;
}
else {
currentHeight = height[front];
front++;rear--;
}
}
return maxVol;
}
};