-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
knapsack.jl
62 lines (55 loc) · 1.46 KB
/
knapsack.jl
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
53
54
55
56
57
58
59
60
61
62
#= You are a thief who has a bag with some capacity, you broke into a hous
and you found some valuables, you want to take the maximum value back
with you. You are confused what to pick, no worries, coders are here
to help you in your burglary. =#
## Function
function knapsack(weights, values, items, capacity)
if (items == 0 || capacity == 0)
return 0
end
if (weights[items] > capacity)
return knapsack(weights, values, items - 1, capacity)
end
return max(
knapsack(weights, values, items - 1, capacity - weights[items]) + values[items],
knapsack(weights, values, items - 1, capacity),
)
end
## Input
println("Enter the number of items")
n = readline()
n = parse(Int64, n)
println("Enter the capacity of the knapsack")
w = readline()
w = parse(Int64, w)
values = Int64[]
println("Enter the values of each item")
for i = 1:n
temp = readline()
temp = parse(Int64, temp)
push!(values, temp)
end
weights = Int64[]
println("Enter the weights of each item")
for i = 1:n
temp = readline()
temp = parse(Int64, temp)
push!(weights, temp)
end
## Calling the function
print("The maximum value you could take is ")
print(knapsack(weights, values, n, w))
#=
Sample Test Case:
Input:
Enter the number of items
3
Enter the capacity of the knapsack
50
Enter the values of each item
60 100 120
Enter the weights of each item
10 20 30
Ouput:
The maximum value you could take is 220
=#