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. linux中文件压缩与打包

    一.常见的压缩命令 在linux环境中,压缩文件的扩展名大多是*.tar,*.tar.gz,*.tgz,*.gz,*.Z,*.bz2,首先我们来介绍以下这些压缩文案的扩展名:. *.Z:compres ...

  2. iOS自动化测试的那些干货

    前言 如果有测试大佬发现内容不对,欢迎指正,我会及时修改. 大多数的iOS App(没有持续集成)迭代流程是这样的 也就是说,测试是发布之前的最后一道关卡.如果bug不能在测试中发现,那么bug就会抵 ...

  3. 匿名类与lambda区别

    第一种是继承Thread, 重写了Thread.run()    getClass()返回的是匿名类 java.long.Thread$1 第二种是lambda, 重写了Runnable.run()  ...

  4. java编程--01介绍日期的比较

    引子:平时开发常常需要对时间进行格式化,进行比较,进行加减计算.最常用的类不外乎:SimpleDateFormat,Calendar,Date,DateTimeStamp等.下面想对java中的日期编 ...

  5. jquery 写ajax

    function down(t){ $.ajax({  url : 'selectWordDate',  data : {   date_time : t  },  dataType : 'json' ...

  6. Collections练习之对字符串先折半,再取最长的一个

    不多说,直接上干货! 代码需求 由 [aa, abcde, cba, cba, nbaa, zzz] 变成 max=abcde CollectionsDemo.java package zhouls. ...

  7. AngularJS directive 不执行

    检查下directive的命名,是不是含有特殊符号和大写,全部改为小写就ok: 原因:html不支持骆驼峰命名,只支持小写:

  8. Unity Shader 学习笔记(一)

    _MainTex_ST (1)简单来说,TRANSFORM_TEX(tex,name) (tex.xy * name##_ST.xy + name##_ST.zw)主要作用是拿顶点的uv去和材质球的t ...

  9. CentOS安装最新Git

    1.依赖库安装 yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel yum install gcc pe ...

  10. DBCP数据库连接池原理分析

    在比较大的项目中,需要不断的从数据库中获取数据,Java中则使用JDBC连接数据库,但是获取数据库的连接可是相当耗时的操作,每次连接数据库都获得 .销毁数据库连接,将是很大的一个开销.为了解决这种开销 ...