LeetCode 0365. Water and Jug Problem水壶问题【Medium】【Python】【BFS】【数学】

Problem

LeetCode

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

问题

力扣

有两个容量分别为 x升 和 y升 的水壶以及无限多的水。请判断能否通过使用这两个水壶,从而可以得到恰好 z升 的水?

如果可以,最后请用以上水壶中的一或两个来盛放取得的 z升 水。

你允许:

  • 装满任意一个水壶
  • 清空任意一个水壶
  • 从一个水壶向另外一个水壶倒水,直到装满或者倒空

示例 1: (From the famous “Die Hard” example)

输入: x = 3, y = 5, z = 4
输出: True

示例 2:

输入: x = 2, y = 6, z = 5
输出: False

思路

解法一

BFS

每次水壶都有三个操作:加满水、清空水、相互倒。
Python3代码
class Solution:def canMeasureWater(self, x: int, y: int, z: int) -> bool:# solution one: BFSfrom collections import dequequeue = deque([[0, 0]])visited = set([(0, 0)])while queue:cur_x, cur_y = queue.pop()if z in [cur_x, cur_y, cur_x + cur_y]:return Truefor item in [# x 加满水,y 加满水(x, cur_y), (cur_x, y),# x 清空水,y 清空水(0, cur_y), (cur_x, 0),# 把 x 壶的水灌进 y 壶,直至灌满或倒空(cur_x + cur_y - y, y) if cur_x + cur_y >= y else (0, cur_x + cur_y),# 把 X 壶的水灌进 Y 壶,直至灌满或倒空(x, cur_x + cur_y - x) if cur_x + cur_y >= x else (cur_x + cur_y, 0)]:if item not in visited:queue.appendleft(item)  # 从队列左边加入元素visited.add(item)return False
解法二

裴蜀定理

能否找到整数 a,b 使得方程 ax + by = z 有解。
有整数解时,当且仅当 z 是 a 和 b 的最大公约数 d 的倍数。
Python3代码
class Solution:def canMeasureWater(self, x: int, y: int, z: int) -> bool:# solution two: 裴蜀定理import mathif x + y < z:return Falseif x == z or y == z or x + y == z:return Truereturn z % math.gcd(x, y) == 0

GitHub链接

Python

参考

暴力 + 数学

LeetCode | 0365. Water and Jug Problem水壶问题【Python】相关推荐

  1. leetcode 365. Water and Jug Problem | 365. 水壶问题(Java)

    题目 https://leetcode.com/problems/water-and-jug-problem/ 又是踩比赞多的一道题-我认为有两个可能的原因: 虽然通过了,但原理不明了. 本质是个数学 ...

  2. LeetCode Water and Jug Problem(巧妙转换为gcd问题)

    题意:给出x,y升水壶,问是否可以量出z升水.有三种操作:填满水壶,清空水壶. 从其中一个水壶倒入另一个水壶 代码如下: public class Solution {private int gcd( ...

  3. LeetCode-3.21-365-M-水壶问题(Water and Jug Problem)

    文章目录 思路-wait 解法 有两个容量分别为 x升 和 y升 的水壶以及无限多的水.请判断能否通过使用这两个水壶,从而可以得到恰好 z升 的水? 如果可以,最后请用以上水壶中的一或两个来盛放取得的 ...

  4. LeetCode 148. Sort List--面试算法题--C++,Python解法

    LeetCode 148. Sort List–面试算法题–C++,Python解法 LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题目总结 大 ...

  5. LeetCode Notes_#206 Reverse Linked List(C++,Python)

    LeetCode Notes_#206 Reverse Linked List(C++,Python) LeetCode Linked List  Contents 题目 思路 思考 解答 C++ P ...

  6. 人工智能 水壶问题 python解法

    人工智能 水壶问题 python解法 系列文章 人工智能 倒啤酒问题 python解法 人工智能 水壶问题 python解法 A*算法之八数码问题 python解法 A*算法之野人传教士问题 pyth ...

  7. 【LeetCode】91. Decode Ways 解题报告(Python)

    [LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  8. LeetCode 226. Invert Binary Tree--反转二叉树--C++,Python解法--递归,迭代做法

    题目地址:Invert Binary Tree - LeetCode Invert a binary tree. Example: Input: 4/ \2 7/ \ / \ 1 3 6 9 Outp ...

  9. 【LeetCode】275. H-Index II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/h-index- ...

最新文章

  1. python开发的软件sparrow-黑客常用wifi蓝牙分析攻击工具,让你的设备陷入危险之中...
  2. 多协议标签的MPLS发布与管理_MPLS
  3. 第三次学JAVA再学不好就吃翔(part89)--HashSet
  4. windows下使用nginx调试简介
  5. MySQL——binlog,redo log
  6. 不等式约束的拉格朗日乘数法_Abaqus血管支架仿真|接触约束执行方式
  7. Win11更新22000.71:优化任务栏、右键菜单视觉风格
  8. RabbitMQ消息确认以及return机制
  9. mac r 导出csv文件_R在Max OS进行导入和导出xlsx文件
  10. mac学python_新手小白学Python必备编程利器Pycharm快捷键大全(Win+Mac)
  11. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_10 打印流_1_打印流_概述和使用...
  12. adb工具包的安装和使用方法
  13. 2.6 列昂惕夫投入产出模型(第2章矩阵代数)
  14. 2022-2028中国安全代码审查软件市场现状研究分析与发展前景预测报告
  15. chm文件打开中文乱码
  16. iOS系统各版本占比/占有率
  17. esp32 Micropython驱动ST7735 1.8寸TFT屏幕 中文显示;时间显示、网络network实时时间获取utptime;urequests、upip等包安装
  18. 【文献翻译】综述:机器学习可解释性
  19. JQuery库的使用
  20. Python3——青蛙跳台阶问题

热门文章

  1. win10如何添加开机自己启动软件
  2. 房源管理系统服务器,房源管理系统服务器
  3. kvm调整配置cpu、内存
  4. Echarts在map地图上添加散点图
  5. Mac苹果电脑怎么格式化U盘
  6. 算法练习(5)———木块问题
  7. PAT (顶级) 升级版 7-1 Werewolf harder version(15分)
  8. ICloud没有密码怎么注销?
  9. python时间序列分析航空旅人_Python深度学习教程:LSTM时间序列预测小练习—国航乘客数量预测...
  10. Sinoregal dbAudit应用-SinoDB