Leetcode: Water and Jug Problem && Summary: GCD求法(辗转相除法 or Euclidean algorithm)
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply available. You need to determine whether it is possible to measure exactly z litres using these two jugs. If z liters of water is measurable, you must have z liters of water contained within one or both buckets by the end. Operations allowed: Fill any of the jugs completely with water.
Empty any of the jugs.
Pour water from one jug into another till the other jug is completely full or the first jug itself is empty.
Example 1: (From the famous "Die Hard" example) Input: x = 3, y = 5, z = 4
Output: True
Example 2: Input: x = 2, y = 6, z = 5
Output: False
参考:https://discuss.leetcode.com/topic/49238/math-solution-java-solution
The basic idea is to use the property of Bézout's identity and check if z is a multiple of GCD(x, y)
Quote from wiki:
Bézout's identity (also called Bézout's lemma) is a theorem in the elementary theory of numbers:
let a and b be nonzero integers and let d be their greatest common divisor. Then there exist integers x
and y such that ax+by=dIn addition, the greatest common divisor d is the smallest positive integer that can be written as ax + by
every integer of the form ax + by is a multiple of the greatest common divisor d.
If x or y is negative this means we are emptying a jug of a or b gallons respectively.
Similarly if x or y is positive this means we are filling a jug of a or b gallons respectively.
a = 4, b = 6, z = 8.
GCD(4, 6) = 2
8 is multiple of 2
so this input is valid and we have:
-1 * 4 + 6 * 2 = 8
In this case, there is a solution obtained by filling the 6 gallon jug twice and emptying the 4 gallon jug once. (Solution. Fill the 6 gallon jug and empty 4 gallons to the 4 gallon jug. Empty the 4 gallon jug. Now empty the remaining two gallons from the 6 gallon jug to the 4 gallon jug. Next refill the 6 gallon jug. This gives 8 gallons in the end)
public class Solution {
public boolean canMeasureWater(int x, int y, int z) {
if (z==x || z==y) return true; //deal with [1, 0, 0], [0, 0, 0] these true cases
if (z > x + y) return false;
return z % GCD(x, y) == 0;
} public int GCD(int x, int y) {
while (y != 0) {
int temp = y;
y = x%y;
x = temp;
}
return x;
}
}
Leetcode: Water and Jug Problem && Summary: GCD求法(辗转相除法 or Euclidean algorithm)的更多相关文章
- [LeetCode] Water and Jug Problem 水罐问题
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...
- 365. Water and Jug Problem (GCD or BFS) TBC
https://leetcode.com/problems/water-and-jug-problem/description/ -- 365 There are two methods to sol ...
- 【LeetCode】365. Water and Jug Problem 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学题 相似题目 参考资料 日期 题目地址:http ...
- 【leetcode】365. Water and Jug Problem
题目描述: You are given two jugs with capacities x and y litres. There is an infinite amount of water su ...
- Leetcode 365. Water and Jug Problem
可以想象有一个无限大的水罐,如果我们有两个杯子x和y,那么原来的问题等价于是否可以通过往里面注入或倒出水从而剩下z. z =? m*x + n*y 如果等式成立,那么z%gcd(x,y) == 0. ...
- [Swift]LeetCode365. 水壶问题 | Water and Jug Problem
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...
- 365. Water and Jug Problem量杯灌水问题
[抄题]: 简而言之:只能对 杯子中全部的水/容量-杯子中全部的水进行操作 You are given two jugs with capacities x and y litres. There i ...
- 365 Water and Jug Problem 水壶问题
有两个容量分别为 x升 和 y升 的水壶以及无限多的水.请判断能否通过使用这两个水壶,从而可以得到恰好 z升 的水?如果可以,最后请用以上水壶中的一或两个来盛放取得的 z升 水.你允许: 装满任 ...
- 365. Water and Jug Problem
莫名奇妙找了个奇怪的规律. 每次用大的减小的,然后差值和小的再减,减减减减减减到差值=0为止.(较小的数 和 差值 相等为止,这么说更确切) 然后看能不能整除就行了. 有些特殊情况. 看答案是用GCD ...
随机推荐
- OpenMP并行编程
什么是OpenMP?“OpenMP (Open Multi-Processing) is an application programming interface (API) that support ...
- 阻止默认行为stopDefault
function stopDefault(e){ if(e && e.preventDefault) e.preventDefault(); else window.event.ret ...
- UBUNTU 14.04 安装 OPENCV 2.4.9
1. 从OpenCV.org 下载源代码 opencv-2.4.9.zip 2. 解压到准备好的目录 unzip opencv-2.4.9.zip 3. 进入源码目录,创建release目录 cd ...
- CC254x(cc2540/cc2541)的微信AirSync调试笔记
一.前言 本尊自诩为IOT小能手,一直没涉足蓝牙实在说不过去.刚好上个月底的时候计划做个BLE设备,这阵子利用业余时间自学了BLE协议栈,了解了GATT,磕磕绊绊完成CC254x(cc2540/cc2 ...
- hadoop与云技术、云计算混肴澄清
本文引用自:http://www.aboutyun.com/blog-61-248.html 一.初学者问题: 请教个问题在实际的生成环境里面,数据源产生的地方部署Hadoop,还是需要程序把数据给迁 ...
- Qt数据库操作(qt-win-commercial-src-4.3.1,VC6,Oracle,SQL Server)
qt-win-commercial-src-4.3.1.qt-x11-commercial-src-4.3.1Microsoft Visual C++ 6.0.KDevelop 3.5.0Window ...
- ELK-Python(一)
不具有通用性,留作纪念. [root@GXB-CTRLCENTER python]# cat insert_active_user.py #!/usr/bin/env python # -*- cod ...
- ThreadLocal 多线程并发,数据隔离
ThreadLocal: 创建一个线程本地变量. 本质:在ThreadLocal类中有一个Map,用于存储每一个线程的变量的副本. 优点:既实现多线程并发,游兼顾数据的安全性. 区别:Synchro ...
- robot framework安装步骤
1.安装python-2.7.11.amd64.msi 2.在命令行窗口输入python命令验证是否成功 3.安装wxPython2.8-win64-unicode-2.8.12.1-py27.exe ...
- Flask 与 Celery 在 windows 下的集成问题
Flask 与 Celery 在 windows 下的集成问题 所有的 Web 框架内部的视图中不适合执行需要长时间运行的任务,包括 Flask .Django 等.这类型的任务会阻塞 Web 的响应 ...