- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
你能帮我解决这个 geeksforgeeks 的问题吗
给定一个正整数的 N X N 矩阵 Matrix[N][N]。一个单元格 Matrix[r][c] 只有三种可能的走法。
矩阵[r+1][c]
矩阵[r+1][c-1]
矩阵[r+1][c+1]
从第 0 行的任意列开始,返回直到第 N-1 行的所有路径的最大总和。
问题链接:https://practice.geeksforgeeks.org/problem-page.php?pid=271
解决方案链接:https://ide.geeksforgeeks.org/R5iu3xwTAO
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t =1;
int n;
int a[][];
while(t-->0){
n = 17;
a = new int [n][n];
int input[]={67,280,171,381,930,781,925,4,393,380,246,433,762,258,5,166,315,503,385,728,854,350,464,288,304,80,689,56,313,843,92,379,122,614,111,403,394,387,406,138,767,651,571,880,260,927,398,926,429,782,653,634,132,468,274,435,548,314,490,212,156,933,942,629,546,404,31,292,142,436,781,260,86,703,140,697,630,537,622,410,318,275,44,801,94,669,236,993,982,77,204,137,10,497,765,907,900,147,550,42,582,331,301,19,33,792,715,14,680,336,424,350,962,467,150,408,135,737,400,468,814,956,956,175,452,72,433,704,218,983,97,799,665,749,169,49,541,883,63,572,570,486,921,884,304,423,291,790,159,42,257,324,997,212,498,801,283,283,504,500,617,952,650,281,700,818,329,592,52,743,164,621,228,436,856,883,858,498,672,17,540,928,340,536,139,190,336,773,472,191,272,88,142,921,720,842,90,400,433,141,143,948,114,722,384,969,605,593,819,276,961,358,556,301,893,46,842,581,819,665,771,90,104,265,363,823,106,452,574,890,945,68,190,58,790,925,378,746,517,196,373,478,905,280,130,798,326,323,730,144,987,500,585,90,764,947,264,221,751,837,463,47,257,652,456,46,576,185,143,444,381,867,921,285,147,402,434,472,724,163,615,710,15,551,151,130,498,414,703};
int k=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
a[i][j] = input[k];
k++;
}
}
HashMap<String,Integer> h = new HashMap();
int max=Integer.MIN_VALUE,ans=0;
for(int i=0;i<a.length;i++){
ans=CostPath(a,0,i,h);
if(ans>max)
max=ans;
}
System.out.println(ans);
}
}
public static int CostPath(int a[][],int x,int y,HashMap<String,Integer>h){
if(h.containsKey(x+","+y))
return h.get(x+","+y);
int r;
if(x>=a.length || y>= a.length || x<0 || y<0 ){
r= Integer.MIN_VALUE;
}
else if(x==a.length-1 ){
r= a[x][y];
}
else{
r= a[x][y]+Math.max(Math.max(CostPath(a,x+1,y,h),CostPath(a,x+1,y-1,h)),CostPath(a,x+1,y-1,h));
}
h.put(x+","+y,r);
return r;
}
}
这应该给出输出 13785 但它给出了 10689
最佳答案
代码中有两个小错误:
r = a[x][y]+ Math.max(Math.max(CostPath(a,x+1,y,h),CostPath(a,x+1,y-1,h)),CostPath(a,x+1,y-1,h));
是错误的(CostPath(a,x+1,y-1,h)
计算了两次)应该是:
r = a[x][y]+ Math.max(Math.max(CostPath(a,x+1,y,h),CostPath(a,x+1,y-1,h)),CostPath(a,x+1,y+1,h));
还有这个
System.out.println(ans);
应该改为
System.out.println(max);
进行这两项更改后,输出是正确的。旁注:
无需将无效 x,y 值添加到 map 路径。为了防止它并使程序更有效的改变
r= Integer.MIN_VALUE;
到
return 0;
关于java - geeksforgeeks 上的矩阵路径问题的这个解决方案有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52463685/
BufferedImage image = ImageIO.read(SpriteSheet.class.getResource(path)); BufferedImage image = Image
希望有人能够帮助我解决将我的 React 应用程序推送到 Heroku 时遇到的问题。 heroku 日志反复显示以下错误。 at=error code=H10 desc="App crashed"
我是 Kotlin 的新手,我正在经历这样的例子。 . . package com.example.lambda1 import spark.Spark.get fun main(args: Arra
如果您已经安装了 32 位 JDK,请在中定义一个 JAVA_HOME 变量 Computer>System Properties>System Setting>Enviorment VAriable
我正在开发一个独立于平台的应用程序。我收到一个文件 URL*。在 Windows 上,这些是: file:///Z:/folder%20to%20file/file.txt file://host/f
我在 OSX、Objective-C 上。 我有一个像 这样的路径/NSURL /Users/xxx/Desktop/image2.png 但我将它传递给第三方应用程序,该应用程序会像 excpect
我已经安装了 Android studio 和插件的 DART,FLUTTER 来启动 flutter,但是因为我在创建我的第一个 flutter 项目时无法提供 sdk 路径。 最佳答案 我试图找出
127.0.0.1:8000/api/仅包含来自第二个应用程序的 url,但我将两个 url 模块链接到相同的模式。甚至有可能做到这一点吗? 第一个应用程序: from django.urls imp
对于大量图像(大约 1k,加上相同数量的拇指,在大约 500 个文件夹中),我们要求网站上使用的所有图像 URI 都必须具有 SEO 优化路径。它们已经准备好并提供完整的路径结构(每个文件夹包含一个具
为什么 f 不是一个文件?什么可能导致这种情况? String currentPhotoPath = "file:/storage/sdcard0/Pictures/someFileName.
Gradle 中的项目名称或路径中允许使用哪些字符? 它是否与特定操作系统的目录名称中允许的字符相同(例如: http://en.wikipedia.org/wiki/Filename#Reserve
我有一个包含文件夹路径的表格。我需要找到层次结构中这些文件夹之间的所有“差距”。我的意思是,如果表格包含这 3 个文件夹: 'A' 'A\B\C' 'A\B\C\D\E\F\G' 我需要在层次结构中找
我在 Linux 服务器上的/home/subversion 中安装了 svn - 那里有一个 ROOT 文件夹,其中包含 db 和 conf 等文件夹。没有映射到项目名称的文件夹,请有人告诉我如何列
对于我的图像位置:/src/assets/bitmap/sample.jpg 给出了关键配置: context: resolve('src') output: { path: resolve('b
我需要创建带有圆角的 SVG 路径,以将它们导出到 DXF 进行切割。我的问题是角应该是圆弧,而不是贝塞尔曲线。 使用 arc 命令相对容易处理直角,因为半径也是从拐角到圆弧起点的距离。对于其他角度,
大家好,我正在玩 Airflow,我正在阅读这篇很有帮助的 tutorial .我正在寻求帮助以更好地了解 Admin->Connection 如何在 Conn Type: File (path) 方
我的目标是定义R将用于安装和搜索库的单个路径。我read可以通过更改Rprofile.site安装路径中的R文件来完成。我在那里尝试了两个命令: .libPaths("D:/RLibrary") .L
我有一个问题:当我在一个页面中时,我想返回到上一页。我使用 $routeProvider。如何读取之前的 url? 我尝试在我的 Controller 中使用此代码但不起作用... angular.m
我正在尝试将一个文件从我的主干合并到一个分支(wc),并且对于看起来位于当前合并操作中不涉及的分支上的路径出现奇怪的未找到路径错误。 例如,在我们的 svn 项目中,我们有: 分行 分支 0 分支 1
我有一个树数据序列化如下: 关系:P到C是“一对多”,C到P是“一对一”。所以列 P 可能有重复的值,但列 C 有唯一的值。 P, C 1, 2 1, 3 3, 4 2, 5 4, 6 # in da
我是一名优秀的程序员,十分优秀!