Skip to content

Commit 4e895e9

Browse files
committed
feat: add solutions to lc problem: No.0365
No.0365.Water and Jug Problem
1 parent a99db20 commit 4e895e9

File tree

6 files changed

+185
-64
lines changed

6 files changed

+185
-64
lines changed

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

+73-2
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@
3030
输出: False
3131
</pre>
3232

33-
3433
## 解法
3534

3635
<!-- 这里可写通用的实现逻辑 -->
3736

37+
可以认为,每次操作只会让壶里的水带来 x 或者 y 的变化量。因此只要满足 `ax + by = z` 即可。
38+
39+
根据裴蜀定理,`ax + by = z` 有解,当且仅当 z 是 x,y 的最大公约数的倍数。所以我们只要找到 x,y 的最大公约数,然后判断 z 是否是这个最大公约数的倍数即可求得答案。
40+
3841
<!-- tabs:start -->
3942

4043
### **Python3**
@@ -74,6 +77,16 @@ class Solution:
7477
return False
7578
```
7679

80+
```python
81+
class Solution:
82+
def canMeasureWater(self, jug1Capacity: int, jug2Capacity: int, targetCapacity: int) -> bool:
83+
if jug1Capacity + jug2Capacity < targetCapacity:
84+
return False
85+
if jug1Capacity == 0 or jug2Capacity == 0:
86+
return targetCapacity == 0 or jug1Capacity + jug2Capacity == targetCapacity
87+
return targetCapacity % gcd(jug1Capacity, jug2Capacity) == 0
88+
```
89+
7790
### **Java**
7891

7992
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -109,7 +122,7 @@ class Solution {
109122
} else {
110123
stk.offer(new int[]{0, cur1 + cur2});
111124
}
112-
125+
113126
}
114127
return false;
115128
}
@@ -120,6 +133,64 @@ class Solution {
120133
}
121134
```
122135

136+
```java
137+
class Solution {
138+
public boolean canMeasureWater(int jug1Capacity, int jug2Capacity, int targetCapacity) {
139+
if (jug1Capacity + jug2Capacity < targetCapacity) {
140+
return false;
141+
}
142+
if (jug1Capacity == 0 || jug2Capacity == 0) {
143+
return targetCapacity == 0 || jug1Capacity + jug2Capacity == targetCapacity;
144+
}
145+
return targetCapacity % gcd(jug1Capacity, jug2Capacity) == 0;
146+
}
147+
148+
private int gcd(int a, int b) {
149+
return b == 0 ? a : gcd(b, a % b);
150+
}
151+
}
152+
```
153+
154+
### **C++**
155+
156+
```cpp
157+
class Solution {
158+
public:
159+
bool canMeasureWater(int jug1Capacity, int jug2Capacity, int targetCapacity) {
160+
if (jug1Capacity + jug2Capacity < targetCapacity) return false;
161+
if (jug1Capacity == 0 || jug2Capacity == 0)
162+
return targetCapacity == 0 || jug1Capacity + jug2Capacity == targetCapacity;
163+
return targetCapacity % gcd(jug1Capacity, jug2Capacity) == 0;
164+
}
165+
166+
int gcd(int a, int b) {
167+
return b == 0 ? a : gcd(b, a % b);
168+
}
169+
};
170+
```
171+
172+
### **Go**
173+
174+
```go
175+
func canMeasureWater(jug1Capacity int, jug2Capacity int, targetCapacity int) bool {
176+
if jug1Capacity+jug2Capacity < targetCapacity {
177+
return false
178+
}
179+
if jug1Capacity == 0 || jug2Capacity == 0 {
180+
return targetCapacity == 0 || jug1Capacity+jug2Capacity == targetCapacity
181+
}
182+
183+
var gcd func(a, b int) int
184+
gcd = func(a, b int) int {
185+
if b == 0 {
186+
return a
187+
}
188+
return gcd(b, a%b)
189+
}
190+
return targetCapacity%gcd(jug1Capacity, jug2Capacity) == 0
191+
}
192+
```
193+
123194
### **...**
124195

125196
```

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

+69-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
<li><code>1 &lt;= jug1Capacity, jug2Capacity, targetCapacity &lt;= 10<sup>6</sup></code></li>
4747
</ul>
4848

49-
5049
## Solutions
5150

5251
<!-- tabs:start -->
@@ -86,6 +85,16 @@ class Solution:
8685
return False
8786
```
8887

88+
```python
89+
class Solution:
90+
def canMeasureWater(self, jug1Capacity: int, jug2Capacity: int, targetCapacity: int) -> bool:
91+
if jug1Capacity + jug2Capacity < targetCapacity:
92+
return False
93+
if jug1Capacity == 0 or jug2Capacity == 0:
94+
return targetCapacity == 0 or jug1Capacity + jug2Capacity == targetCapacity
95+
return targetCapacity % gcd(jug1Capacity, jug2Capacity) == 0
96+
```
97+
8998
### **Java**
9099

91100
```java
@@ -119,7 +128,7 @@ class Solution {
119128
} else {
120129
stk.offer(new int[]{0, cur1 + cur2});
121130
}
122-
131+
123132
}
124133
return false;
125134
}
@@ -130,6 +139,64 @@ class Solution {
130139
}
131140
```
132141

142+
```java
143+
class Solution {
144+
public boolean canMeasureWater(int jug1Capacity, int jug2Capacity, int targetCapacity) {
145+
if (jug1Capacity + jug2Capacity < targetCapacity) {
146+
return false;
147+
}
148+
if (jug1Capacity == 0 || jug2Capacity == 0) {
149+
return targetCapacity == 0 || jug1Capacity + jug2Capacity == targetCapacity;
150+
}
151+
return targetCapacity % gcd(jug1Capacity, jug2Capacity) == 0;
152+
}
153+
154+
private int gcd(int a, int b) {
155+
return b == 0 ? a : gcd(b, a % b);
156+
}
157+
}
158+
```
159+
160+
### **C++**
161+
162+
```cpp
163+
class Solution {
164+
public:
165+
bool canMeasureWater(int jug1Capacity, int jug2Capacity, int targetCapacity) {
166+
if (jug1Capacity + jug2Capacity < targetCapacity) return false;
167+
if (jug1Capacity == 0 || jug2Capacity == 0)
168+
return targetCapacity == 0 || jug1Capacity + jug2Capacity == targetCapacity;
169+
return targetCapacity % gcd(jug1Capacity, jug2Capacity) == 0;
170+
}
171+
172+
int gcd(int a, int b) {
173+
return b == 0 ? a : gcd(b, a % b);
174+
}
175+
};
176+
```
177+
178+
### **Go**
179+
180+
```go
181+
func canMeasureWater(jug1Capacity int, jug2Capacity int, targetCapacity int) bool {
182+
if jug1Capacity+jug2Capacity < targetCapacity {
183+
return false
184+
}
185+
if jug1Capacity == 0 || jug2Capacity == 0 {
186+
return targetCapacity == 0 || jug1Capacity+jug2Capacity == targetCapacity
187+
}
188+
189+
var gcd func(a, b int) int
190+
gcd = func(a, b int) int {
191+
if b == 0 {
192+
return a
193+
}
194+
return gcd(b, a%b)
195+
}
196+
return targetCapacity%gcd(jug1Capacity, jug2Capacity) == 0
197+
}
198+
```
199+
133200
### **...**
134201

135202
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
bool canMeasureWater(int jug1Capacity, int jug2Capacity, int targetCapacity) {
4+
if (jug1Capacity + jug2Capacity < targetCapacity) return false;
5+
if (jug1Capacity == 0 || jug2Capacity == 0)
6+
return targetCapacity == 0 || jug1Capacity + jug2Capacity == targetCapacity;
7+
return targetCapacity % gcd(jug1Capacity, jug2Capacity) == 0;
8+
}
9+
10+
int gcd(int a, int b) {
11+
return b == 0 ? a : gcd(b, a % b);
12+
}
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func canMeasureWater(jug1Capacity int, jug2Capacity int, targetCapacity int) bool {
2+
if jug1Capacity+jug2Capacity < targetCapacity {
3+
return false
4+
}
5+
if jug1Capacity == 0 || jug2Capacity == 0 {
6+
return targetCapacity == 0 || jug1Capacity+jug2Capacity == targetCapacity
7+
}
8+
9+
var gcd func(a, b int) int
10+
gcd = func(a, b int) int {
11+
if b == 0 {
12+
return a
13+
}
14+
return gcd(b, a%b)
15+
}
16+
return targetCapacity%gcd(jug1Capacity, jug2Capacity) == 0
17+
}
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,15 @@
11
class Solution {
22
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-
3+
if (jug1Capacity + jug2Capacity < targetCapacity) {
4+
return false;
5+
}
6+
if (jug1Capacity == 0 || jug2Capacity == 0) {
7+
return targetCapacity == 0 || jug1Capacity + jug2Capacity == targetCapacity;
328
}
33-
return false;
9+
return targetCapacity % gcd(jug1Capacity, jug2Capacity) == 0;
3410
}
3511

36-
public long hash(int[] nums) {
37-
return nums[0] * 10000006L + nums[1];
12+
private int gcd(int a, int b) {
13+
return b == 0 ? a : gcd(b, a % b);
3814
}
3915
}
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,7 @@
11
class Solution:
22
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
3+
if jug1Capacity + jug2Capacity < targetCapacity:
4+
return False
5+
if jug1Capacity == 0 or jug2Capacity == 0:
6+
return targetCapacity == 0 or jug1Capacity + jug2Capacity == targetCapacity
7+
return targetCapacity % gcd(jug1Capacity, jug2Capacity) == 0

0 commit comments

Comments
 (0)