Java 最大路径和

Java 最大路径和,java,Java,我正试图开发一个Java程序来解决这个问题。但是,我遇到了一些困难,我不知道为什么这段代码不起作用: int[][] testTriangle = { {3}, {7, 4}, {2, 4, 6}, {8, 5, 9, 3} }; HashMap<Integer, Integer> hasGoneDownRoute = new HashMap<Integer, In

我正试图开发一个Java程序来解决这个问题。但是,我遇到了一些困难,我不知道为什么这段代码不起作用:

int[][] testTriangle = {
            {3},
            {7, 4},
            {2, 4, 6},
            {8, 5, 9, 3}
    };

    HashMap<Integer, Integer> hasGoneDownRoute = new HashMap<Integer, Integer>(); //Stores numbers and start positions to numbers

    int largestValue = 0;
    int value = 0;
    int row = testTriangle.length-1;
    for (int startPosition = 0; startPosition <= testTriangle[row].length-1; startPosition++) { //starts from all possible start positions on the bottom row
        for (int y = 1; y<=2; y++) { //executes from the same start position twice to get all possible routes (ACTUALLY THIS MIGHT BE WRONG!?)
            while (row != 0) { //until it reaches the top row
                if (startPosition == 0) { //if it's on the far left
                    value += testTriangle[row-1][0];
                }
                else if (startPosition == testTriangle[row].length-1) { //if at's on the far right
                    value += testTriangle[row-1][testTriangle[row-1].length-1]; //set the value to the row above it on the far right
                }
                else { //This never gets called?
                    int noToChooseFrom1 = testTriangle[row-1][startPosition]; //above it and to the right
                    int noToChooseFrom2 = testTriangle[row-1][startPosition-1]; //above it and to the left
                    if (hasGoneDownRoute.containsKey(noToChooseFrom1) && hasGoneDownRoute.get(noToChooseFrom1) == startPosition) { //checks if it has gone down a certain route before
                        value += noToChooseFrom2;
                        hasGoneDownRoute.put(testTriangle[row-1][startPosition-1], startPosition);
                    }
                    else {
                        value += noToChooseFrom1;
                        hasGoneDownRoute.put(noToChooseFrom1, startPosition);
                    }
                }
                row--;
            }
        }
        if (value > largestValue) {
            largestValue = value;
        }
        System.out.println(largestValue);
    }
int[]testTriangle={
{3},
{7, 4},
{2, 4, 6},
{8, 5, 9, 3}
};
HashMap hasGoneDownRoute=新HashMap()//将数字和起始位置存储到数字
int最大值=0;
int值=0;
int row=testTriangle.length-1;

对于(int startPosition=0;startPositionEDIT):刚看了这个问题,我的编程技能现在好多了。这可能是个死问题,但我宁愿给出正确的答案

您真正需要的是递归,它允许您以for循环不可能的方式向前看。(无论如何,很容易)

创建一个函数

public int findLargestPath(int[][] triangle, row , col) {

    // Check to see if the row and column are actually in the triangle
    if(col == -1 || col >= triangle[row].length) 
    {
        return -1; //If they are not. ensure that this path wont be taken. 
    }
    if(row == triangle.length -1)
    {
        return triangle[row][col]; //Return the value.
    }
    else
    {
        //return this value plus the maximum path below
        return triangle[row][col]+Math.max(findLargestPath(row+1,col),
                                           findLargestPath(row+1,col+1));
    }
}
然后在主管道内,它将是一个简单的

int[][] testTriangle = {
        {3},
        {7, 4},
        {2, 4, 6},
        {8, 5, 9, 3}
};

System.out.println("The Maximum Path = "+findLargestPath(testTriangle,0,0));
递归是一种跟踪所有路由的方法,无需创建新变量。我面前没有IDE。因此可能存在一些错误。如果您想让我修复它,请告诉我,当返回IDE时,我将对其进行测试。

动态规划方法:

导入java.io.*

公共班机{

public static void main(String[] args) {
    int tri[][] = {
        {
            6,
            0,
            0
        },
        {
            2,
            -3,
            0
        },
        {
            9,
            20,
            -1
        }
    };
    System.out.println(maxPathSum(tri)); 
}


static int maxPathSum(int tri[][]) {  
    for (int i = tri.length - 1; i > 0; i--) {
        for (int j = 0; j < i; j++) {
            //second condition in conditional ensures that we don't add 
            //negative numbers, which would only decrease our max sum
            if (tri[i][j] > tri[i][j + 1] && tri[i][j] + tri[i - 1][j] > tri[i - 1][j]) {
                tri[i - 1][j] += tri[i][j];
           } else if (tri[i][j] < tri[i][j + 1] && [i][j + 1] + tri[i - 1][j] > tri[i - 1][j]) {
                tri[i - 1][j] += tri[i][j + 1];
             }
        }
    }
    return tri[0][0];
}
publicstaticvoidmain(字符串[]args){
int tri[][]={
{
6.
0,
0
},
{
2.
-3,
0
},
{
9,
20,
-1
}
};
System.out.println(maxPathSum(tri));
}
静态int-maxPathSum(int-tri[][]){
对于(int i=tri.length-1;i>0;i--){
对于(int j=0;jtri[i][j+1]&tri[i][j]+tri[i-1][j]>tri[i-1][j]){
tri[i-1][j]+=tri[i][j];
}如果(tri[i][j]tri[i-1][j]){
tri[i-1][j]+=tri[i][j+1];
}
}
}
返回tri[0][0];
}

它输出什么?