-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
knapsack.go
52 lines (47 loc) · 1021 Bytes
/
knapsack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package dynamic
// Knapsack Problem
// https://www.geeksforgeeks.org/0-1-knapsack-problem-dp-10/
// https://en.wikipedia.org/wiki/Knapsack_problem
// time complexity: O(n*maxWeight)
// space complexity: O(n*maxWeight)
import (
"math"
)
// Max function - possible duplicate
func Max(a, b int) int {
return int(math.Max(float64(a), float64(b)))
}
// Knapsack solves knapsack problem
// return maxProfit
func Knapsack(maxWeight int, weights, values []int) int {
n := len(weights)
m := maxWeight
// create dp data structure
dp := make([][]int, n+1)
for i := range dp {
dp[i] = make([]int, m+1)
}
for i := 0; i < len(weights); i++ {
for j := 0; j <= maxWeight; j++ {
if weights[i] > j {
dp[i+1][j] = dp[i][j]
} else {
dp[i+1][j] = Max(dp[i][j-weights[i]]+values[i], dp[i][j])
}
}
}
return dp[n][m]
}
/*
func main() {
maxWeight := 50
values := []int{
60, 100, 120,
}
weights := []int{
10, 20, 30,
}
maxProfit := Knapsack(maxWeight, weights, values)
fmt.Println(maxProfit)
}
*/