Skip to content

Commit ff16d60

Browse files
authored
Update Water_Jug_Problem.js
formated the code with prettier
1 parent 844c127 commit ff16d60

File tree

1 file changed

+35
-26
lines changed

1 file changed

+35
-26
lines changed

Diff for: Recursive/Water_Jug_Problem.js

+35-26
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,43 @@
11
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;
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;
44

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.
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.
88

9-
while (queue.length > 0) {
10-
let [jug1, jug2] = queue.shift();
9+
while (queue.length > 0) {
10+
let [jug1, jug2] = queue.shift();
1111

12-
// If we've reached the target amount in either jug.
13-
if (jug1 === targetAmount || jug2 === targetAmount || jug1 + jug2 === targetAmount) {
14-
return true;
15-
}
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+
}
1620

17-
// If this state has been visited, skip it.
18-
let state = `${jug1},${jug2}`;
19-
if (visited.has(state)) continue;
20-
visited.add(state);
21+
// If this state has been visited, skip it.
22+
let state = `${jug1},${jug2}`;
23+
if (visited.has(state)) continue;
24+
visited.add(state);
2125

22-
// Add all possible next states to the queue:
23-
queue.push([jug1Capacity, jug2]); // Fill Jug 1
24-
queue.push([jug1, jug2Capacity]); // Fill Jug 2
25-
queue.push([0, jug2]); // Empty Jug 1
26-
queue.push([jug1, 0]); // Empty Jug 2
27-
queue.push([Math.max(0, jug1 - (jug2Capacity - jug2)), Math.min(jug2Capacity, jug1 + jug2)]); // Pour Jug 1 into Jug 2
28-
queue.push([Math.min(jug1Capacity, jug1 + jug2), Math.max(0, jug2 - (jug1Capacity - jug1))]); // Pour Jug 2 into Jug 1
29-
}
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+
}
3040

31-
// If no solution is found
32-
return false;
41+
// If no solution is found
42+
return false;
3343
}
34-

0 commit comments

Comments
 (0)