You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LeetCode Grid Game暴力解法遇测试用例失败求解析(C#)

Grid Game暴力解法测试用例失败求助

我没了解前缀和的情况下,用暴力法解决LeetCode的Grid Game问题,原以为解法虽非最优但能完成任务,却在第9个测试用例上失败,搞不懂原因。该测试用例的预期输出为63,我无法理解这个数值的来源,希望得到帮助。

测试用例

using solutions;

int[][] testCase = new int [2][];

testCase[0] = new int[] {20,3,20,17,2,12,15,17,4,15};
testCase[1] = new int[] {20,10,13,14,15,5,2,3,14,3};

SolutionV2 SLN = new SolutionV2();
Console.WriteLine(SLN.GridGame(testCase));

我的C#解法

namespace solutions;
public class SolutionV2 {
    public long GridGame(int[][] grid) 
    {
        long returnValue = 0;
        long maxValue = 0;
        int[] currentPosition = new int[] {0,0};
        int maxCol = grid[0].GetLength(0);
        Console.WriteLine("[{0}]", string.Join(", ", grid[0]));
        Console.WriteLine("[{0}]", string.Join(", ", grid[1]));

        for(int k=0;k<maxCol;k++) //find the highest path sum for the first robot
        {
            int sum = 0;
            for(int i=0;i<maxCol;i++) 
            {
                if(i<k)
                { 
                    sum += grid[0][i]; //sum up the first row until it is time to move down
                }
                else if(i==k)
                {
                    sum += grid[0][i];
                    for(int j=i;j<maxCol;j++)
                    {
                        sum += grid[1][j];
                    }
                    if(sum>maxValue)
                    {
                        maxValue = sum;
                    }
                    break;
                }
            }
        }

        for(int k=0;k<maxCol;k++) //there has to be a better way to set the sum path to zero
        {
            int sum = 0;
            for(int i=0;i<maxCol;i++) 
            {
                if(i<k)
                { 
                    sum += grid[0][i]; //sum up the first row until it is time to move down
                }
                else if(i==k)
                {
                    sum += grid[0][i];
                    for(int j=i;j<maxCol;j++)
                    {
                        sum += grid[1][j];
                    }
                    if(sum==maxValue) //found it, now set the path to zero
                    {
                        for(int a=0;a<=i;a++)
                        {
                            grid[0][a] = 0;
                        }
                        for(int a=i;a<maxCol;a++)
                        {
                            grid[1][a] = 0;
                        }
                    }
                    break;
                }
            }
        }
        Console.WriteLine("[{0}]", string.Join(", ", grid[0]));
        Console.WriteLine("[{0}]", string.Join(", ", grid[1]));

        for(int k=0;k<maxCol;k++) //find the highest path sum for the second robot
        {
            int sum = 0;
            for(int i=0;i<maxCol;i++) 
            {
                if(i<k)
                { 
                    sum += grid[0][i]; //sum up the first row until it is time to move down
                }
                else if(i==k)
                {
                    sum += grid[0][i];
                    for(int j=i;j<maxCol;j++)
                    {
                        sum += grid[1][j];
                    }
                    if(sum>returnValue)
                    {
                        returnValue = sum;
                    }
                    break;
                }
            }
        }
        
        return returnValue;
    }
}

内容的提问来源于stack exchange,提问作者SenLong Yu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 03:50:26