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. python解决excel工作薄合并处理

    年度了,要对每个月的数据进行总的汇总,去计算每消耗品的使用情况,表格都在一个工作表的不同sheet中,并且格式相同,所以就用python写了这个小脚本,现在把脚本粘贴出来,以后有需要就可以在此基础上改 ...

  2. pip 安装库的时候使用豆瓣镜像 提升效率

    由于众所周知的原因,国内网络环境始终处于水深火热之中,python库的安装也不例外. 比如在安装 PyQt5-tools 的时候,网速奇慢无比. 好在国内有不少镜像服务源,以豆瓣为例,网速突飞猛进 使 ...

  3. Turn.js 实现翻书效果

    Turn.js的官方网址: http://www.turnjs.com/ 官网上运行demo如下,大家主要关注是 属性使用: <!DOCTYPE html> <html> &l ...

  4. JVM---概述

    1.JVM架构 1.1 JVM组成: ClassLoader类加载器 : 将class文件加载到JVM内存中: Runtime Data Area运行时数据区域 :  java程序运行时的内存区域: ...

  5. Mybatis学习笔记8 - resultMap自定义结果集映射规则

    一.resultMap自定义结果集映射规则 示例如下: 接口定义: package com.mybatis.dao; import com.mybatis.bean.Employee; public ...

  6. android 闹钟设置问题

    Android开发中,alarmManager在5.0以上系统,启动时间设置无效的问题 做一个app,需要后台保持发送心跳包.由于锁屏后CPU休眠,导致心跳包线程被挂起,所以尝试使用alarmMana ...

  7. [转]JS 只能输入数字和两位小数的JS

    本文转自:http://blog.sina.com.cn/s/blog_724008890101dgep.html JS代码: <script language="JavaScript ...

  8. 使用mspaint查看图片像素

    图片打码服务,要求对图像验证码进行切图,即具体知道像素的位置,网上搜了yhb-chi但下载不方便. 就想着看看mspaint可不可以,将图片拖到mspaint中: 显示了整张图片的像素大小(100X4 ...

  9. POJ 3164——Command Network——————【最小树形图、固定根】

    Command Network Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 15080   Accepted: 4331 ...

  10. MongoDB 搭建Node.js开发环境

    理解Mongoose Elegant MongoDB object modeling for Node.js   安装Mongoose   $ cnpm install --save mongoose ...