forked from lee2018jian/airbnb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaterDrop
31 lines (31 loc) · 939 Bytes
/
WaterDrop
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
public int[] pourWater(int[] heights, int V, int K) {
if (heights == null || heights.length == 0 || V == 0) {
return heights;
}
int index;
while (V > 0) {
index = K;
for (int i = K - 1; i >= 0; i--) {
if (heights[i] > heights[index]) {
break;
} else if (heights[i] < heights[index]) {
index = i;
}
}
if (index != K) {
heights[index]++;
V--;
continue;
}
for (int i = K + 1; i < heights.length; i++) {
if (heights[i] > heights[index]) {
break;
} else if (heights[i] < heights[index]) {
index = i;
}
}
heights[index]++;
V--;
}
return heights;
}