当前位置:网站首页>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];
}
}
边栏推荐
- Four basic strategies for migrating cloud computing workloads
- MySQL约束与多表查询实例分析
- How to build and use redis environment
- Using mongodb in laravel
- 【C#】使用正则校验内容
- 牛客网——华为题库(51~60)
- 开发那些事儿:如何利用Go单例模式保障流媒体高并发的安全性?
- 如何远程、在线调试app?
- C language 3-7 daffodils (enhanced version)
- Android: the kotlin language uses grendao3, a cross platform app development framework
猜你喜欢

Software No.1

如何用一款产品推动「品牌的惊险一跃」?

How to debug apps remotely and online?

Quatre stratégies de base pour migrer la charge de travail de l'informatique en nuage

What are the skills of spot gold analysis?

Architecture evolution from MVC to DDD

The concept, function, characteristics, creation and deletion of MySQL constraints

Discussion on the idea of platform construction

Golang lock

This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
随机推荐
Opengauss database backup and recovery guide
Matlab uses resample to complete resampling
Experimental reproduction of variable image compression with a scale hyperprior
医药管理系统(大一下C语言课设)
Openssl3.0 learning XXI provider encoder
Is the knowledge of University useless and outdated?
How does MySQL solve the problem of not releasing space after deleting a large amount of data
Matlab uses audiorecorder and recordblocking to record sound, play to play sound, and audiobook to save sound
leetcode2309. 兼具大小写的最好英文字母(简单,周赛)
Software No.1
SQLite 3 of embedded database
Iterative unified writing method of binary tree
2022 Q2 - résumé des compétences pour améliorer les compétences
Redis有序集合如何使用
Word search applet design report based on cloud development +ppt+ project source code + demonstration video
Design and implementation of key value storage engine based on LSM tree
RTL8189FS如何关闭Debug信息
matlab 使用 audioread 、 sound 读取和播放 wav 文件
遷移雲計算工作負載的四個基本策略
Construction and maintenance of business websites [13]