-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path755.cpp
More file actions
42 lines (42 loc) · 1.27 KB
/
755.cpp
File metadata and controls
42 lines (42 loc) · 1.27 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
class Solution {
public:
vector<int> pourWater(vector<int>& heights, int volume, int k) {
int n = heights.size();
while (volume) {
int left = k;
int leftIdx = k;
int leftValue = heights[k];
int right = k;
int rightIdx = k;
int rightValue = heights[k];
bool flag = false;
while (left - 1 >= 0 && heights[left - 1] <= heights[left]) {
if (heights[left - 1] < leftValue) {
leftValue = heights[left - 1];
leftIdx = left - 1;
}
left--;
}
if (leftIdx != k) {
heights[leftIdx]++;
flag = true;
}
while (!flag && right + 1 < n && heights[right + 1] <= heights[right]) {
if (heights[right + 1] < rightValue) {
rightValue = heights[right + 1];
rightIdx = right + 1;
}
right++;
}
if (rightIdx != k) {
heights[rightIdx]++;
flag = true;
}
if (!flag) {
heights[k]++;
}
volume--;
}
return heights;
}
};