Skip to content

Commit a99db20

Browse files
authored
feat: add solutions to lc problem: No.0365 (doocs#641)
No.0365.Water and Jug Problem
1 parent 4723a89 commit a99db20

File tree

4 files changed

+207
-4
lines changed

4 files changed

+207
-4
lines changed

solution/0300-0399/0365.Water and Jug Problem/README.md

+69-2
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,82 @@
4242
<!-- 这里可写当前语言的特殊实现逻辑 -->
4343

4444
```python
45-
45+
class Solution:
46+
def canMeasureWater(self, jug1Capacity: int, jug2Capacity: int, targetCapacity: int) -> bool:
47+
stk, seen = [], set()
48+
stk.append([0, 0])
49+
50+
def get_hash(nums):
51+
return nums[0] * 10000006 + nums[1]
52+
53+
while stk:
54+
if get_hash(stk[-1]) in seen:
55+
stk.pop()
56+
continue
57+
seen.add(get_hash(stk[-1]))
58+
cur = stk.pop()
59+
cur1, cur2 = cur[0], cur[1]
60+
if cur1 == targetCapacity or cur2 == targetCapacity or cur1 + cur2 == targetCapacity:
61+
return True
62+
stk.append([jug1Capacity, cur2])
63+
stk.append([0, cur2])
64+
stk.append([cur1, jug2Capacity])
65+
stk.append([cur1, 0])
66+
if cur1 + cur2 > jug1Capacity:
67+
stk.append([jug1Capacity, cur2 - jug1Capacity + cur1])
68+
else:
69+
stk.append([cur1 + cur2, 0])
70+
if cur1 + cur2 > jug2Capacity:
71+
stk.append([cur1 - jug2Capacity + cur2, jug2Capacity])
72+
else:
73+
stk.append([0, cur1 + cur2])
74+
return False
4675
```
4776

4877
### **Java**
4978

5079
<!-- 这里可写当前语言的特殊实现逻辑 -->
5180

5281
```java
53-
82+
class Solution {
83+
public boolean canMeasureWater(int jug1Capacity, int jug2Capacity, int targetCapacity) {
84+
Deque<int[]> stk = new ArrayDeque<>();
85+
stk.add(new int[]{0, 0});
86+
Set<Long> seen = new HashSet<>();
87+
while (!stk.isEmpty()) {
88+
if (seen.contains(hash(stk.peek()))) {
89+
stk.pop();
90+
continue;
91+
}
92+
int[] cur = stk.pop();
93+
seen.add(hash(cur));
94+
int cur1 = cur[0], cur2 = cur[1];
95+
if (cur1 == targetCapacity || cur2 == targetCapacity || cur1 + cur2 == targetCapacity) {
96+
return true;
97+
}
98+
stk.offer(new int[]{jug1Capacity, cur2});
99+
stk.offer(new int[]{0, cur2});
100+
stk.offer(new int[]{cur1, jug1Capacity});
101+
stk.offer(new int[]{cur2, 0});
102+
if (cur1 + cur2 > jug1Capacity) {
103+
stk.offer(new int[]{jug1Capacity, cur2 - jug1Capacity + cur1});
104+
} else {
105+
stk.offer(new int[]{cur1 + cur2, 0});
106+
}
107+
if (cur1 + cur2 > jug2Capacity) {
108+
stk.offer(new int[]{cur1 - jug2Capacity + cur2, jug2Capacity});
109+
} else {
110+
stk.offer(new int[]{0, cur1 + cur2});
111+
}
112+
113+
}
114+
return false;
115+
}
116+
117+
public long hash(int[] nums) {
118+
return nums[0] * 10000006L + nums[1];
119+
}
120+
}
54121
```
55122

56123
### **...**

solution/0300-0399/0365.Water and Jug Problem/README_EN.md

+69-2
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,80 @@
5454
### **Python3**
5555

5656
```python
57-
57+
class Solution:
58+
def canMeasureWater(self, jug1Capacity: int, jug2Capacity: int, targetCapacity: int) -> bool:
59+
stk, seen = [], set()
60+
stk.append([0, 0])
61+
62+
def get_hash(nums):
63+
return nums[0] * 10000006 + nums[1]
64+
65+
while stk:
66+
if get_hash(stk[-1]) in seen:
67+
stk.pop()
68+
continue
69+
seen.add(get_hash(stk[-1]))
70+
cur = stk.pop()
71+
cur1, cur2 = cur[0], cur[1]
72+
if cur1 == targetCapacity or cur2 == targetCapacity or cur1 + cur2 == targetCapacity:
73+
return True
74+
stk.append([jug1Capacity, cur2])
75+
stk.append([0, cur2])
76+
stk.append([cur1, jug2Capacity])
77+
stk.append([cur1, 0])
78+
if cur1 + cur2 > jug1Capacity:
79+
stk.append([jug1Capacity, cur2 - jug1Capacity + cur1])
80+
else:
81+
stk.append([cur1 + cur2, 0])
82+
if cur1 + cur2 > jug2Capacity:
83+
stk.append([cur1 - jug2Capacity + cur2, jug2Capacity])
84+
else:
85+
stk.append([0, cur1 + cur2])
86+
return False
5887
```
5988

6089
### **Java**
6190

6291
```java
63-
92+
class Solution {
93+
public boolean canMeasureWater(int jug1Capacity, int jug2Capacity, int targetCapacity) {
94+
Deque<int[]> stk = new ArrayDeque<>();
95+
stk.add(new int[]{0, 0});
96+
Set<Long> seen = new HashSet<>();
97+
while (!stk.isEmpty()) {
98+
if (seen.contains(hash(stk.peek()))) {
99+
stk.pop();
100+
continue;
101+
}
102+
int[] cur = stk.pop();
103+
seen.add(hash(cur));
104+
int cur1 = cur[0], cur2 = cur[1];
105+
if (cur1 == targetCapacity || cur2 == targetCapacity || cur1 + cur2 == targetCapacity) {
106+
return true;
107+
}
108+
stk.offer(new int[]{jug1Capacity, cur2});
109+
stk.offer(new int[]{0, cur2});
110+
stk.offer(new int[]{cur1, jug1Capacity});
111+
stk.offer(new int[]{cur2, 0});
112+
if (cur1 + cur2 > jug1Capacity) {
113+
stk.offer(new int[]{jug1Capacity, cur2 - jug1Capacity + cur1});
114+
} else {
115+
stk.offer(new int[]{cur1 + cur2, 0});
116+
}
117+
if (cur1 + cur2 > jug2Capacity) {
118+
stk.offer(new int[]{cur1 - jug2Capacity + cur2, jug2Capacity});
119+
} else {
120+
stk.offer(new int[]{0, cur1 + cur2});
121+
}
122+
123+
}
124+
return false;
125+
}
126+
127+
public long hash(int[] nums) {
128+
return nums[0] * 10000006L + nums[1];
129+
}
130+
}
64131
```
65132

66133
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
public boolean canMeasureWater(int jug1Capacity, int jug2Capacity, int targetCapacity) {
3+
Deque<int[]> stk = new ArrayDeque<>();
4+
stk.add(new int[]{0, 0});
5+
Set<Long> seen = new HashSet<>();
6+
while (!stk.isEmpty()) {
7+
if (seen.contains(hash(stk.peek()))) {
8+
stk.pop();
9+
continue;
10+
}
11+
int[] cur = stk.pop();
12+
seen.add(hash(cur));
13+
int cur1 = cur[0], cur2 = cur[1];
14+
if (cur1 == targetCapacity || cur2 == targetCapacity || cur1 + cur2 == targetCapacity) {
15+
return true;
16+
}
17+
stk.offer(new int[]{jug1Capacity, cur2});
18+
stk.offer(new int[]{0, cur2});
19+
stk.offer(new int[]{cur1, jug1Capacity});
20+
stk.offer(new int[]{cur2, 0});
21+
if (cur1 + cur2 > jug1Capacity) {
22+
stk.offer(new int[]{jug1Capacity, cur2 - jug1Capacity + cur1});
23+
} else {
24+
stk.offer(new int[]{cur1 + cur2, 0});
25+
}
26+
if (cur1 + cur2 > jug2Capacity) {
27+
stk.offer(new int[]{cur1 - jug2Capacity + cur2, jug2Capacity});
28+
} else {
29+
stk.offer(new int[]{0, cur1 + cur2});
30+
}
31+
32+
}
33+
return false;
34+
}
35+
36+
public long hash(int[] nums) {
37+
return nums[0] * 10000006L + nums[1];
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution:
2+
def canMeasureWater(self, jug1Capacity: int, jug2Capacity: int, targetCapacity: int) -> bool:
3+
stk, seen = [], set()
4+
stk.append([0, 0])
5+
6+
def get_hash(nums):
7+
return nums[0] * 10000006 + nums[1]
8+
9+
while stk:
10+
if get_hash(stk[-1]) in seen:
11+
stk.pop()
12+
continue
13+
seen.add(get_hash(stk[-1]))
14+
cur = stk.pop()
15+
cur1, cur2 = cur[0], cur[1]
16+
if cur1 == targetCapacity or cur2 == targetCapacity or cur1 + cur2 == targetCapacity:
17+
return True
18+
stk.append([jug1Capacity, cur2])
19+
stk.append([0, cur2])
20+
stk.append([cur1, jug2Capacity])
21+
stk.append([cur1, 0])
22+
if cur1 + cur2 > jug1Capacity:
23+
stk.append([jug1Capacity, cur2 - jug1Capacity + cur1])
24+
else:
25+
stk.append([cur1 + cur2, 0])
26+
if cur1 + cur2 > jug2Capacity:
27+
stk.append([cur1 - jug2Capacity + cur2, jug2Capacity])
28+
else:
29+
stk.append([0, cur1 + cur2])
30+
return False

0 commit comments

Comments
 (0)