当前位置:网站首页>剑指 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];
}
}
边栏推荐
- 基于SSM实现微博系统
- Redis有序集合如何使用
- Opengauss database backup and recovery guide
- 2022 Q2 - Summary of skills to improve skills
- Volume compression, decompression
- 医药管理系统(大一下C语言课设)
- Post infiltration flow encryption
- Construction and maintenance of business websites [15]
- 【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
- Réseau neuronal convolutif (y compris le Code et l'illustration correspondante)
猜你喜欢

479. Additive binary tree (interval DP on the tree)

k线图形态这样记(口诀篇)

What are the skills of spot gold analysis?

It's already 30. Can you learn programming from scratch?

MPLS experiment operation
![[技术发展-21]:网络与通信技术的应用与发展快速概览-1- 互联网网络技术](/img/2d/299fa5c76416f74bd1a693c433dd09.png)
[技术发展-21]:网络与通信技术的应用与发展快速概览-1- 互联网网络技术

The technology boss is ready, and the topic of position C is up to you

Game thinking 15: thinking about the whole region and sub region Services

matlab 使用 audiorecorder、recordblocking录制声音,play 播放声音,audiowrite 保存声音

人工智能在网络安全中的作用
随机推荐
KS006基于SSM实现学生成绩管理系统
Using mongodb in laravel
JMeter (I) - download, installation and plug-in management
Quatre stratégies de base pour migrer la charge de travail de l'informatique en nuage
MATLAB realizes voice signal resampling and normalization, and plays the comparison effect
Four basic strategies for migrating cloud computing workloads
MySQL如何解决delete大量数据后空间不释放的问题
matlab 实现语音信号重采样和归一化,并播放比对效果
2022 Q2 - 提升技能的技巧总结
Réseau neuronal convolutif (y compris le Code et l'illustration correspondante)
leetcode2310. 个位数字为 K 的整数之和(中等,周赛)
Another programmer "deleted the library and ran away", deleted the code of the retail platform, and was sentenced to 10 months
Experimental reproduction of variable image compression with a scale hyperprior
What style of Bluetooth headset is easy to use? High quality Bluetooth headset ranking
matlab 使用 audioread 、 sound 读取和播放 wav 文件
如何远程、在线调试app?
ES6 new method of string
Six lessons to be learned for the successful implementation of edge coding
开发工具创新升级,鲲鹏推进计算产业“竹林”式生长
Feature extraction and detection 16 brisk feature detection and matching