101 Logo
onenoughtone

Code Implementation

Hiking Backpack Packer Implementation

Below is the implementation of the hiking backpack packer:

solution.js
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
92
93
// Recursive Backtracking Approach
function canPackExactly(gearWeights, capacity) {
return subsetSum(gearWeights, capacity, 0);
}
function subsetSum(gearWeights, capacity, index) {
// Base cases
if (capacity === 0) {
return true;
}
if (capacity < 0 || index >= gearWeights.length) {
return false;
}
// Recursive cases
// Exclude the current item
if (subsetSum(gearWeights, capacity, index + 1)) {
return true;
}
// Include the current item
return subsetSum(gearWeights, capacity - gearWeights[index], index + 1);
}
// Dynamic Programming Approach
function canPackExactlyDP(gearWeights, capacity) {
const n = gearWeights.length;
// Create a 2D DP table
const dp = Array(n + 1).fill().map(() => Array(capacity + 1).fill(false));
// Base case: empty subset sums to 0
for (let i = 0; i <= n; i++) {
dp[i][0] = true;
}
// Fill the DP table
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= capacity; j++) {
// If current item is too heavy, exclude it
if (gearWeights[i - 1] > j) {
dp[i][j] = dp[i - 1][j];
} else {
// Either exclude or include the current item
dp[i][j] = dp[i - 1][j] || dp[i - 1][j - gearWeights[i - 1]];
}
}
}
return dp[n][capacity];
}
// Space-Optimized Dynamic Programming Approach
function canPackExactlyOptimized(gearWeights, capacity) {
// Create a 1D DP array
const dp = Array(capacity + 1).fill(false);
// Base case: empty subset sums to 0
dp[0] = true;
// Fill the DP array
for (let i = 0; i < gearWeights.length; i++) {
for (let j = capacity; j >= gearWeights[i]; j--) {
// Either exclude or include the current item
dp[j] = dp[j] || dp[j - gearWeights[i]];
}
}
return dp[capacity];
}
// Test cases
const gearWeights1 = [2, 3, 7, 8, 10];
const capacity1 = 11;
console.log(`Gear weights: [${gearWeights1}], Capacity: ${capacity1}`);
console.log(`Recursive Approach: ${canPackExactly(gearWeights1, capacity1)}`);
console.log(`DP Approach: ${canPackExactlyDP(gearWeights1, capacity1)}`);
console.log(`Optimized DP Approach: ${canPackExactlyOptimized(gearWeights1, capacity1)}`);
const gearWeights2 = [1, 3, 5];
const capacity2 = 10;
console.log(`\nGear weights: [${gearWeights2}], Capacity: ${capacity2}`);
console.log(`Recursive Approach: ${canPackExactly(gearWeights2, capacity2)}`);
console.log(`DP Approach: ${canPackExactlyDP(gearWeights2, capacity2)}`);
console.log(`Optimized DP Approach: ${canPackExactlyOptimized(gearWeights2, capacity2)}`);
const gearWeights3 = [4, 2, 3, 5, 6];
const capacity3 = 9;
console.log(`\nGear weights: [${gearWeights3}], Capacity: ${capacity3}`);
console.log(`Recursive Approach: ${canPackExactly(gearWeights3, capacity3)}`);
console.log(`DP Approach: ${canPackExactlyDP(gearWeights3, capacity3)}`);
console.log(`Optimized DP Approach: ${canPackExactlyOptimized(gearWeights3, capacity3)}`);

Step-by-Step Explanation

Let's break down the implementation:

  1. Understand the Problem: We need to determine if there's a subset of gear weights that sum exactly to the backpack's capacity.
  2. Identify the Recursive Structure: Recognize that this is a classic Selection Pattern problem where we recursively decide whether to include or exclude each item.
  3. Define the Base Cases: The base cases are when the remaining capacity is 0 (success), or when it's negative or we've processed all items (failure).
  4. Implement the Recursive Solution: Write a function that handles the base cases and recursively explores both options (include/exclude) for each item.
  5. Optimize with Dynamic Programming: Implement a dynamic programming solution to avoid redundant calculations and improve efficiency.
  6. Further Optimize Space Complexity: Implement a space-optimized version of the dynamic programming solution using a 1D array instead of a 2D table.
ProblemSolutionCode
101 Logo
onenoughtone