|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -/** |
4 |
| - * 755. Pour Water |
5 |
| - * |
6 |
| - * We are given an elevation map, heights[i] representing the height of the terrain at that index. |
7 |
| - * The width at each index is 1. After V units of water fall at index K, how much water is at each index? |
8 |
| - * Water first drops at index K and rests on top of the highest terrain or water at that index. |
9 |
| - * Then, it flows 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 |
| -
|
15 |
| - 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. |
16 |
| - 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. |
17 |
| -
|
18 |
| - Example 1: |
19 |
| - Input: heights = [2,1,1,2,1,2,2], V = 4, K = 3 |
20 |
| - Output: [2,2,2,3,2,2,2] |
21 |
| - Explanation: |
22 |
| - # # |
23 |
| - # # |
24 |
| - ## # ### |
25 |
| - ######### |
26 |
| - 0123456 <- index |
27 |
| -
|
28 |
| - The first drop of water lands at index K = 3: |
29 |
| -
|
30 |
| - # # |
31 |
| - # w # |
32 |
| - ## # ### |
33 |
| - ######### |
34 |
| - 0123456 |
35 |
| -
|
36 |
| - When moving left or right, the water can only move to the same level or a lower level. |
37 |
| - (By level, we mean the total height of the terrain plus any water in that column.) |
38 |
| - Since moving left will eventually make it fall, it moves left. |
39 |
| - (A droplet "made to fall" means go to a lower height than it was at previously.) |
40 |
| -
|
41 |
| - # # |
42 |
| - # # |
43 |
| - ## w# ### |
44 |
| - ######### |
45 |
| - 0123456 |
46 |
| -
|
47 |
| - Since moving left will not make it fall, it stays in place. The next droplet falls: |
48 |
| -
|
49 |
| - # # |
50 |
| - # w # |
51 |
| - ## w# ### |
52 |
| - ######### |
53 |
| - 0123456 |
54 |
| -
|
55 |
| - Since the new droplet moving left will eventually make it fall, it moves left. |
56 |
| - Notice that the droplet still preferred to move left, |
57 |
| - even though it could move right (and moving right makes it fall quicker.) |
58 |
| -
|
59 |
| - # # |
60 |
| - # w # |
61 |
| - ## w# ### |
62 |
| - ######### |
63 |
| - 0123456 |
64 |
| -
|
65 |
| - # # |
66 |
| - # # |
67 |
| - ##ww# ### |
68 |
| - ######### |
69 |
| - 0123456 |
70 |
| -
|
71 |
| - After those steps, the third droplet falls. |
72 |
| - Since moving left would not eventually make it fall, it tries to move right. |
73 |
| - Since moving right would eventually make it fall, it moves right. |
74 |
| -
|
75 |
| - # # |
76 |
| - # w # |
77 |
| - ##ww# ### |
78 |
| - ######### |
79 |
| - 0123456 |
80 |
| -
|
81 |
| - # # |
82 |
| - # # |
83 |
| - ##ww#w### |
84 |
| - ######### |
85 |
| - 0123456 |
86 |
| -
|
87 |
| - Finally, the fourth droplet falls. |
88 |
| - Since moving left would not eventually make it fall, it tries to move right. |
89 |
| - Since moving right would not eventually make it fall, it stays in place: |
90 |
| -
|
91 |
| - # # |
92 |
| - # w # |
93 |
| - ##ww#w### |
94 |
| - ######### |
95 |
| - 0123456 |
96 |
| -
|
97 |
| - The final answer is [2,2,2,3,2,2,2]: |
98 |
| -
|
99 |
| - # |
100 |
| - ####### |
101 |
| - ####### |
102 |
| - 0123456 |
103 |
| -
|
104 |
| -
|
105 |
| - Example 2: |
106 |
| - Input: heights = [1,2,3,4], V = 2, K = 2 |
107 |
| - Output: [2,3,3,4] |
108 |
| - Explanation: |
109 |
| - The last droplet settles at index 1, since moving further left would not cause it to eventually fall to a lower height. |
110 |
| -
|
111 |
| -
|
112 |
| - Example 3: |
113 |
| - Input: heights = [3,1,3], V = 5, K = 1 |
114 |
| - Output: [4,4,4] |
115 |
| - Note: |
116 |
| -
|
117 |
| - heights will have length in [1, 100] and contain integers in [0, 99]. |
118 |
| - V will be in range [0, 2000]. |
119 |
| - K will be in range [0, heights.length - 1]. |
120 |
| - */ |
121 |
| - |
122 | 3 | public class _755 {
|
123 |
| - public static class Solution1 { |
124 |
| - public int[] pourWater(int[] heights, int V, int K) { |
125 |
| - int index; |
126 |
| - while (V > 0) { |
127 |
| - index = K; |
128 |
| - for (int i = K - 1; i >= 0; i--) { |
129 |
| - if (heights[i] > heights[index]) { |
130 |
| - break; |
131 |
| - } else if (heights[i] < heights[index]) { |
132 |
| - index = i; |
133 |
| - } |
134 |
| - } |
135 |
| - if (index != K) { |
136 |
| - heights[index]++; |
137 |
| - V--; |
138 |
| - continue; |
139 |
| - } |
140 |
| - |
141 |
| - for (int i = K + 1; i < heights.length; i++) { |
142 |
| - if (heights[i] > heights[index]) { |
143 |
| - break; |
144 |
| - } else if (heights[i] < heights[index]) { |
145 |
| - index = i; |
146 |
| - } |
| 4 | + public static class Solution1 { |
| 5 | + public int[] pourWater(int[] heights, int V, int K) { |
| 6 | + int index; |
| 7 | + while (V > 0) { |
| 8 | + index = K; |
| 9 | + for (int i = K - 1; i >= 0; i--) { |
| 10 | + if (heights[i] > heights[index]) { |
| 11 | + break; |
| 12 | + } else if (heights[i] < heights[index]) { |
| 13 | + index = i; |
| 14 | + } |
| 15 | + } |
| 16 | + if (index != K) { |
| 17 | + heights[index]++; |
| 18 | + V--; |
| 19 | + continue; |
| 20 | + } |
| 21 | + |
| 22 | + for (int i = K + 1; i < heights.length; i++) { |
| 23 | + if (heights[i] > heights[index]) { |
| 24 | + break; |
| 25 | + } else if (heights[i] < heights[index]) { |
| 26 | + index = i; |
| 27 | + } |
| 28 | + } |
| 29 | + heights[index]++; |
| 30 | + V--; |
| 31 | + } |
| 32 | + return heights; |
147 | 33 | }
|
148 |
| - heights[index]++; |
149 |
| - V--; |
150 |
| - } |
151 |
| - return heights; |
152 | 34 | }
|
153 |
| - } |
154 | 35 | }
|
0 commit comments