KatInterview

756. Pour Water

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to simulate pouring a given volume of water onto a terrain represented by an array of heights. The water is dropped one unit at a time at a specific index k. Each water droplet follows a set of rules to find its final resting place. The key challenge is to correctly interpret and implement these rules, especially the concept of "eventually falling" and the priority of movement (left, then right, then stay).

Solution

Pour Water in Python

class Solution:
    def pourWater(self, heights: list[int], volume: int, k: int) -> list[int]:
        n = len(heights)
        # Simulate dropping 'volume' units of water one by one.
        for _ in range(volume):
            # --- Try to move left ---
            best_left = -1
            # Scan from k-1 down to 0 to find the best place for water to settle.
            for j in range(k - 1, -1, -1):
                # If we encounter a wall, water cannot flow further left.
                if heights[j] > heights[j + 1]:
                    break
                # If we find a dip, it's a potential place for the water.
                # We keep searching left for a better spot (further away).
                if heights[j] < heights[j + 1]:
                    best_left = j
            
            # If a valid position to the left was found, place the water there.
            if best_left != -1:
                heights[best_left] += 1
                continue

            # --- Try to move right ---
            best_right = -1
            # If no suitable spot was found to the left, scan right from k+1 to n-1.
            for j in range(k + 1, n):
                # If we encounter a wall, water cannot flow further right.
                if heights[j] > heights[j - 1]:
                    break
                # If we find a dip, it's a potential place.
                # Keep searching for a better spot further to the right.
                if heights[j] < heights[j - 1]:
                    best_right = j
            
            # If a valid position to the right was found, place the water there.
            if best_right != -1:
                heights[best_right] += 1
                continue

            # --- Stay at k ---
            # If water can't flow left or right, it stays at k.
            heights[k] += 1
            
        return heights

Pour Water in Java

class Solution {
    public int[] pourWater(int[] heights, int volume, int k) {
        int n = heights.length;
        // Simulate dropping 'volume' units of water one by one.
        for (int i = 0; i < volume; i++) {
            // --- Try to move left ---
            int bestLeft = -1;
            // Scan from k-1 down to 0 to find the best place for water to settle.
            for (int j = k - 1; j >= 0; j--) {
                // If we encounter a wall, water cannot flow further left.
                if (heights[j] > heights[j + 1]) {
                    break;
                }
                // If we find a dip, it's a potential place for the water.
                // We keep searching left for a better spot (further away).
                if (heights[j] < heights[j + 1]) {
                    bestLeft = j;
                }
            }

            // If a valid position to the left was found, place the water there.
            if (bestLeft != -1) {
                heights[bestLeft]++;
                continue;
            }

            // --- Try to move right ---
            int bestRight = -1;
            // If no suitable spot was found to the left, scan right from k+1 to n-1.
            for (int j = k + 1; j < n; j++) {
                // If we encounter a wall, water cannot flow further right.
                if (heights[j] > heights[j - 1]) {
                    break;
                }
                // If we find a dip, it's a potential place.
                // Keep searching for a better spot further to the right.
                if (heights[j] < heights[j - 1]) {
                    bestRight = j;
                }
            }
            
            // If a valid position to the right was found, place the water there.
            if (bestRight != -1) {
                heights[bestRight]++;
                continue;
            }

            // --- Stay at k ---
            // If water can't flow left or right, it stays at k.
            heights[k]++;
        }
        return heights;
    }
}

Pour Water in C++

class Solution {
public:
    vector<int> pourWater(vector<int>& heights, int volume, int k) {
        int n = heights.size();
        // Simulate dropping 'volume' units of water one by one.
        for (int i = 0; i < volume; ++i) {
            // --- Try to move left ---
            int best_left = -1;
            // Scan from k-1 down to 0 to find the best place for water to settle.
            for (int j = k - 1; j >= 0; --j) {
                // If we encounter a wall, water cannot flow further left.
                if (heights[j] > heights[j + 1]) {
                    break;
                }
                // If we find a dip, it's a potential place for the water.
                // We keep searching left for an even better spot (further away).
                if (heights[j] < heights[j + 1]) {
                    best_left = j;
                }
            }

            // If a valid position to the left was found, place the water there.
            if (best_left != -1) {
                heights[best_left]++;
                continue;
            }

            // --- Try to move right ---
            int best_right = -1;
            // If no suitable spot was found to the left, scan right from k+1 to n-1.
            for (int j = k + 1; j < n; ++j) {
                // If we encounter a wall, water cannot flow further right.
                if (heights[j] > heights[j - 1]) {
                    break;
                }
                // If we find a dip, it's a potential place.
                // Keep searching for a better spot further to the right.
                if (heights[j] < heights[j - 1]) {
                    best_right = j;
                }
            }
            
            // If a valid position to the right was found, place the water there.
            if (best_right != -1) {
                heights[best_right]++;
                continue;
            }

            // --- Stay at k ---
            // If water can't flow left or right, it stays at k.
            heights[k]++;
        }
        return heights;
    }
};

Pour Water in TypeScript

function pourWater(heights: number[], volume: number, k: number): number[] {
    const n = heights.length;
    // Simulate dropping 'volume' units of water one by one.
    for (let i = 0; i < volume; i++) {
        // --- Try to move left ---
        let bestLeft = -1;
        // Scan from k-1 down to 0 to find the best place for water to settle.
        for (let j = k - 1; j >= 0; j--) {
            // If we encounter a wall, water cannot flow further left.
            if (heights[j] > heights[j + 1]) {
                break;
            }
            // If we find a dip, it's a potential place for the water.
            // We keep searching left for a better spot (further away).
            if (heights[j] < heights[j + 1]) {
                bestLeft = j;
            }
        }

        // If a valid position to the left was found, place the water there.
        if (bestLeft !== -1) {
            heights[bestLeft]++;
            continue;
        }

        // --- Try to move right ---
        let bestRight = -1;
        // If no suitable spot was found to the left, scan right from k+1 to n-1.
        for (let j = k + 1; j < n; j++) {
            // If we encounter a wall, water cannot flow further right.
            if (heights[j] > heights[j - 1]) {
                break;
            }
            // If we find a dip, it's a potential place.
            // Keep searching for a better spot further to the right.
            if (heights[j] < heights[j - 1]) {
                bestRight = j;
            }
        }
        
        // If a valid position to the right was found, place the water there.
        if (bestRight !== -1) {
            heights[bestRight]++;
            continue;
        }

        // --- Stay at k ---
        // If water can't flow left or right, it stays at k.
        heights[k]++;
    }
    return heights;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.