Skip to content

Commit 0e049af

Browse files
committed
add offer47
1 parent 76c45e9 commit 0e049af

File tree

3 files changed

+64
-13
lines changed

3 files changed

+64
-13
lines changed

README.md

+14-13
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33

44

55
### Problems
6-
| # | Title | Solution | Difficulty |
7-
| :---------------------: | :---------------------------------------------------------------------------------------------------------: | :-----------------------------: | :--------: |
8-
| [1](./algorithms/0001) | [1. 两数之和](https://leetcode-cn.com/problems/two-sum/) | [Go](./algorithms/0001/main.go) | 简单 |
9-
| [2](./algorithms/0002) | [2. 两数相加](https://leetcode-cn.com/problems/add-two-numbers/) | [Go](./algorithms/0002/main.go) | 中等 |
10-
| [3](./algorithms/0003) | [3. 无重复字符的最长子串](https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/) | [Go](./algorithms/0003/main.go) | 中等 |
11-
| [4](./algorithms/0004) | [4. 寻找两个正序数组的中位数](https://leetcode-cn.com/problems/median-of-two-sorted-arrays/) | [Go](./algorithms/0004/main.go) | 困难 |
12-
| [5](./algorithms/0005) | [5. 最长回文子串](https://leetcode-cn.com/problems/longest-palindromic-substring/) | [Go](./algorithms/0005/main.go) | 中等 |
13-
| [7](./algorithms/0007) | [7. 整数反转](https://leetcode-cn.com/problems/reverse-integer/) | [Go](./algorithms/0007/main.go) | 简单 |
14-
| [8](./algorithms/0008) | [8. 字符串转换整数 (atoi)](https://leetcode-cn.com/problems/string-to-integer-atoi/) | [Go](./algorithms/0008/main.go) | 中等 |
15-
| [9](./algorithms/0009) | [9. 回文数](https://leetcode-cn.com/problems/palindrome-number/) | [Go](./algorithms/0009/main.go) | 简单 |
16-
| [11](./algorithms/0011) | [11. 盛最多水的容器](https://leetcode-cn.com/problems/container-with-most-water/) | [Go](./algorithms/0011/main.go) | 中等 |
17-
| [14](./algorithms/0014) | [14. 最长公共前缀](https://leetcode-cn.com/problems/longest-common-prefix/) | [Go](./algorithms/0014/main.go) | 简单 |
18-
| [79](./algorithms/0079) | [79. 单词搜索](https://leetcode-cn.com/problems/word-search/) | [Go](./algorithms/0079/main.go) | 中等 |
6+
| # | Title | Solution | Difficulty |
7+
| :-----------------------------: | :---------------------------------------------------------------------------------------------------------: | :--------------------------------: | :--------: |
8+
| [1](./algorithms/0001) | [1. 两数之和](https://leetcode-cn.com/problems/two-sum/) | [Go](./algorithms/0001/main.go) | 简单 |
9+
| [2](./algorithms/0002) | [2. 两数相加](https://leetcode-cn.com/problems/add-two-numbers/) | [Go](./algorithms/0002/main.go) | 中等 |
10+
| [3](./algorithms/0003) | [3. 无重复字符的最长子串](https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/) | [Go](./algorithms/0003/main.go) | 中等 |
11+
| [4](./algorithms/0004) | [4. 寻找两个正序数组的中位数](https://leetcode-cn.com/problems/median-of-two-sorted-arrays/) | [Go](./algorithms/0004/main.go) | 困难 |
12+
| [5](./algorithms/0005) | [5. 最长回文子串](https://leetcode-cn.com/problems/longest-palindromic-substring/) | [Go](./algorithms/0005/main.go) | 中等 |
13+
| [7](./algorithms/0007) | [7. 整数反转](https://leetcode-cn.com/problems/reverse-integer/) | [Go](./algorithms/0007/main.go) | 简单 |
14+
| [8](./algorithms/0008) | [8. 字符串转换整数 (atoi)](https://leetcode-cn.com/problems/string-to-integer-atoi/) | [Go](./algorithms/0008/main.go) | 中等 |
15+
| [9](./algorithms/0009) | [9. 回文数](https://leetcode-cn.com/problems/palindrome-number/) | [Go](./algorithms/0009/main.go) | 简单 |
16+
| [11](./algorithms/0011) | [11. 盛最多水的容器](https://leetcode-cn.com/problems/container-with-most-water/) | [Go](./algorithms/0011/main.go) | 中等 |
17+
| [14](./algorithms/0014) | [14. 最长公共前缀](https://leetcode-cn.com/problems/longest-common-prefix/) | [Go](./algorithms/0014/main.go) | 简单 |
18+
| [79](./algorithms/0079) | [79. 单词搜索](https://leetcode-cn.com/problems/word-search/) | [Go](./algorithms/0079/main.go) | 中等 |
19+
| [offer47](./algorithms/offer47) | [剑指 Offer 47. 礼物的最大价值](https://leetcode-cn.com/problems/li-wu-de-zui-da-jie-zhi-lcof/) | [Go](./algorithms/offer47/main.go) | 中等 |
1920

2021

2122

algorithms/offer47/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#### [剑指 Offer 47. 礼物的最大价值](https://leetcode-cn.com/problems/li-wu-de-zui-da-jie-zhi-lcof/)
2+
3+
难度中等
4+
5+
在一个 m*n 的棋盘的每一格都放有一个礼物,每个礼物都有一定的价值(价值大于 0)。你可以从棋盘的左上角开始拿格子里的礼物,并每次向右或者向下移动一格、直到到达棋盘的右下角。给定一个棋盘及其上面的礼物的价值,请计算你最多能拿到多少价值的礼物?
6+
7+
8+
9+
**示例 1:**
10+
11+
```
12+
输入:
13+
[
14+
[1,3,1],
15+
[1,5,1],
16+
[4,2,1]
17+
]
18+
输出: 12
19+
解释: 路径 1→3→5→2→1 可以拿到最多价值的礼物
20+
```
21+
22+
23+
24+
提示:
25+
26+
- `0 < grid.length <= 200`
27+
- `0 < grid[0].length <= 200`

algorithms/offer47/main.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package offer47
2+
3+
func maxValue(grid [][]int) int {
4+
for i := 0; i < len(grid)-1; i++ {
5+
grid[i+1][0] += grid[i][0]
6+
}
7+
for i := 0; i < len(grid[0])-1; i++ {
8+
grid[0][i+1] += grid[0][i]
9+
}
10+
for m := 1; m < len(grid); m++ {
11+
for n := 1; n < len(grid[0]); n++ {
12+
grid[m][n] += max(grid[m][n-1], grid[m-1][n])
13+
}
14+
}
15+
return grid[len(grid)-1][len(grid[0])-1]
16+
}
17+
18+
func max(x, y int) int {
19+
if x > y {
20+
return x
21+
}
22+
return y
23+
}

0 commit comments

Comments
 (0)