当前位置:网站首页>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];
}
}
边栏推荐
- Selection of field types for creating tables in MySQL database
- The concept, function, characteristics, creation and deletion of MySQL constraints
- Construction and maintenance of business websites [12]
- Electronic Society C language level 1 32, calculate the power of 2
- Based on configured schedule, the given trigger will never fire
- Matlab uses audiorecorder and recordblocking to record sound, play to play sound, and audiobook to save sound
- 跨域?同源?一次搞懂什么是跨域
- 牛客网——华为题库(51~60)
- What style of Bluetooth headset is easy to use? High quality Bluetooth headset ranking
- 剑指 Offer 29. 顺时针打印矩阵
猜你喜欢
MySQL中一条SQL是怎么执行的
Five skills of adding audio codec to embedded system
What is AQS and its principle
matlab 实现语音信号重采样和归一化,并播放比对效果
剑指 Offer 62. 圆圈中最后剩下的数字
RTL8189FS如何关闭Debug信息
How to debug apps remotely and online?
Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory
[question] - why is optical flow not good for static scenes
Another programmer "deleted the library and ran away", deleted the code of the retail platform, and was sentenced to 10 months
随机推荐
Software No.1
Architecture evolution from MVC to DDD
leetcode2305. 公平分发饼干(中等,周赛,状压dp)
Logging only errors to the console Set system property ‘log4j2. debug‘ to sh
leetcode2311. Longest binary subsequence less than or equal to K (medium, weekly)
With the innovation and upgrading of development tools, Kunpeng promotes the "bamboo forest" growth of the computing industry
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
开发工具创新升级,鲲鹏推进计算产业“竹林”式生长
D discard the virtual recovery method
np. Where and torch Where usage
2022 Q2 - 提升技能的技巧总结
MATLAB realizes voice signal resampling and normalization, and plays the comparison effect
Construction and maintenance of business websites [14]
剑指 Offer II 031. 最近最少使用缓存
Ubuntu20.04 PostgreSQL 14 installation configuration record
Should enterprises choose server free computing?
[technology development -21]: rapid overview of the application and development of network and communication technology -1- Internet Network Technology
Feature extraction and detection 16 brisk feature detection and matching
Six lessons to be learned for the successful implementation of edge coding
Niuke - Huawei question bank (51~60)