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=d

In 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)的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 【LeetCode】365. Water and Jug Problem 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学题 相似题目 参考资料 日期 题目地址:http ...

  4. 【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 ...

  5. Leetcode 365. Water and Jug Problem

    可以想象有一个无限大的水罐,如果我们有两个杯子x和y,那么原来的问题等价于是否可以通过往里面注入或倒出水从而剩下z. z =? m*x + n*y 如果等式成立,那么z%gcd(x,y) == 0. ...

  6. [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 ...

  7. 365. Water and Jug Problem量杯灌水问题

    [抄题]: 简而言之:只能对 杯子中全部的水/容量-杯子中全部的水进行操作 You are given two jugs with capacities x and y litres. There i ...

  8. 365 Water and Jug Problem 水壶问题

    有两个容量分别为 x升 和 y升 的水壶以及无限多的水.请判断能否通过使用这两个水壶,从而可以得到恰好 z升 的水?如果可以,最后请用以上水壶中的一或两个来盛放取得的 z升 水.你允许:    装满任 ...

  9. 365. Water and Jug Problem

    莫名奇妙找了个奇怪的规律. 每次用大的减小的,然后差值和小的再减,减减减减减减到差值=0为止.(较小的数 和 差值 相等为止,这么说更确切) 然后看能不能整除就行了. 有些特殊情况. 看答案是用GCD ...

随机推荐

  1. 初步理解Java的三大特性——封装、继承和多态

    声明:整理自网络,如有雷同,请联系博主处理 一.封装 封装从字面上来理解就是包装的意思,专业点就是信息隐藏,是指利用抽象数据类型将数据和基于数据的操作封装在一起,使其构成一个不可分割的独立实体,数据被 ...

  2. 【转】下载量最高的 100 个 Laravel 扩展包推荐

    说明 Laravel 另一个令人喜欢的地方,是拥有活跃的开发者社区,而活跃的开发者社区带来的,是繁华的扩展包生态. 本文对 Packagist 上打了 Laravel 标签 的扩展包进行整理,截止到现 ...

  3. 面向对象之abstract

    1.abstract class,抽象类不能被实例化,只能被继承:抽象类中可以包含非抽象方法 2.abstract method();抽象方法只能在抽象类中进行声明,并且没有方法体,非抽象继承子类中必 ...

  4. docker debug diagnose

    $ sudo systemctl stop docker $ sudo docker -d -D DEBU[0282] Error contacting registry https://regist ...

  5. ArcGIS API for Silverlight 调用GP服务准备---GP模型建立、发布、测试

    原文:ArcGIS API for Silverlight 调用GP服务准备---GP模型建立.发布.测试 第一篇.GP降雨量等值线建模.发布及测试 在水利.气象等行业中,要在WebGIS中实现空间分 ...

  6. C# serialport

    最近使用C#写串口的程序,实现串口助手的功能,参考文档记录于此. 参考文档 http://blog.csdn.net/geekwangminli/article/details/7851673 htt ...

  7. Task+http请求

    Task+http请求  这个算是一个简单的事例吧

  8. DBCC TRACEON/TRACEOFF/TRACESTATUS

    1. enable trace DBCC TRACEON ( trace# [ ,...n ][ , -1 ] ) [ WITH NO_INFOMSGS ] trace# Is the number ...

  9. 追加文件内容java

    1.向空文件文件中追加内容(如果原来有内容,则覆盖) FileWriter writer; try { writer = new FileWriter(listFile);//创建字符输出流类对象和已 ...

  10. Android 在Windows上安装FFmpeg程序

    FFmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.它提供了录制.转换以及流化音视频的完整解决方案.它包含了非常先进的音频/视频编解码库libavcodec. 该程序 ...