|
| 1 | +package array; |
| 2 | + |
| 3 | +/** |
| 4 | + * Created by gouthamvidyapradhan on 03/02/2018. |
| 5 | + * We are given an elevation map, heights[i] representing the height of the terrain at that index. The width at each |
| 6 | + * index is 1. After V units of water fall at index K, how much water is at each index? |
| 7 | +
|
| 8 | + Water first drops at index K and rests on top of the highest terrain or water at that index. Then, it flows |
| 9 | + according to the following rules: |
| 10 | +
|
| 11 | + If the droplet would eventually fall by moving left, then move left. |
| 12 | + Otherwise, if the droplet would eventually fall by moving right, then move right. |
| 13 | + Otherwise, rise at it's current position. |
| 14 | + Here, "eventually fall" means that the droplet will eventually be at a lower level if it moves in that direction. |
| 15 | + Also, "level" means the height of the terrain plus any water in that column. |
| 16 | + We can assume there's infinitely high terrain on the two sides out of bounds of the array. Also, there could not be |
| 17 | + partial water being spread out evenly on more than 1 grid block - each unit of water has to be in exactly one block. |
| 18 | +
|
| 19 | + Example 1: |
| 20 | + Input: heights = [2,1,1,2,1,2,2], V = 4, K = 3 |
| 21 | + Output: [2,2,2,3,2,2,2] |
| 22 | + Explanation: |
| 23 | + # # |
| 24 | + # # |
| 25 | + ## # ### |
| 26 | + ######### |
| 27 | + 0123456 <- index |
| 28 | +
|
| 29 | + The first drop of water lands at index K = 3: |
| 30 | +
|
| 31 | + # # |
| 32 | + # w # |
| 33 | + ## # ### |
| 34 | + ######### |
| 35 | + 0123456 |
| 36 | +
|
| 37 | + When moving left or right, the water can only move to the same level or a lower level. |
| 38 | + (By level, we mean the total height of the terrain plus any water in that column.) |
| 39 | + Since moving left will eventually make it fall, it moves left. |
| 40 | + (A droplet "made to fall" means go to a lower height than it was at previously.) |
| 41 | +
|
| 42 | + # # |
| 43 | + # # |
| 44 | + ## w# ### |
| 45 | + ######### |
| 46 | + 0123456 |
| 47 | +
|
| 48 | + Since moving left will not make it fall, it stays in place. The next droplet falls: |
| 49 | +
|
| 50 | + # # |
| 51 | + # w # |
| 52 | + ## w# ### |
| 53 | + ######### |
| 54 | + 0123456 |
| 55 | +
|
| 56 | + Since the new droplet moving left will eventually make it fall, it moves left. |
| 57 | + Notice that the droplet still preferred to move left, |
| 58 | + even though it could move right (and moving right makes it fall quicker.) |
| 59 | +
|
| 60 | + # # |
| 61 | + # w # |
| 62 | + ## w# ### |
| 63 | + ######### |
| 64 | + 0123456 |
| 65 | +
|
| 66 | + # # |
| 67 | + # # |
| 68 | + ##ww# ### |
| 69 | + ######### |
| 70 | + 0123456 |
| 71 | +
|
| 72 | + After those steps, the third droplet falls. |
| 73 | + Since moving left would not eventually make it fall, it tries to move right. |
| 74 | + Since moving right would eventually make it fall, it moves right. |
| 75 | +
|
| 76 | + # # |
| 77 | + # w # |
| 78 | + ##ww# ### |
| 79 | + ######### |
| 80 | + 0123456 |
| 81 | +
|
| 82 | + # # |
| 83 | + # # |
| 84 | + ##ww#w### |
| 85 | + ######### |
| 86 | + 0123456 |
| 87 | +
|
| 88 | + Finally, the fourth droplet falls. |
| 89 | + Since moving left would not eventually make it fall, it tries to move right. |
| 90 | + Since moving right would not eventually make it fall, it stays in place: |
| 91 | +
|
| 92 | + # # |
| 93 | + # w # |
| 94 | + ##ww#w### |
| 95 | + ######### |
| 96 | + 0123456 |
| 97 | +
|
| 98 | + The final answer is [2,2,2,3,2,2,2]: |
| 99 | +
|
| 100 | + # |
| 101 | + ####### |
| 102 | + ####### |
| 103 | + 0123456 |
| 104 | + Example 2: |
| 105 | + Input: heights = [1,2,3,4], V = 2, K = 2 |
| 106 | + Output: [2,3,3,4] |
| 107 | + Explanation: |
| 108 | + The last droplet settles at index 1, since moving further left would not cause it to eventually fall to a lower |
| 109 | + height. |
| 110 | + Example 3: |
| 111 | + Input: heights = [3,1,3], V = 5, K = 1 |
| 112 | + Output: [4,4,4] |
| 113 | + Note: |
| 114 | +
|
| 115 | + heights will have length in [1, 100] and contain integers in [0, 99]. |
| 116 | + V will be in range [0, 2000]. |
| 117 | + K will be in range [0, heights.length - 1]. |
| 118 | +
|
| 119 | + Solution: |
| 120 | + Check first left and then right to see if there are any lower levels, if yes then drop the water at this point. Else |
| 121 | + maintain the drop at the start position |
| 122 | + */ |
| 123 | +public class PourWater { |
| 124 | + |
| 125 | + /** |
| 126 | + * Main method |
| 127 | + * @param args |
| 128 | + * @throws Exception |
| 129 | + */ |
| 130 | + public static void main(String[] args) throws Exception{ |
| 131 | + int[] A = {2, 1, 1, 2, 1, 2, 2}; |
| 132 | + int[] result = new PourWater().pourWater(A, 4, 3); |
| 133 | + for(int i : result){ |
| 134 | + System.out.print(i + " "); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + public int[] pourWater(int[] heights, int V, int K) { |
| 139 | + while(V-- > 0){ |
| 140 | + heights[K] += 1; |
| 141 | + int index = K; |
| 142 | + int min = heights[K]; |
| 143 | + for(int i = K - 1; i >= 0; i--){ |
| 144 | + if(heights[i] + 1 > min){ |
| 145 | + break; |
| 146 | + } else if(heights[i] + 1 < min){ |
| 147 | + min = heights[i] + 1; |
| 148 | + index = i; |
| 149 | + } |
| 150 | + } |
| 151 | + if(index == K){ |
| 152 | + for(int i = K + 1; i < heights.length; i++){ |
| 153 | + if(heights[i] + 1 > min){ |
| 154 | + break; |
| 155 | + } else if(heights[i] + 1 < min){ |
| 156 | + min = heights[i] + 1; |
| 157 | + index = i; |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + if(index != K){ |
| 162 | + heights[K]--; |
| 163 | + heights[index]++; |
| 164 | + } |
| 165 | + } |
| 166 | + return heights; |
| 167 | + } |
| 168 | + |
| 169 | +} |
0 commit comments