-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompression.diff
More file actions
91 lines (87 loc) · 3.26 KB
/
Copy pathcompression.diff
File metadata and controls
91 lines (87 loc) · 3.26 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
--- input/input.rs
+++ output/output.rs
@@ -45,21 +45,7 @@
pub fn knapsack(capacity: usize, items: Vec<Item>) -> KnapsackSolution {
let num_items = items.len();
let item_weights: Vec<usize> = items.iter().map(|item| item.weight).collect();
- let item_values: Vec<usize> = items.iter().map(|item| item.value).collect();
-
- let knapsack_matrix = generate_knapsack_matrix(capacity, &item_weights, &item_values);
- let items_included =
- retrieve_knapsack_items(&item_weights, &knapsack_matrix, num_items, capacity);
-
- let total_weight = items_included
- .iter()
- .map(|&index| item_weights[index - 1])
- .sum();
-
- KnapsackSolution {
- optimal_profit: knapsack_matrix[num_items][capacity],
- total_weight,
- item_indices: items_included,
+ { … 15 line(s) … ⟦tj:3e19850ef0b4fe2769090bb083e337ec⟧ }
}
}
@@ -76,25 +62,7 @@
) -> Vec<Vec<usize>> {
let num_items = item_weights.len();
- (0..=num_items).fold(
- vec![vec![0; capacity + 1]; num_items + 1],
- |mut matrix, item_index| {
- (0..=capacity).for_each(|current_capacity| {
- matrix[item_index][current_capacity] = if item_index == 0 || current_capacity == 0 {
- 0
- } else if item_weights[item_index - 1] <= current_capacity {
- usize::max(
- item_values[item_index - 1]
- + matrix[item_index - 1]
- [current_capacity - item_weights[item_index - 1]],
- matrix[item_index - 1][current_capacity],
- )
- } else {
- matrix[item_index - 1][current_capacity]
- };
- });
- matrix
- },
+ { … 19 line(s) … ⟦tj:9144734b0bfb47a62e1ca6f8ca21b006⟧ }
)
}
@@ -116,29 +84,7 @@
) -> Vec<usize> {
match item_index {
0 => vec![],
- _ => {
- let current_value = knapsack_matrix[item_index][remaining_capacity];
- let previous_value = knapsack_matrix[item_index - 1][remaining_capacity];
-
- match current_value.cmp(&previous_value) {
- Ordering::Greater => {
- let mut knap = retrieve_knapsack_items(
- item_weights,
- knapsack_matrix,
- item_index - 1,
- remaining_capacity - item_weights[item_index - 1],
- );
- knap.push(item_index);
- knap
- }
- Ordering::Equal | Ordering::Less => retrieve_knapsack_items(
- item_weights,
- knapsack_matrix,
- item_index - 1,
- remaining_capacity,
- ),
- }
- }
+ { … 23 line(s) … ⟦tj:8dfc1f2b7ede0fba131f29480eb33d27⟧ }
}
}
@@ -361,3 +307,6 @@
),
}
}
+[omitted blocks are individually retrievable: call tinyjuice_retrieve with the token inside an omission marker to expand just that block]
+
+[PARTIAL view — full original (12426 bytes): call tinyjuice_retrieve with token "fb14afe7f59fbac023e5c6edd6a0c5e2"]
\ No newline at end of file