365. 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
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
ab直接倒入c/a+b=c
[思维问题]:
没啥思路:倒水问题其实就是倍数问题,可以从gcd角度想想。
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- gcd最后b = 0了,因此要求返回a
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
倒水问题是倍数问题,倍数问题想想gcd
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public boolean canMeasureWater(int x, int y, int z) {
//exit case
if (x + y < z) return false;
//corner case
if (x == z || y == z || x + y == z) return true;
//calculate
return z % gcd(x, y) == 0;
}
public int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
}
365. Water and Jug Problem量杯灌水问题的更多相关文章
- 【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 ...
- 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 ...
- 【LeetCode】365. Water and Jug Problem 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学题 相似题目 参考资料 日期 题目地址:http ...
- Leetcode 365. Water and Jug Problem
可以想象有一个无限大的水罐,如果我们有两个杯子x和y,那么原来的问题等价于是否可以通过往里面注入或倒出水从而剩下z. z =? m*x + n*y 如果等式成立,那么z%gcd(x,y) == 0. ...
- 365 Water and Jug Problem 水壶问题
有两个容量分别为 x升 和 y升 的水壶以及无限多的水.请判断能否通过使用这两个水壶,从而可以得到恰好 z升 的水?如果可以,最后请用以上水壶中的一或两个来盛放取得的 z升 水.你允许: 装满任 ...
- 365. Water and Jug Problem
莫名奇妙找了个奇怪的规律. 每次用大的减小的,然后差值和小的再减,减减减减减减到差值=0为止.(较小的数 和 差值 相等为止,这么说更确切) 然后看能不能整除就行了. 有些特殊情况. 看答案是用GCD ...
- [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 ...
- [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 ...
- 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 ...
随机推荐
- 对数据进行GZIP压缩或解压缩
/** * 对data进行GZIP解压缩 * @param data * @return * @throws Exception */ public static String unCompress( ...
- Delphi XE5 Android 调用 Google ZXing
{ Google ZXing Call demo Delphi Version: Delphi XE5 Version 19.0.13476.4176 By: flcop(zylove619@hotm ...
- vs2012 函数参数内存对齐引发编译错误
编译一个游戏库时,遇到个奇怪的问题.一个模板函数,形参是按值传入的.编译时实参是内存对齐过的,无法通过,引发类似下面的错误: error C2719: '_Val': formal parameter ...
- C语言 练习题
subString #include <iostream> int subString(char* sSeek, char* sKey) { char* p = sSeek; while( ...
- POI导入工具类
前言 导入的通用方法,包括xls.xlsx的取值方法,非空判断方法,空行判断,处理了手机号读取和日期读取格式问题.这几个方法就可以完成简单读取了,有时间我在优化下. maven依赖 <!-- P ...
- C语言的基础输入输出
首先来整理一下各个数据类型的输入输出格式: 1.char %c 2.int/short int %d 3.long int %ld 4.long long int %lld 5.float %f 6. ...
- 201772020113 李清华《面向对象程序设计(java)》第三周学习总结
一.测试题反思: 这次的测试题暴露出我在学习上的很多问题:首先,编程能力非常薄弱,编程题目只写出了第一个程序,还因为小问题通不过测试,以后一定要多上手练习,多阅读示例程序.其次,对理论知识的掌握不全面 ...
- centos下安装djangobb
曾经在freenas虚拟环境下安装过djangobb,因为要安装的依赖文件太多,最后没有安装成功. 今晚在centos6.9 下,先创建了虚拟环境,然后照着官方网站的快速安装指南,安装后也运行不了,后 ...
- leetcode169
public class Solution { public int MajorityElement(int[] nums) { Dictionary<int, int> dic = ne ...
- Keil中 Program Size: Code RO-data RW-data ZI-data
一般 MCU 包含的存储空间有:片内 Flash 与片内 RAM,RAM 相当于内存,Flash 相当于硬盘. 现在我们就一个STM32的工程为例子 linking... Program Size: ...