summaryrefslogtreecommitdiffstats
path: root/01-knapsack/0-works-only-if-sorted-by-val-or-weight-asc.patch
diff options
context:
space:
mode:
Diffstat (limited to '01-knapsack/0-works-only-if-sorted-by-val-or-weight-asc.patch')
-rw-r--r--01-knapsack/0-works-only-if-sorted-by-val-or-weight-asc.patch38
1 files changed, 38 insertions, 0 deletions
diff --git a/01-knapsack/0-works-only-if-sorted-by-val-or-weight-asc.patch b/01-knapsack/0-works-only-if-sorted-by-val-or-weight-asc.patch
new file mode 100644
index 0000000..bb4a6e1
--- /dev/null
+++ b/01-knapsack/0-works-only-if-sorted-by-val-or-weight-asc.patch
@@ -0,0 +1,38 @@
+diff --git a/01-knapsack/ks_dp-ng.c b/01-knapsack/ks_dp-ng.c
+index 7830651..106328b 100644
+--- a/01-knapsack/ks_dp-ng.c
++++ b/01-knapsack/ks_dp-ng.c
+@@ -120,20 +120,32 @@ static void solve(Solver* solver)
+
+ // this computes the top-left bottom-right diagonal,
+ // values above this don't need to be computed
++ j = k;
+ min_c -= w;
+ lower_bound = (k - min_c);
+ if (lower_bound < w)
+ lower_bound = w;
+- for (j = k; j >= lower_bound; j--, data--, up_left--)
++
++ while (j >= lower_bound)
+ {
+ counter++;
++ /* printf(" % 4d\n", j); */
+
+ if (data->v < (v + up_left->v))
+ {
+ data->i = i;
+ data->v = v + up_left->v;
+ data->w = w + up_left->w;
++ if (j == k) {
++ j -= w;
++ data -= w;
++ up_left -= w;
++ continue;
++ }
+ }
++ j--;
++ data--;
++ up_left--;
+ }
+
+ if (debug > 2)