|
1 | 1 | // WaterJugProblem.js
|
2 | 2 |
|
3 | 3 | export function canMeasureWater(jug1Capacity, jug2Capacity, targetAmount) {
|
4 |
| - // Input validation |
5 |
| - if (jug1Capacity < 0 || jug2Capacity < 0) { |
6 |
| - throw new Error('Invalid input: capacities must be non-negative.'); |
7 |
| - } |
8 |
| - if (targetAmount < 0) { |
9 |
| - throw new Error('Invalid input: target amount must be non-negative.'); |
10 |
| - } |
11 |
| - |
12 |
| - // Base case: If the target amount is greater than the sum of both jugs, it's not possible. |
13 |
| - if (targetAmount > jug1Capacity + jug2Capacity) return false; |
14 |
| - |
15 |
| - // Use BFS to explore possible states. |
16 |
| - let visited = new Set(); // To keep track of visited states. |
17 |
| - let queue = [[0, 0]]; // Starting with both jugs empty. |
18 |
| - |
19 |
| - while (queue.length > 0) { |
20 |
| - let [jug1, jug2] = queue.shift(); |
21 |
| - |
22 |
| - // If we've reached the target amount in either jug. |
23 |
| - if (jug1 === targetAmount || jug2 === targetAmount || jug1 + jug2 === targetAmount) { |
24 |
| - return true; |
25 |
| - } |
26 |
| - |
27 |
| - // If this state has been visited, skip it. |
28 |
| - let state = `${jug1},${jug2}`; |
29 |
| - if (visited.has(state)) continue; |
30 |
| - visited.add(state); |
31 |
| - |
32 |
| - // Add all possible next states to the queue: |
33 |
| - queue.push([jug1Capacity, jug2]); // Fill Jug 1 |
34 |
| - queue.push([jug1, jug2Capacity]); // Fill Jug 2 |
35 |
| - queue.push([0, jug2]); // Empty Jug 1 |
36 |
| - queue.push([jug1, 0]); // Empty Jug 2 |
37 |
| - queue.push([Math.max(0, jug1 - (jug2Capacity - jug2)), Math.min(jug2Capacity, jug1 + jug2)]); // Pour Jug 1 into Jug 2 |
38 |
| - queue.push([Math.min(jug1Capacity, jug1 + jug2), Math.max(0, jug2 - (jug1Capacity - jug1))]); // Pour Jug 2 into Jug 1 |
| 4 | + // Input validation |
| 5 | + if (jug1Capacity < 0 || jug2Capacity < 0) { |
| 6 | + throw new Error("Invalid input: capacities must be non-negative."); |
| 7 | + } |
| 8 | + if (targetAmount < 0) { |
| 9 | + throw new Error("Invalid input: target amount must be non-negative."); |
| 10 | + } |
| 11 | + |
| 12 | + // Base case: If the target amount is greater than the sum of both jugs, it's not possible. |
| 13 | + if (targetAmount > jug1Capacity + jug2Capacity) return false; |
| 14 | + |
| 15 | + // Use BFS to explore possible states. |
| 16 | + let visited = new Set(); // To keep track of visited states. |
| 17 | + let queue = [[0, 0]]; // Starting with both jugs empty. |
| 18 | + |
| 19 | + while (queue.length > 0) { |
| 20 | + let [jug1, jug2] = queue.shift(); |
| 21 | + |
| 22 | + // If we've reached the target amount in either jug. |
| 23 | + if ( |
| 24 | + jug1 === targetAmount || |
| 25 | + jug2 === targetAmount || |
| 26 | + jug1 + jug2 === targetAmount |
| 27 | + ) { |
| 28 | + return true; |
39 | 29 | }
|
40 | 30 |
|
41 |
| - // If no solution is found |
42 |
| - return false; |
| 31 | + // If this state has been visited, skip it. |
| 32 | + let state = `${jug1},${jug2}`; |
| 33 | + if (visited.has(state)) continue; |
| 34 | + visited.add(state); |
| 35 | + |
| 36 | + // Add all possible next states to the queue: |
| 37 | + queue.push([jug1Capacity, jug2]); // Fill Jug 1 |
| 38 | + queue.push([jug1, jug2Capacity]); // Fill Jug 2 |
| 39 | + queue.push([0, jug2]); // Empty Jug 1 |
| 40 | + queue.push([jug1, 0]); // Empty Jug 2 |
| 41 | + queue.push([ |
| 42 | + Math.max(0, jug1 - (jug2Capacity - jug2)), |
| 43 | + Math.min(jug2Capacity, jug1 + jug2), |
| 44 | + ]); // Pour Jug 1 into Jug 2 |
| 45 | + queue.push([ |
| 46 | + Math.min(jug1Capacity, jug1 + jug2), |
| 47 | + Math.max(0, jug2 - (jug1Capacity - jug1)), |
| 48 | + ]); // Pour Jug 2 into Jug 1 |
| 49 | + } |
| 50 | + |
| 51 | + // If no solution is found |
| 52 | + return false; |
43 | 53 | }
|
0 commit comments