https://leetcode.com/problems/water-and-jug-problem/description/ -- 365

There are two methods to solve this problem : GCD(+ elementary number theory) --> how to get GCF, HCD,  BFS

Currently, I sove this by first method

1. how to compute GCD recursively

//get the GCD of two number s
int GCD(int a, int b){
if(a == 0) return b;
if(b == 0) return a;
return GCD(b,a%b);
}

12, 8  -> 8,4 -> 4, 4 -> 4, 0

math solution

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 a or b is negative this means we are emptying a jug of x or y gallons respectively.

Similarly if a or b is positive this means we are filling a jug of x or y gallons respectively.

x = 4, y = 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)

code:

class Solution {
public boolean canMeasureWater(int x, int y, int z) {
//check the limitiation which x + y < z such as 3,4 , 8: notmeeting the requirement
if(x+ y < z) return false;
//check all 0
System.out.println(GCD(x,y));
//there is a theory about that
//ax + by = gcd z%gcd == 0 Bézout's identity
if(GCD(x,y) == 0) return z==0;
else return (z%GCD(x,y)==0);
} //get the GCD of two number s
int GCD(int a, int b){
if(a == 0) return b;
if(b == 0) return a;
return GCD(b,a%b);
} }

--------------------------------------------------------------------------------------------------------------------------------

BFS method

365. Water and Jug Problem (GCD or BFS) TBC的更多相关文章

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

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

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

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

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

  4. Leetcode 365. Water and Jug Problem

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

  5. 365. Water and Jug Problem

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

  6. 365 Water and Jug Problem 水壶问题

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

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

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

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

随机推荐

  1. CentOS快速搭建FTP(初级-四步)

    部署FTP,如果之前没有搭建过,刚开始找资料的时候网上各种各样的复杂参数配置,看的头晕,这里就把最核心的部分展示出来. 1.安装 vsftpd yum install -y vsftpd 2.如果是默 ...

  2. jsonp跨域请求及本质

    在html页面中,能实现跨域请求的是 第一: <script src="http://localhost:59602/JsonpTest.ashx?callBack=callBack& ...

  3. 省市联动 js

    工作中见到这个省市联动代码,虽然很简单也能写出来,还是随便把它记录下来. //省市联动 function area(obj_id, area_pId, data_call_back) { ) retu ...

  4. 事务背书 ACID, CAP, BASE

    A atomicity 原子性 一个事务(transaction)中的所有操作,要么全部完成,要么全部不完成, C consistency 一致性 在事务开始之前和事务结束以后,数据库的完整性没有被破 ...

  5. Apache重定向URL

    (1)去除httpd.conf文件中"#LoadModule rewrite_module modules/mod_rewrite.so"前面的"#"号; (2 ...

  6. Flex布局教程

    一.Flex布局是什么? Flex 是 Flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性.任何一个容器都可以指定为 Flex 布局. .box{ ...

  7. 转 ogg issue

    1.http://www.dbdream.com.cn/2013/05/17/ogg-00446%E9%94%99%E8%AF%AF%E8%A7%A3%E5%86%B3/ OGG-00446错误解决 ...

  8. java生成复杂word文档

    在Web应用中,有时需要按照固定的模板将数据导出到Word,如流程审批单,在流程处理完成后将处理过程按照流程单的要求导出,有时程序中需要实现生成 标准Word文档,要求能够打印,并且保持页面样式不变, ...

  9. JS Date 时间格式化

    Date2Str(x, y) { , d: x.getDate(), h: x.getHours(), m: x.getMinutes(), s: x.getSeconds() }; y = y.re ...

  10. 获取tomcat路径

    String serverPath = System.getProperty("catalina.home");