© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago
morry329#

A small hiccup for the LeetCode puzzle Where the ball will fall

Link to the original puzzle https://leetcode.com/problems/where-will-the-ball-fall/

My code does not print out the right output just yet
public class ZigZagBallFallsTry
{

    public int[] FindBall(int[][] grid)
    {
        int col = grid[0].Length-1;
        int row = grid.Length-1;
        int[] result = new int[col];

        for (int i = 0; i < col; i++)
        {
            result[i] = BallHelp(grid, row, col);
        }

        return result;

    }

    private int BallHelp(int[][] grid, int row, int col)
    {
        int currRow = 0;
        int currCol = col;

        while (currRow <= row)
        {
            if (grid[currRow][currCol] == 1)
            {
                if (currCol == col || grid[currRow][currCol + 1] == -1)
                {
                    return -1;
                }
                else
                {
                    currCol++;
                    currRow++;
                } 
            } 
            else if (grid[currRow][currCol] == -1)
            {
                if (currCol == 0 || grid[currRow][currCol - 1] == 1)
                {
                    return -1;
                }
                else
                {
                    currCol--;
                    currRow++;
                }
            } 
            else 
            {
                currRow++;
            }
        }

        return currCol;
    }

}
public class ZigZagBallFallsTry
{

    public int[] FindBall(int[][] grid)
    {
        int col = grid[0].Length-1;
        int row = grid.Length-1;
        int[] result = new int[col];

        for (int i = 0; i < col; i++)
        {
            result[i] = BallHelp(grid, row, col);
        }

        return result;

    }

    private int BallHelp(int[][] grid, int row, int col)
    {
        int currRow = 0;
        int currCol = col;

        while (currRow <= row)
        {
            if (grid[currRow][currCol] == 1)
            {
                if (currCol == col || grid[currRow][currCol + 1] == -1)
                {
                    return -1;
                }
                else
                {
                    currCol++;
                    currRow++;
                } 
            } 
            else if (grid[currRow][currCol] == -1)
            {
                if (currCol == 0 || grid[currRow][currCol - 1] == 1)
                {
                    return -1;
                }
                else
                {
                    currCol--;
                    currRow++;
                }
            } 
            else 
            {
                currRow++;
            }
        }

        return currCol;
    }

}


The output is 


The output is 
-1, -1, -1, -1
 whilst it is supposed to be 
 whilst it is supposed to be 
1, -1, -1, -1, -1.
The input here  is 
The input here  is 
grid = [[1,1,1,-1,-1],[1,1,1,-1,-1],[-1,-1,-1,1,1],[1,1,1,1,-1],[-1,-1,-1,-1,-1]]`
Could anyone kindly point me in the right direction?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

IndexOutOfBoundsException triggered | LeetCode Where will the ball fall
C#CC# / help
2y ago
❔ ✅ comprehension questions for a LeetCode puzzle
C#CC# / help
3y ago
❔ N puzzle game Im getting a random color everytime I slide the puzzle
C#CC# / help
3y ago
❔ LeetCode Alternatives
C#CC# / help
3y ago