当前位置:网站首页>Sword finger offer 47 Maximum value of gifts
Sword finger offer 47 Maximum value of gifts
2022-07-02 02:00:00 【Yake1965】
The finger of the sword Offer 47. The greatest value of gifts
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++){
// Start with the second line
for(int j = 1; j < n; j++){
// From the second column
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; // Number of columns
int[] arr = grid[0]; // First line prefix and
for(int i = 1; i < n ; i++){
arr[i] += arr[i-1];
}
for(int i = 1; i < m; i++){
// Start with the second line
arr[0] += grid[i][0];
for(int j = 1; j < n; j++){
// From the second column
// 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];
}
}
边栏推荐
- Data analysis on the disaster of Titanic
- How to execute an SQL in MySQL
- Number of palindromes in C language (leetcode)
- 医药管理系统(大一下C语言课设)
- leetcode2310. 个位数字为 K 的整数之和(中等,周赛)
- 2022 Q2 - résumé des compétences pour améliorer les compétences
- np. Where and torch Where usage
- leetcode2309. The best English letters with both upper and lower case (simple, weekly)
- Medical management system (C language course for freshmen)
- 遷移雲計算工作負載的四個基本策略
猜你喜欢

Word search applet design report based on cloud development +ppt+ project source code + demonstration video

Matlab uses resample to complete resampling

matlab 实现语音信号重采样和归一化,并播放比对效果

This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable

What is AQS and its principle

321. Chessboard segmentation (2D interval DP)

leetcode373. 查找和最小的 K 对数字(中等)

Selection of field types for creating tables in MySQL database
![[question] - why is optical flow not good for static scenes](/img/8d/2cf6f582bc58cc2985f50e3f85f334.jpg)
[question] - why is optical flow not good for static scenes
![[Floyd] post disaster reconstruction](/img/7a/f72c7781ef148212c870a56fb9a607.jpg)
[Floyd] post disaster reconstruction
随机推荐
人工智能在网络安全中的作用
Failed to transform file 'xxx' to match attributes
Word search applet design report based on cloud development +ppt+ project source code + demonstration video
How does MySQL solve the problem of not releasing space after deleting a large amount of data
leetcode2311. 小于等于 K 的最长二进制子序列(中等,周赛)
This is the form of the K-line diagram (pithy formula)
MySQL view concept, create view, view, modify view, delete view
大学的知识是否学而无用、过时?
Matlab uses audiorecorder and recordblocking to record sound, play to play sound, and audiobook to save sound
Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory
There are spaces in the for loop variable in the shell -- IFS variable
Implementation principle of city selector component
* and & symbols in C language
leetcode2312. 卖木头块(困难,周赛)
【视频】马尔可夫链蒙特卡罗方法MCMC原理与R语言实现|数据分享
OpenCASCADE7.6编译
How to execute an SQL in MySQL
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
遷移雲計算工作負載的四個基本策略
电子协会 C语言 1级 33 、奇偶数判断