Skip to content

Commit dfe60bf

Browse files
authored
feat: 更新多种解法和多种语言
1 parent 8b62b78 commit dfe60bf

File tree

1 file changed

+131
-50
lines changed

1 file changed

+131
-50
lines changed

problems/365.water-and-jug-problem.md

+131-50
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,62 @@ Output: False
2525
2626
```
2727

28-
## 思路
28+
29+
## BFS(超时)
30+
31+
### 思路
32+
33+
两个水壶的水我们考虑成状态,然后我们不断进行倒的操作,改变状态。那么初始状态就是(0 0) 目标状态就是 (any, z)或者 (z, any),其中any 指的是任意升水。
34+
35+
36+
已题目的例子,其过程示意图,其中括号表示其是由哪个状态转移过来的:
37+
38+
0 0
39+
3 5(0 0) 3 0 (0 0 )0 5(0 0)
40+
3 2(0 5) 0 3(0 0)
41+
0 2(3 2)
42+
2 0(0 2)
43+
2 5(2 0)
44+
3 4(2 5) bingo
45+
46+
### 代码
47+
48+
```python
49+
class Solution:
50+
def canMeasureWater(self, x: int, y: int, z: int) -> bool:
51+
if x + y < z:
52+
return False
53+
queue = [(0, 0)]
54+
seen = set((0, 0))
55+
56+
while(len(queue) > 0):
57+
a, b = queue.pop(0)
58+
if a ==z or b == z or a + b == z:
59+
return True
60+
states = set()
61+
62+
states.add((x, b))
63+
states.add((a, y))
64+
states.add((0, b))
65+
states.add((a, 0))
66+
states.add((min(x, b + a), 0 if b < x - a else b - (x - a)))
67+
states.add((0 if a + b < y else a - (y - b), min(b + a, y)))
68+
for state in states:
69+
if state in seen:
70+
continue;
71+
queue.append(state)
72+
seen.add(state)
73+
return False
74+
```
75+
76+
**复杂度分析**
77+
78+
- 时间复杂度:不太好算,大概是指数级别
79+
- 空间复杂度:不太好算,大概是指数级别
80+
81+
## 数学法 - 最大公约数
82+
83+
### 思路
2984

3085
这是一道关于`数论`的题目,确切地说是关于`裴蜀定理`(英语:Bézout's identity)的题目。
3186

@@ -40,60 +95,61 @@ ax+by=m
4095
4196
```
4297

43-
因此这道题可以完全转化为`裴蜀定理`
98+
因此这道题可以完全转化为`裴蜀定理`。还是以题目给的例子`x = 3, y = 5, z = 4`,我们其实可以表示成`3 * 3 - 1 * 5 = 4`, 即`3 * x - 1 * y = z`。我们用a和b分别表示3
99+
升的水壶和5升的水壶。那么我们可以:
44100

45-
## 关键点解析
46101

47-
- 数论
48-
- 裴蜀定理
102+
- 倒满a(**1**
103+
- 将a倒到b
104+
- 再次倒满a(**2**
105+
- 再次将a倒到b(a这个时候还剩下1升)
106+
- 倒空b(**-1**
107+
- 将剩下的1升倒到b
108+
- 将a倒满(**3**
109+
- 将a倒到b
110+
- b此时正好是4升
49111

50-
## 代码
51-
```js
112+
上面的过程就是`3 * x - 1 * y = z`的具体过程解释。
52113

114+
**也就是说我们只需要求出x和y的最大公约数d,并判断z是否是d的整数倍即可。**
53115

54-
/*
55-
* @lc app=leetcode id=365 lang=javascript
56-
*
57-
* [365] Water and Jug Problem
58-
*
59-
* https://leetcode.com/problems/water-and-jug-problem/description/
60-
*
61-
* algorithms
62-
* Medium (28.76%)
63-
* Total Accepted: 27K
64-
* Total Submissions: 93.7K
65-
* Testcase Example: '3\n5\n4'
66-
*
67-
* You are given two jugs with capacities x and y litres. There is an infinite
68-
* amount of water supply available. You need to determine whether it is
69-
* possible to measure exactly z litres using these two jugs.
70-
*
71-
* If z liters of water is measurable, you must have z liters of water
72-
* contained within one or both buckets by the end.
73-
*
74-
* Operations allowed:
75-
*
76-
*
77-
* Fill any of the jugs completely with water.
78-
* Empty any of the jugs.
79-
* Pour water from one jug into another till the other jug is completely full
80-
* or the first jug itself is empty.
81-
*
82-
*
83-
* Example 1: (From the famous "Die Hard" example)
84-
*
85-
*
86-
* Input: x = 3, y = 5, z = 4
87-
* Output: True
88-
*
89-
*
90-
* Example 2:
91-
*
92-
*
93-
* Input: x = 2, y = 6, z = 5
94-
* Output: False
95-
*
96-
*/
116+
117+
### 代码
118+
119+
代码支持:Python3,JavaScript
120+
121+
122+
Python Code:
123+
124+
```python
125+
class Solution:
126+
def canMeasureWater(self, x: int, y: int, z: int) -> bool:
127+
if x + y < z:
128+
return False
129+
130+
if (z == 0):
131+
return True
132+
133+
if (x == 0):
134+
return y == z
135+
136+
if (y == 0):
137+
return x == z
138+
139+
def GCD(a, b):
140+
smaller = min(a, b)
141+
while smaller:
142+
if a % smaller == 0 and b % smaller == 0:
143+
return smaller
144+
smaller -= 1
145+
146+
return z % GCD(x, y) == 0
147+
```
148+
149+
JavaScript:
150+
151+
152+
```js
97153
/**
98154
* @param {number} x
99155
* @param {number} y
@@ -121,3 +177,28 @@ var canMeasureWater = function(x, y, z) {
121177
return z % GCD(x, y) === 0;
122178
};
123179
```
180+
181+
实际上求最大公约数还有更好的方式,比如辗转相除法:
182+
183+
```python
184+
def GCD(a, b):
185+
if b == 0: return a
186+
return GCD(b, a % b)
187+
```
188+
189+
**复杂度分析**
190+
191+
- 时间复杂度:$O(log(max(a, b)))$
192+
- 空间复杂度:空间复杂度取决于递归的深度,因此空间复杂度为 $O(log(max(a, b)))$
193+
194+
195+
## 关键点分析
196+
197+
- 数论
198+
- 裴蜀定理
199+
200+
更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经接近30K star啦。
201+
202+
大家也可以关注我的公众号《脑洞前端》获取更多更新鲜的LeetCode题解
203+
204+
![](https://pic.leetcode-cn.com/89ef69abbf02a2957838499a96ce3fbb26830aae52e3ab90392e328c2670cddc-file_1581478989502)

0 commit comments

Comments
 (0)