Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    Practice problems / Water flowing to both oceans

    Water flowing to both oceans

    Mediumgraphdfsbfsmatrix

    heights is an elevation map. One ocean touches the top and left edges, the other the bottom and right edges. Water flows to a neighbouring cell (up/down/left/right) whose height is lower or equal. Return every [row, col] whose water can reach both oceans, sorted by row then column.

    Example 1

    Input:  heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]
    Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]

    Example 2

    Input:  heights = [[1]]
    Output: [[0,0]]

    Constraints

    • 1 ≤ rows, cols ≤ 100
    • 0 ≤ heights[r][c] ≤ 10⁵

    Submit checks your solution against 7 tests, including hidden ones.

    /**
     * @param {int[][]} heights
     * @return {int[][]}
     */
    function pacificAtlantic(heights) {
      
    }
    
    Sign in to run
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.