cap4 examples

main
lensfrex 6 months ago
parent 97674a6368
commit 9ca75884fc
Signed by: lensfrex
GPG Key ID: B1E395B3C6CA0356
  1. 10
      cap4/common.go
  2. 15
      cap4/ex15.go
  3. 22
      cap4/ex24.go
  4. 41
      cap4/ex25.go
  5. 31
      cap4/ex26.go
  6. 22
      cap4/ex27.go
  7. 17
      cap4/ex28.go
  8. 44
      cap4/example_test.go
  9. 1
      cap5/ex1.go

@ -0,0 +1,10 @@
package cap4
func _makeMatrix(m, n int) [][]int {
matrix := make([][]int, m)
for i := 0; i < m; i++ {
matrix[i] = make([]int, n)
}
return matrix
}

@ -0,0 +1,15 @@
package cap4
// Ex15 最大子段和
func Ex15(nums []int) int {
result := nums[0]
currMaxSum := nums[0]
n := len(nums)
for i := 1; i < n; i++ {
num := nums[i]
currMaxSum = max(currMaxSum+num, num)
result = max(result, currMaxSum)
}
return result
}

@ -0,0 +1,22 @@
package cap4
// Ex24 树塔问题
func Ex24(nums [][]int) (int, []int) {
steps := make([]int, 0)
dp := make([][]int, len(nums))
for i, layer := range nums {
dp[i] = make([]int, len(layer))
copy(dp[i], layer)
}
for i := len(nums) - 2; i >= 0; i-- {
layer := nums[i]
for j := range layer {
// 取最大的两个来源(下层同位置和下层同位置+1)
dp[i][j] += max(dp[i+1][j], dp[i+1][j+1])
}
}
return dp[0][0], steps
}

@ -0,0 +1,41 @@
package cap4
// Ex25 项目资源分配,项目数,资源数,收益表(第i个项目可得到的利润)
func Ex25(m, n int, income []float64) ([]int, float64) {
dp := make([]float64, n)
tmp := make([]float64, n)
a := _makeMatrix(m, n)
for i := 0; i < n; i++ {
a[0][i] = i
}
copy(dp, income)
copy(tmp, income)
// 前i个项目
for i := 1; i < m; i++ {
// 资源数限制在j时
for j := 0; j < n; j++ {
// 第i个项目可分配k个资源
for k := 0; k < j; k++ {
if dp[j-k]+income[k] > tmp[j] {
tmp[j] = dp[j-k] + income[k]
a[i][j] = k
}
}
}
copy(dp, tmp)
}
rest := n
gain := make([]int, m+1)
for i := m - 1; i > 0; i-- {
gain[i] = a[i][rest]
rest -= gain[i]
}
return gain, dp[n]
}

@ -0,0 +1,31 @@
package cap4
import (
"math"
)
// Ex26 矩阵连乘,p为矩阵链,p[0],p[1]代表第一个矩阵的行数和列数,p[1],p[2]代表第二个矩阵的行数和列数
func Ex26(p []int) (int, [][]int) {
length := len(p)
m, s := _makeMatrix(length, length), _makeMatrix(length, length)
for i := 1; i < length; i++ {
m[i][i] = 0
}
n := length - 1
for l := 2; l <= n; l++ {
for i := 1; i <= n-l+1; i++ {
j := i + l - 1
m[i][j] = math.MaxInt32
for k := i; k <= j-1; k++ {
q := m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j]
if q < m[i][j] {
m[i][j], s[i][j] = q, k
}
}
}
}
return m[1][length-1], s
}

@ -0,0 +1,22 @@
package cap4
import "strings"
// Ex27 最长公共子序列
func Ex27(str1, str2 []rune) (int, string) {
dp := _makeMatrix(len(str1)+1, len(str2)+1)
subStr := strings.Builder{}
for i := 1; i <= len(str1); i++ {
for j := 1; j <= len(str2); j++ {
a, b := str1[i], str2[j]
if a == b {
dp[i][j] = dp[i-1][j-1] + 1
subStr.WriteRune(a)
} else {
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
}
}
}
return dp[len(str1)][len(str2)], subStr.String()
}

@ -0,0 +1,17 @@
package cap4
// Ex28 最长不下降子序列
func Ex28(nums []int) int {
dp := make([]int, len(nums))
for i := 0; i < len(nums); i++ {
dp[i] = 1
for j := 0; j < len(nums); j++ {
if nums[i] > nums[j] {
dp[i] = max(dp[i], dp[j]+1)
}
}
}
return dp[len(dp)-1]
}

@ -0,0 +1,44 @@
package cap4
import (
"fmt"
"testing"
)
func TestEx14(t *testing.T) {
result := Ex15([]int{-1})
println(result)
}
func TestEx24(t *testing.T) {
nums := [][]int{
{9},
{12, 15},
{10, 6, 8},
{2, 18, 9, 5},
{19, 7, 10, 4, 16},
}
result, steps := Ex24(nums)
println(result)
for _, step := range steps {
fmt.Printf("%d ", step)
}
println()
}
func TestEx28(t *testing.T) {
nums := []int{3, 18, 7, 14, 10, 12, 23, 41, 16, 24}
result := Ex28(nums)
fmt.Println(result)
}
func PrintEx26Answer(s [][]int, i, j int) {
if i == j {
fmt.Printf("A%d", i)
} else {
fmt.Print("(")
PrintEx26Answer(s, i, s[i][j])
PrintEx26Answer(s, s[i][j]+1, j)
fmt.Print(")")
}
}

@ -0,0 +1 @@
package cap5
Loading…
Cancel
Save