有解决的演习从codeChef麻烦[易]麻烦、codeChef

2023-09-12 21:20:57 作者:情深当斩.

所以,基本上我感觉是因为这个练习令人难以置信的愚蠢,我花了像4或5个小时试图code吧,到目前为止,我没有成功。

So, basically I'm feeling incredibly dumb, because of this exercise, I've spent like 4 or 5 hours trying to code it, and so far I've not been successful.

我已经开始认识到这个人是比较容易使用最长路径有树遍历解决的办法,但我不知道(请证实这个给我。),可能是过度杀死,因为它应该是容易的问题之一,所以你可以请帮助我有一些指导或基本的步骤或算法方法如何解决这一个?各种帮助肯定是AP preciated。

I have come to the realization that this one is easier to solve with a tree traversal using the Longest Path approach, But I'm not sure (Could you please confirm this to me.), could be over-kill since it's supposed to be one of the easy problems, So Could you please help me with some guidance or basic steps or algorithm approaches on how to solve this one? all kinds of help is certainly appreciated.

PS。我通常会发布一些code一下我这样做的远,但我相信这一点的一切已经错了我preFER从头开始,至少在理念上。

PS. I usually post some code about what I've done so far, but I believe that to this point everything has been so wrong that I prefer to start from scratch, at least in terms of ideas.

感谢。

由于每个请求,这里的code我按照接受的答案来解决练习类型:

As per-request, here's the Code I typed according to the accepted answer to solve the Exercise:

def get_max_sum(matrix)
  (1...matrix.length).each do |index|  
    flag = 0  
    matrix[index].each_with_index do |e, i|    
      add = (matrix[index-1][flag] > matrix[index-1][flag+1]) ? matrix[index-1][flag] : matrix[index-1][flag+1]
      e += add
      matrix[index][i] = e
      flag = flag + 1
    end    
  end
  matrix[-1][0]
end

其中矩阵参数是数组的数组,每个人再$ P $从三角psenting一行。

Where the matrix param is an array of arrays, each one representing a row from the triangle.

推荐答案

这个问题很容易,如果你从底部开始和您的方式工作。考虑三角

This problem is easy if you start from the bottom and work your way up. Consider the triangle

   1
  1 2
 4 1 2
2 3 1 1

先看下到最后一排。如果通过你在4到三角形的一些路径,你会向右移动到3,给出7的总和(加上无论是在它上面的路径)。如果你已经达到了1,你会向左移动到3,给人4的总和(加上无论是在它上面的路径)。如果你在2,你可以将任何一种方式为3的总和(加上无论是在它上面的路径)。因此,通过与求和替换下一个到最后一行,三角

Look at the next-to-last row. If by some path through the triangle you arrive at 4, you will move right to the 3, giving a sum of 7 (plus whatever is in the path above it). If you've reached 1, you will move left to the 3, giving a sum of 4 (plus whatever is in the path above it). If you're at 2, you can move either way for a sum of 3 (plus whatever is in the path above it). Thus, by replacing the next-to-last row with the sums, the triangle

  1
 1 2
7 4 3

将具有相同的最大总和的路径作为原三角形。现在做同样的过程递归的减少三角形。从1就下到最后一排向左移动到7,给人8之和,并从2向左移动4,给人6.总和减少的三角现在看起来像

will have the same maximum-sum path as the original triangle. Now do the same process recursively on the reduced triangle. From 1 on the next-to-last row move left to 7, giving a sum of 8, and from 2 move left to 4, giving a sum of 6. The reduced triangle now looks like

 1
8 6

最后,从1上的下一个到最后一排向左移动到8,得到的总和的9,这是答案的问题。

Finally, from 1 on the next-to-last row move left to 8, giving a sum of 9, which is the answer to the problem.

还有,从上而下工作的方法。在每一个步骤替换在与通向该数目的任何路径的最大总和的三角形每个号码。从顶部开始,三角形启动

There is also a method of working from the top down. At each step you replace each number in the triangle with the maximum-sum of any path leading to that number. Starting from the top, the triangle starts

1

然后,第二行被替换其总和

Then the second row is replaced by its sums

 1
2 3

然后在第三行

Then the third row

  1
 2 3
6 4 5

和最后的第四行

   1
  2 3
 6 4 5
8 9 6 6

答案是最下面一排,这是9。我总觉得自上而下的方式更难管理比自下而上的方法中最大的一笔,但两种算法均采用双对方,所以它的您选择哪种实现。自上而下的方法确实有,你可以积累为你读数据的下一行的优点;与自下而上的方法,你必须阅读和存储整个输入您计算任何款项之前。

The answer is the largest sum in the bottom row, which is 9. I've always found the top-down approach harder to manage than the bottom-up approach, but the two algorithms are dual to each other, so it's your choice which to implement. The top-down approach does have the advantage that you can accumulate the next row as you're reading the data; with the bottom-up approach, you have to read and store the entire input before you compute any of the sums.

我会离开它给你写的code。当你做什么,请记住,你只需要存储两行的时间,在previous行和下一行。这是previous且毗邻取决于你的工作自上而下或自下而上 - 在previous行是您刚才填写的行和下一行是您目前的行工作,这意味着如果你的工作自上而下的下一行有一个比previous行多总结,如果你的工作自下而上的下一行有超过$ P $一笔少pvious一行。请发表您的code,当你得到它的工作,以便其他人可以从你的努力学习。

I'll leave it to you to write the code. When you do, remember that you only need to store two rows at a time, the previous row and the next row. Which is previous and which is next depends on whether you're working top-down or bottom-up -- the previous row is the row you just filled in and the next row is the row you're currently working on, which means that if you're working top-down the next row has one more sum than the previous row, and if you're working bottom-up the next row has one less sum than the previous row. Please post your code when you get it working, so others can learn from your effort.

顺便说一句,这个问题最初来源于项目欧拉。 code厨师从他们偷的,显然没有归属,这真的不是一个非常好的事情。

By the way, this problem originally comes from Project Euler. Code Chef stole it from them, apparently without attribution, which really isn't a very nice thing to do.