classSolution { public: /** * @param heights: the height of the terrain * @param V: the units of water * @param K: the index * @return: how much water is at each index */
intfind_min_left(vector<int> heights, int K){ int index = -1, min = heights[K]; for (int i = K-1; i >=0; i--) {
if (heights[i] < min) { index = i; min = heights[i]; }
if (heights[i] > min) break; } return index; }
intfind_min_right(vector<int> heights, int K){ int index = -1, min = heights[K]; for (int i = K+1; i <heights.size(); i++) {
if (heights[i] < min) { index = i; min = heights[i]; }
if (heights[i] > min) break; } return index;
}
vector<int> pourWater(vector<int> &heights, int V, int K) { // Write your code here bool flag_left = true, flag_self = false; for (int i = 0; i < V; i++) {
int index = find_min_left(heights, K); if (index != -1) { heights[index]++; continue; }
index = find_min_right(heights, K); if (index != -1) { heights[index]++; continue; }
heights[K]++; }
return heights; } };
-------------end of file
thanks for reading-------------