当前位置:网站首页>剑指 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];
}
}
边栏推荐
- VARIATIONAL IMAGE COMPRESSION WITH A SCALE HYPERPRIOR文献实验复现
- 大学的知识是否学而无用、过时?
- [Floyd] post disaster reconstruction
- Redis环境搭建和使用的方法
- Penser au jeu 15: penser au service complet et au sous - service
- Game thinking 15: thinking about the whole region and sub region Services
- Three core problems of concurrent programming
- The smart Park "ZhongGuanCun No.1" subverts your understanding of the park
- matlab 使用 resample 完成重采样
- Another programmer "deleted the library and ran away", deleted the code of the retail platform, and was sentenced to 10 months
猜你喜欢

6-2 vulnerability exploitation - inevitable problems of FTP

New news, Wuhan Yangluo international port, filled with black technology, refreshes your understanding of the port

leetcode2311. 小于等于 K 的最长二进制子序列(中等,周赛)

Raspberry pie 4B learning notes - IO communication (1-wire)

Volume compression, decompression

KS006基于SSM实现学生成绩管理系统

Matlab uses resample to complete resampling

开发工具创新升级,鲲鹏推进计算产业“竹林”式生长

Cross domain? Homology? Understand what is cross domain at once

MATLAB realizes voice signal resampling and normalization, and plays the comparison effect
随机推荐
遷移雲計算工作負載的四個基本策略
Six lessons to be learned for the successful implementation of edge coding
What style of Bluetooth headset is easy to use? High quality Bluetooth headset ranking
[C #] use regular verification content
Self drawing of menu items and CListBox items
Four basic strategies for migrating cloud computing workloads
mysql列转行函数指的是什么
MySQL约束与多表查询实例分析
Failed to transform file 'xxx' to match attributes
Construction and maintenance of business websites [11]
The role of artificial intelligence in network security
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
ES6 new method of string
正则表达式学习笔记
并发编程的三大核心问题
matlab 实现语音信号重采样和归一化,并播放比对效果
人工智能在网络安全中的作用
C language 3-7 daffodils (enhanced version)
医药管理系统(大一下C语言课设)
1222. Password dropping (interval DP, bracket matching)