当前位置:网站首页>剑指 Offer 47. 礼物的最大价值
剑指 Offer 47. 礼物的最大价值
2022-07-02 01:54:00 【Yake1965】
剑指 Offer 47. 礼物的最大价值
class Solution {
public int maxValue(int[][] grid) {
int n = grid[0].length, m = grid.length;
int[][] dp = new int[m][n];
dp[0][0] = grid[0][0];
for(int i = 1; i < n ; i++){
dp[0][i] += dp[0][i-1] + grid[0][i];
}
for(int i = 1; i < m ; i++){
dp[i][0] += dp[i-1][0] + grid[i][0];
}
for(int i = 1; i < m; i++){
// 从第二行开始
for(int j = 1; j < n; j++){
// 从第二列开始
dp[i][j] = Math.max(dp[i][j-1], dp[i-1][j]) + grid[i][j];
}
}
return dp[m-1][n-1];
}
}
class Solution {
public int maxValue(int[][] grid) {
int n = grid[0].length, m = grid.length; // 列数
int[] arr = grid[0]; // 第一行前缀和
for(int i = 1; i < n ; i++){
arr[i] += arr[i-1];
}
for(int i = 1; i < m; i++){
// 从第二行开始
arr[0] += grid[i][0];
for(int j = 1; j < n; j++){
// 从第二列开始
// arr[j] = Math.max(arr[j], arr[j-1]) + grid[i][j];
arr[j] = (arr[j] > arr[j-1] ? arr[j] : arr[j-1]) + grid[i][j];
}
}
return arr[n-1];
}
}
边栏推荐
- Word search applet design report based on cloud development +ppt+ project source code + demonstration video
- Have you stepped on the nine common pits in the e-commerce system?
- The concepts and differences between MySQL stored procedures and stored functions, as well as how to create them, the role of delimiter, the viewing, modification, deletion of stored procedures and fu
- Pyldavis installation and use | attributeerror: module 'pyldavis' has no attribute' gensim '| visual results are exported as separate web pages
- Failed to transform file 'xxx' to match attributes
- Ks006 student achievement management system based on SSM
- What are the skills of spot gold analysis?
- The difference between new and malloc
- 分卷压缩,解压
- Regular expression learning notes
猜你喜欢
Learn basic K-line diagram knowledge in three minutes
Word search applet design report based on cloud development +ppt+ project source code + demonstration video
This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
Android: how can golden nine and silver ten squeeze into the first-line big factories from small and medium-sized enterprises? The depth of interview questions in large factories
Cross domain? Homology? Understand what is cross domain at once
Game thinking 15: thinking about the whole region and sub region Services
成功实现边缘编码需要了解的六大经验教训
What are the necessary things for students to start school? Ranking list of Bluetooth headsets with good sound quality
MySQL view concept, create view, view, modify view, delete view
MySQL主从延迟问题怎么解决
随机推荐
正则表达式学习笔记
遷移雲計算工作負載的四個基本策略
1217 supermarket coin processor
What style of Bluetooth headset is easy to use? High quality Bluetooth headset ranking
Redis环境搭建和使用的方法
What are the affordable Bluetooth headsets? Student party parity Bluetooth headset recommendation
Learn basic K-line diagram knowledge in three minutes
三分钟学会基础k线图知识
This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
Makefile simple induction
Opengauss database backup and recovery guide
From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
2022 Q2 - résumé des compétences pour améliorer les compétences
Experimental reproduction of variable image compression with a scale hyperprior
Construction and maintenance of business websites [14]
Develop those things: how to use go singleton mode to ensure the security of high concurrency of streaming media?
Five skills of adding audio codec to embedded system
Openssl3.0 learning XXI provider encoder
基于SSM实现微博系统
Word search applet design report based on cloud development +ppt+ project source code + demonstration video