LeetCode-Water and Jug Problem
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的更多相关文章
- [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 ...
- 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 ...
- 【LeetCode】365. Water and Jug Problem 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学题 相似题目 参考资料 日期 题目地址:http ...
- 【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 ...
- Leetcode 365. Water and Jug Problem
可以想象有一个无限大的水罐,如果我们有两个杯子x和y,那么原来的问题等价于是否可以通过往里面注入或倒出水从而剩下z. z =? m*x + n*y 如果等式成立,那么z%gcd(x,y) == 0. ...
- [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 ...
- 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 ...
- 365. Water and Jug Problem量杯灌水问题
[抄题]: 简而言之:只能对 杯子中全部的水/容量-杯子中全部的水进行操作 You are given two jugs with capacities x and y litres. There i ...
- 365 Water and Jug Problem 水壶问题
有两个容量分别为 x升 和 y升 的水壶以及无限多的水.请判断能否通过使用这两个水壶,从而可以得到恰好 z升 的水?如果可以,最后请用以上水壶中的一或两个来盛放取得的 z升 水.你允许: 装满任 ...
- 365. Water and Jug Problem
莫名奇妙找了个奇怪的规律. 每次用大的减小的,然后差值和小的再减,减减减减减减到差值=0为止.(较小的数 和 差值 相等为止,这么说更确切) 然后看能不能整除就行了. 有些特殊情况. 看答案是用GCD ...
随机推荐
- django abstract base class ---- 抽象基类
抽象蕨类用于定义一些同享的列.类本身并不会在数据库端有表与之对应 一.例子: 1.定义一个叫Person 的抽象基类.Student 继承自Person from django.db import m ...
- Nfs实现linux下文件共享
前提条件 130是第一台机器,131为第二台机器.都为redhat linux64位系统. 1. 共享要求: 130server上生成的清算文件在131文件夹下能够看得到,131文件夹下生成 ...
- libjpeg.a exists or that its path is correct
Android NDK: ERROR:/cygdrive/e/cocos2d-x/code/cocos2d-2.1rc0-x-2.1.3/HelloTest1/proj.android/../../c ...
- 每日英语:The First Day On A Job Is Tough Work
Why is the first day on the job often the worst? New employees tend to be greeted with stacks of ben ...
- json中把非json格式的字符串转换成json对象再转换成json字符串
JSON.toJson(str).toString()假如key和value都是整数的时候,先转换成jsonObject对象,再转换成json字符串
- 基于jQuery页面窗口拖动预览效果
今天给大家分享一款基于Query页面窗口拖动预览效果.这是一款基于jQuery+HTML5实现的模拟页面窗口显示拖动窗口预览特效.这款实例适用浏览器:IE8.360.FireFox.Chrome.Sa ...
- C - The C Answer (2nd Edition) - Exercise 1-2
/* Experiment to find out what happens when printf's argument string contains \c, where c is some ch ...
- python手册
https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/
- [工具使用] 如何访问github
1.ping github.com,记录github的ip:192.30.252.129 2.找到系统的 hosts文件位置: C:\Windows\System32\drivers\etc\host ...
- scanner, BufferedReader, InputStreamReader 区别及特殊字符的输入
1. Scanner是一个可以使用正则表达式来分析基本类型和字符串的简单文本扫描器!也就是控制台应用程序最为常用的文本输入方式!Scanner取得输入数据的依据是空格符:如按下空格键,Tab键或者En ...