Shohei Yoshida
Shohei Yoshida's Blog

Shohei Yoshida's Blog

LeetCode 1706. Where Will the Ball Fall in F#

Shohei Yoshida's photo
Shohei Yoshida
·Nov 1, 2022·

1 min read

URL

leetcode.com/problems/where-will-the-ball-f..

Code

github.com/syohex/dotnet-study/blob/master/..

let findBall (grid: int [,]) : int list =
    let rows = Array2D.length1 grid
    let cols = Array2D.length2 grid

    let rec findBall' row col (grid: int [,]) =
        if row >= rows then
            col
        else if grid.[row, col] = 1 then
            if col = cols - 1 || grid.[row, col + 1] = -1 then
                -1
            else
                findBall' (row + 1) (col + 1) grid
        else if col = 0 || grid.[row, col - 1] = 1 then
            -1
        else
            findBall' (row + 1) (col - 1) grid

    seq { 0 .. (cols - 1) }
    |> Seq.map (fun col -> findBall' 0 col grid)
    |> Seq.toList
 
Share this