Skip to content

Commit 3ab1fcb

Browse files
authored
Update WaterJugProblem.js
prettier
1 parent 74a65b7 commit 3ab1fcb

File tree

1 file changed

+47
-37
lines changed

1 file changed

+47
-37
lines changed

Recursive/WaterJugProblem.js

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,53 @@
11
// WaterJugProblem.js
22

33
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;
3929
}
4030

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;
4353
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy