LintCode——Pour Water

Pour Water:

We are given an elevation map, heights[i] representing the height of the terrain at that index. The width at each index is 1. After V units of water fall at index K, how much water is at each index?

Water first drops at index K and rests on top of the highest terrain or water at that index. Then, it flows according to the following rules:

  • If the droplet would eventually fall by moving left, then move left.
  • Otherwise, if the droplet would eventually fall by moving right, then move right.
  • Otherwise, rise at it's current position.

Here, "eventually fall" means that the droplet will eventually be at a lower level if it moves in that direction. Also, "level" means the height of the terrain plus any water in that column.
We can assume there's infinitely high terrain on the two sides out of bounds of the array. Also, there could not be partial water being spread out evenly on more than 1 grid block - each unit of water has to be in exactly one block.

注意事项:

1.heights will have length in [1, 100] and contain integers in [0, 99].
2.V will be in range [0, 2000].
3.K will be in range [0, heights.length - 1].

样例:

 1 Example_1:
 2 Given: heights = [2,1,1,2,1,2,2], V = 4, K = 3
 3 Return: [2,2,2,3,2,2,2]
 4 
 5 Example_2:
 6 Given: heights = [1,2,3,4], V = 2, K = 2
 7 Return: [2,3,3,4]
 8 
 9 Example_3:
10 Given: heights = [3,1,3], V = 5, K = 1
11 Return: [4,4,4]

题目大意:给定一个组高度值,代表一个水槽的底部高度分布情况。在K点处倒入V体积的水,求倒水之后的高度分布。

 1 class Solution {
 2     public int findLeftMinIdx(int[] heights, int K) {
 3         int minIdx = K, minHeight = heights[K];
 4         for (int i = K - 1; i >= 0; i--) {
 5             if (heights[i] < minHeight) {
 6                 minIdx = i;
 7                 minHeight = heights[i];
 8             } 
 9             else if (heights[i] > minHeight) {
10                 break;
11             }
12         }
13         return minIdx;
14     }
15     
16     public int findRightMinIdx(int[] heights, int K) {
17         int minIdx = K, minHeight = heights[K];
18         for (int i = K + 1; i < heights.length; i++) {
19             if (heights[i] < minHeight) {
20                 minIdx = i;
21                 minHeight = heights[i];
22             } 
23             else if (heights[i] > minHeight) {
24                 break;
25             }
26         }
27         return minIdx;
28     }
29 
30     public int[] pourWater(int[] heights, int V, int K) {
31         for (int i = 0; i < V; i++) {
32             int leftMinIdx = findLeftMinIdx(heights, K);
33             if (leftMinIdx < K) {
34                 heights[leftMinIdx]++;
35             } 
36             else {
37                 int rightMinIdx = findRightMinIdx(heights, K);
38                 if (rightMinIdx > K) {
39                     heights[rightMinIdx]++;
40                 } 
41                 else {
42                     heights[K]++;
43                 }
44             }
45         }
46         return heights;
47     }
48 }

猜你喜欢

转载自www.cnblogs.com/wangcj2015/p/9049514.html
今日推荐