mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #352 from zhangzw001/master
添加背包问题理论基础完全背包、0518 、 0377 、0070 、0322 、0279 Go版本
This commit is contained in:
@ -149,7 +149,26 @@ Python:
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```go
|
||||||
|
func climbStairs(n int) int {
|
||||||
|
//定义
|
||||||
|
dp := make([]int, n+1)
|
||||||
|
//初始化
|
||||||
|
dp[0] = 1
|
||||||
|
// 本题物品只有两个1,2
|
||||||
|
m := 2
|
||||||
|
// 遍历顺序
|
||||||
|
for j := 1; j <= n; j++ { //先遍历背包
|
||||||
|
for i := 1; i <= m; i++ { //再遍历物品
|
||||||
|
if j >= i {
|
||||||
|
dp[j] += dp[j-i]
|
||||||
|
}
|
||||||
|
//fmt.Println(dp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dp[n]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -186,7 +186,55 @@ Python:
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```go
|
||||||
|
// 版本一,先遍历物品, 再遍历背包
|
||||||
|
func numSquares1(n int) int {
|
||||||
|
//定义
|
||||||
|
dp := make([]int, n+1)
|
||||||
|
// 初始化
|
||||||
|
dp[0] = 0
|
||||||
|
for i := 1; i <= n; i++ {
|
||||||
|
dp[i] = math.MaxInt32
|
||||||
|
}
|
||||||
|
// 遍历物品
|
||||||
|
for i := 1; i <= n; i++ {
|
||||||
|
// 遍历背包
|
||||||
|
for j := i*i; j <= n; j++ {
|
||||||
|
dp[j] = min(dp[j], dp[j-i*i]+1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dp[n]
|
||||||
|
}
|
||||||
|
|
||||||
|
// 版本二,先遍历背包, 再遍历物品
|
||||||
|
func numSquares2(n int) int {
|
||||||
|
//定义
|
||||||
|
dp := make([]int, n+1)
|
||||||
|
// 初始化
|
||||||
|
dp[0] = 0
|
||||||
|
// 遍历背包
|
||||||
|
for j := 1; j <= n; j++ {
|
||||||
|
//初始化
|
||||||
|
dp[j] = math.MaxInt32
|
||||||
|
// 遍历物品
|
||||||
|
for i := 1; i <= n; i++ {
|
||||||
|
if j >= i*i {
|
||||||
|
dp[j] = min(dp[j], dp[j-i*i]+1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dp[n]
|
||||||
|
}
|
||||||
|
|
||||||
|
func min(a, b int) int {
|
||||||
|
if a < b {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -211,7 +211,68 @@ Python:
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```go
|
||||||
|
// 版本一, 先遍历物品,再遍历背包
|
||||||
|
func coinChange1(coins []int, amount int) int {
|
||||||
|
dp := make([]int, amount+1)
|
||||||
|
// 初始化dp[0]
|
||||||
|
dp[0] = 0
|
||||||
|
// 初始化为math.MaxInt32
|
||||||
|
for j := 1; j <= amount; j++ {
|
||||||
|
dp[j] = math.MaxInt32
|
||||||
|
}
|
||||||
|
|
||||||
|
// 遍历物品
|
||||||
|
for i := 0; i < len(coins); i++ {
|
||||||
|
// 遍历背包
|
||||||
|
for j := coins[i]; j <= amount; j++ {
|
||||||
|
//if dp[j-coins[i]] != math.MaxInt32 {
|
||||||
|
// 推导公式
|
||||||
|
dp[j] = min(dp[j], dp[j-coins[i]]+1)
|
||||||
|
fmt.Println(dp,j,i)
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 没找到能装满背包的, 就返回-1
|
||||||
|
if dp[amount] == math.MaxInt32 {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return dp[amount]
|
||||||
|
}
|
||||||
|
|
||||||
|
// 版本二,先遍历背包,再遍历物品
|
||||||
|
func coinChange2(coins []int, amount int) int {
|
||||||
|
dp := make([]int, amount+1)
|
||||||
|
// 初始化dp[0]
|
||||||
|
dp[0] = 0
|
||||||
|
// 遍历背包,从1开始
|
||||||
|
for j := 1; j <= amount; j++ {
|
||||||
|
// 初始化为math.MaxInt32
|
||||||
|
dp[j] = math.MaxInt32
|
||||||
|
// 遍历物品
|
||||||
|
for i := 0; i < len(coins); i++ {
|
||||||
|
if j >= coins[i] && dp[j-coins[i]] != math.MaxInt32 {
|
||||||
|
// 推导公式
|
||||||
|
dp[j] = min(dp[j], dp[j-coins[i]]+1)
|
||||||
|
//fmt.Println(dp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 没找到能装满背包的, 就返回-1
|
||||||
|
if dp[amount] == math.MaxInt32 {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return dp[amount]
|
||||||
|
}
|
||||||
|
|
||||||
|
func min(a, b int) int {
|
||||||
|
if a < b {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -183,7 +183,23 @@ class Solution:
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```go
|
||||||
|
func combinationSum4(nums []int, target int) int {
|
||||||
|
//定义dp数组
|
||||||
|
dp := make([]int, target+1)
|
||||||
|
// 初始化
|
||||||
|
dp[0] = 1
|
||||||
|
// 遍历顺序, 先遍历背包,再循环遍历物品
|
||||||
|
for j:=0;j<=target;j++ {
|
||||||
|
for i:=0 ;i < len(nums);i++ {
|
||||||
|
if j >= nums[i] {
|
||||||
|
dp[j] += dp[j-nums[i]]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dp[target]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -224,7 +224,24 @@ class Solution:
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```go
|
||||||
|
func change(amount int, coins []int) int {
|
||||||
|
// 定义dp数组
|
||||||
|
dp := make([]int, amount+1)
|
||||||
|
// 初始化,0大小的背包, 当然是不装任何东西了, 就是1种方法
|
||||||
|
dp[0] = 1
|
||||||
|
// 遍历顺序
|
||||||
|
// 遍历物品
|
||||||
|
for i := 0 ;i < len(coins);i++ {
|
||||||
|
// 遍历背包
|
||||||
|
for j:= coins[i] ; j <= amount ;j++ {
|
||||||
|
// 推导公式
|
||||||
|
dp[j] += dp[j-coins[i]]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dp[amount]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -220,7 +220,58 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```go
|
||||||
|
|
||||||
|
// test_CompletePack1 先遍历物品, 在遍历背包
|
||||||
|
func test_CompletePack1(weight, value []int, bagWeight int) int {
|
||||||
|
// 定义dp数组 和初始化
|
||||||
|
dp := make([]int, bagWeight+1)
|
||||||
|
// 遍历顺序
|
||||||
|
for i := 0; i < len(weight); i++ {
|
||||||
|
// 正序会多次添加 value[i]
|
||||||
|
for j := weight[i]; j <= bagWeight; j++ {
|
||||||
|
// 推导公式
|
||||||
|
dp[j] = max(dp[j], dp[j-weight[i]]+value[i])
|
||||||
|
// debug
|
||||||
|
//fmt.Println(dp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dp[bagWeight]
|
||||||
|
}
|
||||||
|
|
||||||
|
// test_CompletePack2 先遍历背包, 在遍历物品
|
||||||
|
func test_CompletePack2(weight, value []int, bagWeight int) int {
|
||||||
|
// 定义dp数组 和初始化
|
||||||
|
dp := make([]int, bagWeight+1)
|
||||||
|
// 遍历顺序
|
||||||
|
// j从0 开始
|
||||||
|
for j := 0; j <= bagWeight; j++ {
|
||||||
|
for i := 0; i < len(weight); i++ {
|
||||||
|
if j >= weight[i] {
|
||||||
|
// 推导公式
|
||||||
|
dp[j] = max(dp[j], dp[j-weight[i]]+value[i])
|
||||||
|
}
|
||||||
|
// debug
|
||||||
|
//fmt.Println(dp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dp[bagWeight]
|
||||||
|
}
|
||||||
|
|
||||||
|
func max(a, b int) int {
|
||||||
|
if a > b {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
weight := []int{1, 3, 4}
|
||||||
|
price := []int{15, 20, 30}
|
||||||
|
fmt.Println(test_CompletePack1(weight, price, 4))
|
||||||
|
fmt.Println(test_CompletePack2(weight, price, 4))
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user