{"id":6432,"date":"2020-03-09T00:52:12","date_gmt":"2020-03-09T07:52:12","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6432"},"modified":"2020-03-09T00:53:00","modified_gmt":"2020-03-09T07:53:00","slug":"leetcode-365-water-and-jug-problem","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-365-water-and-jug-problem\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 365. Water and Jug Problem"},"content":{"rendered":"\n<p>You are given two jugs with capacities&nbsp;<em>x<\/em>&nbsp;and&nbsp;<em>y<\/em>&nbsp;litres. There is an infinite amount of water supply available. You need to determine whether it is possible to measure exactly&nbsp;<em>z<\/em>&nbsp;litres using these two jugs.<\/p>\n\n\n\n<p>If&nbsp;<em>z<\/em>&nbsp;liters of water is measurable, you must have&nbsp;<em>z<\/em>&nbsp;liters of water contained within&nbsp;<strong>one or both buckets<\/strong>&nbsp;by the end.<\/p>\n\n\n\n<p>Operations allowed:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Fill any of the jugs completely with water.<\/li><li>Empty any of the jugs.<\/li><li>Pour water from one jug into another till the other jug is completely full or the first jug itself is empty.<\/li><\/ul>\n\n\n\n<p><strong>Example 1:<\/strong>&nbsp;(From the famous&nbsp;<a href=\"https:\/\/www.youtube.com\/watch?v=BVtQNK_ZUJg\" target=\"_blank\" rel=\"noreferrer noopener\"><em>&#8220;Die Hard&#8221;<\/em>&nbsp;example<\/a>)<\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\">Input: x = 3, y = 5, z = 4\nOutput: True\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\">Input: x = 2, y = 6, z = 5\nOutput: False<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math<\/strong><\/h2>\n\n\n\n<p>special case 1: x == z or y == z or x + y == z: return True<br>special case 2: x + y &lt; z: return False<br>normal case: z must be a factor of gcd(x, y)<\/p>\n\n\n\n<p>Time complexity: O(log(min(x, y))<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool canMeasureWater(int x, int y, int z) {\n    if (x == z || y == z || x + y == z) return true;\n    if (x + y < z) return false;\n    return z % gcd(x, y) == 0;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two jugs with capacities&nbsp;x&nbsp;and&nbsp;y&nbsp;litres. There is an infinite amount of water supply available. You need to determine whether it is possible to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[359,31,177,459],"class_list":["post-6432","post","type-post","status-publish","format-standard","hentry","category-math","tag-gcd","tag-math","tag-medium","tag-ologn","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6432","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6432"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6432\/revisions"}],"predecessor-version":[{"id":6434,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6432\/revisions\/6434"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}