LeetCode Solutions

755. Pour Water

Time: $O(VK)$

Space: $O(1)$

			

class Solution {
 public:
  vector<int> pourWater(vector<int>& heights, int V, int K) {
    const int n = heights.size();

    int i = K;
    while (V--) {
      while (i > 0 && heights[i] >= heights[i - 1])
        --i;
      while (i + 1 < n && heights[i] >= heights[i + 1])
        ++i;
      while (i > K && heights[i] == heights[i - 1])
        --i;
      ++heights[i];
    }

    return heights;
  }
};
			

class Solution {
  public int[] pourWater(int[] heights, int V, int K) {
    final int n = heights.length;

    int i = K;
    while (V-- > 0) {
      while (i > 0 && heights[i] >= heights[i - 1])
        --i;
      while (i + 1 < n && heights[i] >= heights[i + 1])
        ++i;
      while (i > K && heights[i] == heights[i - 1])
        --i;
      ++heights[i];
    }

    return heights;
  }
}