-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWaterAndJugProblem.java
More file actions
53 lines (43 loc) · 1.79 KB
/
WaterAndJugProblem.java
File metadata and controls
53 lines (43 loc) · 1.79 KB
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
package graph;
import java.util.*;
/**
* Description: https://leetcode.com/problems/water-and-jug-problem
* Difficulty: Medium
* Time complexity: O(x * y)
* Space complexity: O(x * y)
*/
public class WaterAndJugProblem {
public boolean canMeasureWater(int first, int second, int target) {
if (target > first + second) return false;
State start = new State(0, 0); // both jugs are empty
Set<State> visited = new HashSet<>();
visited.add(start);
Queue<State> planned = new LinkedList<>();
planned.offer(start);
while (!planned.isEmpty()) {
State current = planned.poll();
if (current.first + current.second == target) return true;
for (State neighbor : findNeighbors(current, first, second)) {
if (visited.add(neighbor)) {
planned.offer(neighbor);
}
}
}
return false;
}
private List<State> findNeighbors(State current, int small, int large) {
return List.of(
new State(small, current.second), // fill first
new State(current.first, small), // fill second
new State(0, current.second), // empty first
new State(current.first, 0), // empty second
new State( // first -> second
current.first - Math.min(current.first, large - current.second),
current.second + Math.min(current.first, large - current.second)),
new State( // second -> first
current.first + Math.min(current.second, small - current.first),
current.second - Math.min(current.second, small - current.first)));
}
private record State(int first, int second) {
}
}