|
1 |
| -function canMeasureWater(jug1Capacity, jug2Capacity, targetAmount) { |
2 |
| - // Base case: If the target amount is greater than the sum of both jugs, it's not possible. |
3 |
| - if (targetAmount > jug1Capacity + jug2Capacity) return false; |
| 1 | +// WaterJugProblem.js |
4 | 2 |
|
5 |
| - // Use BFS to explore possible states. |
6 |
| - let visited = new Set(); // To keep track of visited states. |
7 |
| - let queue = [[0, 0]]; // Starting with both jugs empty. |
| 3 | +/** |
| 4 | + * Function to determine if it is possible to measure exactly targetAmount |
| 5 | + * using two jugs of capacity jug1Capacity and jug2Capacity. |
| 6 | + * |
| 7 | + * @param {number} jug1Capacity - Capacity of the first jug |
| 8 | + * @param {number} jug2Capacity - Capacity of the second jug |
| 9 | + * @param {number} targetAmount - Target amount of water |
| 10 | + * @returns {boolean} |
| 11 | + */ |
| 12 | +export function canMeasureWater(jug1Capacity, jug2Capacity, targetAmount) { |
| 13 | + // Base case: If the target amount is greater than the sum of both jugs, it's not possible. |
| 14 | + if (targetAmount > jug1Capacity + jug2Capacity) return false; |
8 | 15 |
|
9 |
| - while (queue.length > 0) { |
10 |
| - let [jug1, jug2] = queue.shift(); |
| 16 | + // Use BFS to explore possible states. |
| 17 | + let visited = new Set(); // To keep track of visited states. |
| 18 | + let queue = [[0, 0]]; // Starting with both jugs empty. |
11 | 19 |
|
12 |
| - // If we've reached the target amount in either jug. |
13 |
| - if ( |
14 |
| - jug1 === targetAmount || |
15 |
| - jug2 === targetAmount || |
16 |
| - jug1 + jug2 === targetAmount |
17 |
| - ) { |
18 |
| - return true; |
19 |
| - } |
| 20 | + while (queue.length > 0) { |
| 21 | + let [jug1, jug2] = queue.shift(); |
| 22 | + |
| 23 | + // If we've reached the target amount in either jug. |
| 24 | + if (jug1 === targetAmount || jug2 === targetAmount || jug1 + jug2 === targetAmount) { |
| 25 | + return true; |
| 26 | + } |
20 | 27 |
|
21 |
| - // If this state has been visited, skip it. |
22 |
| - let state = `${jug1},${jug2}`; |
23 |
| - if (visited.has(state)) continue; |
24 |
| - visited.add(state); |
| 28 | + // If this state has been visited, skip it. |
| 29 | + let state = `${jug1},${jug2}`; |
| 30 | + if (visited.has(state)) continue; |
| 31 | + visited.add(state); |
25 | 32 |
|
26 |
| - // Add all possible next states to the queue: |
27 |
| - queue.push([jug1Capacity, jug2]); // Fill Jug 1 |
28 |
| - queue.push([jug1, jug2Capacity]); // Fill Jug 2 |
29 |
| - queue.push([0, jug2]); // Empty Jug 1 |
30 |
| - queue.push([jug1, 0]); // Empty Jug 2 |
31 |
| - queue.push([ |
32 |
| - Math.max(0, jug1 - (jug2Capacity - jug2)), |
33 |
| - Math.min(jug2Capacity, jug1 + jug2), |
34 |
| - ]); // Pour Jug 1 into Jug 2 |
35 |
| - queue.push([ |
36 |
| - Math.min(jug1Capacity, jug1 + jug2), |
37 |
| - Math.max(0, jug2 - (jug1Capacity - jug1)), |
38 |
| - ]); // Pour Jug 2 into Jug 1 |
39 |
| - } |
| 33 | + // Add all possible next states to the queue: |
| 34 | + queue.push([jug1Capacity, jug2]); // Fill Jug 1 |
| 35 | + queue.push([jug1, jug2Capacity]); // Fill Jug 2 |
| 36 | + queue.push([0, jug2]); // Empty Jug 1 |
| 37 | + queue.push([jug1, 0]); // Empty Jug 2 |
| 38 | + queue.push([Math.max(0, jug1 - (jug2Capacity - jug2)), Math.min(jug2Capacity, jug1 + jug2)]); // Pour Jug 1 into Jug 2 |
| 39 | + queue.push([Math.min(jug1Capacity, jug1 + jug2), Math.max(0, jug2 - (jug1Capacity - jug1))]); // Pour Jug 2 into Jug 1 |
| 40 | + } |
40 | 41 |
|
41 |
| - // If no solution is found |
42 |
| - return false; |
| 42 | + // If no solution is found |
| 43 | + return false; |
43 | 44 | }
|
0 commit comments