-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathks_memoization.c
More file actions
26 lines (21 loc) · 883 Bytes
/
ks_memoization.c
File metadata and controls
26 lines (21 loc) · 883 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
int knapsack_memoization(int ind, int bag_weight, int *weights, int *values, int **max_weight){
// Edge case when we have to choose from last ind
if (ind == 0){
if (bag_weight >= weights[0]) return values[0];
return 0;
}
if (max_weight[ind][bag_weight] != -1) return max_weight[ind][bag_weight];
// Max weight till index ind if it is not picked
int notPick = 0 + knapsack_memoization(ind-1, bag_weight, weights, values, max_weight);
// Max weight till index ind if it is picked
int pick = INT_MIN;
if (bag_weight >= weights[ind]){
pick = values[ind] + knapsack_memoization(ind-1, bag_weight-weights[ind], weights, values, max_weight);
}
// Return max of both cases
return max_weight[ind][bag_weight] = fmax(pick, notPick);
}