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

Credits:
Special thanks to @vinod23 for adding this problem and creating all test cases.

Analysis:

It is a number theory problem.

https://discuss.leetcode.com/topic/49238/math-solution-java-solution/2

Solution:

public class Solution {
public boolean canMeasureWater(int x, int y, int z) {
if (x+y < z) return false; if (x==z || y==z || x+y==z) return true; return z%GCD(x,y)==0; } public int GCD(int x, int y){
while (x%y!=0){
int temp = x;
x = y;
y = temp%y;
}
return y;
}
}

LeetCode-Water and Jug Problem的更多相关文章

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

  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 (GCD or BFS) TBC

    https://leetcode.com/problems/water-and-jug-problem/description/ -- 365 There are two methods to sol ...

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

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

  9. 365 Water and Jug Problem 水壶问题

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

  10. 365. Water and Jug Problem

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

随机推荐

  1. centos-7 charpter one

    一.system locale 的介绍: 系统的区域设置被保存在/etc/locale.conf 这个文件中,systemd在启动的时候会去读取它:所有用户和程序的区域设置都继承自它, 当然用户&am ...

  2. CXSprite.cpp文件

    #include "XSprite.h" CXSprite::CXSprite(void) { m_strPic.clear(); } CXSprite::~CXSprite() ...

  3. Activiti工作流简单入门 (zhuan)

    https://my.oschina.NET/Barudisshu/blog/309721 *********************************************** 摘要: 自j ...

  4. Oracle 11g安装图文攻略

    一.Oracle 下载 注意Oracle分成两个文件,下载完后,将两个文件解压到同一目录下即可. 路径名称中,最好不要出现中文,也不要出现空格等不规则字符. 官方下地址: http://www.ora ...

  5. Netty 源码分析之 番外篇 Java NIO 的前生今世

    简介 Java NIO 是由 Java 1.4 引进的异步 IO. Java NIO 由以下几个核心部分组成: Channel Buffer Selector NIO 和 IO 的对比 IO 和 NI ...

  6. windows测试模式

    测试模式通常意义就是让windows 操作系统在测试状态下运行,windows操作系统在这种模式下可以运行非官方或无数字签名的驱动程序 .   目录 1 定义 2 进入/退出windows测试模式方法 ...

  7. 基于css3的鼠标经过动画显示详情特效

    之前为大家分享过一款基于jquery的手风琴显示详情,今天给大家分享基于css3的鼠标经过动画显示详情特效.这款实例鼠标经过的时候基于中间动画放大,效果非常不错,效果图如下: 在线预览   源码下载 ...

  8. [systemd]Linux系统启动之systemd

    参照:https://wiki.debian.org/systemd 最近在添加板子应用程序自启动的时候,发现在rcN.d中的符号链接并没有用,文件系统为Debian Jessie 8, 后来从同事那 ...

  9. hive表分区的修复

    hive从低版本升级到高版本或者做hadoop的集群数据迁移时,需要重新创建表和表分区,由于使用的是动态分区,所以需要重新刷新分区表字段,否则无法查看数据. 在hive中执行中以下命令即可自动更新元数 ...

  10. 实战c++中的vector系列--知道emplace_back为何优于push_back吗?

    上一篇博客说道vector中放入struct.我们先构造一个struct对象.再push_back. 那段代码中,之所以不能使用emplace_back,就是由于我们定义的struct没有显示的构造函 ...