{"id":7116,"date":"2020-07-18T21:43:28","date_gmt":"2020-07-19T04:43:28","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7116"},"modified":"2020-07-18T21:44:15","modified_gmt":"2020-07-19T04:44:15","slug":"leetcode-1518-water-bottles","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1518-water-bottles\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1518. Water Bottles"},"content":{"rendered":"\n<p>Given&nbsp;<code>numBottles<\/code>&nbsp;full water bottles, you can exchange&nbsp;<code>numExchange<\/code>&nbsp;empty water bottles for one full water bottle.<\/p>\n\n\n\n<p>The operation of drinking a full water bottle turns it into an empty bottle.<\/p>\n\n\n\n<p>Return the&nbsp;<strong>maximum<\/strong>&nbsp;number of water bottles you can&nbsp;drink.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/07\/01\/sample_1_1875.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> numBottles = 9, numExchange = 3\n<strong>Output:<\/strong> 13\n<strong>Explanation:<\/strong> You can exchange 3 empty bottles to get 1 full water bottle.\nNumber of water bottles you can&nbsp;drink: 9 + 3 + 1 = 13.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/07\/01\/sample_2_1875.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> numBottles = 15, numExchange = 4\n<strong>Output:<\/strong> 19\n<strong>Explanation:<\/strong> You can exchange 4 empty bottles to get 1 full water bottle. \nNumber of water bottles you can&nbsp;drink: 15 + 3 + 1 = 19.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> numBottles = 5, numExchange = 5\n<strong>Output:<\/strong> 6\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> numBottles = 2, numExchange = 3\n<strong>Output:<\/strong> 2\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;=&nbsp;numBottles &lt;= 100<\/code><\/li><li><code>2 &lt;=&nbsp;numExchange &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(logb\/loge)?<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++\">class Solution {\npublic:\n  int numWaterBottles(int numBottles, int numExchange) {\n    int ans = 0;\n    while (numBottles <= numExchange) {\n      const int r = numBottles % numExchange;\n      ans += numBottles - r;\n      numBottles = r + numBottles \/ numExchange;\n    }\n    ans += numBottles;\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"java\">class Solution {\n  public int numWaterBottles(int numBottles, int numExchange) {\n    int ans = 0;\n    while (numBottles <= numExchange) {\n      final int r = numBottles % numExchange;\n      ans += numBottles - r;\n      numBottles = r + numBottles \/ numExchange;\n    }\n    ans += numBottles;\n    return ans;\n  }\n}\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">class Solution:\n  def numWaterBottles(self, numBottles: int, numExchange: int) ->; int:\n    ans = 0\n    while numBottles <= numExchange:\n      r = numBottles % numExchange\n      ans += numBottles - r\n      numBottles = r + numBottles \/\/ numExchange\n    ans += numBottles\n    return ans\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given&nbsp;numBottles&nbsp;full water bottles, you can exchange&nbsp;numExchange&nbsp;empty water bottles for one full water bottle. The operation of drinking a full water bottle turns it into an&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[],"class_list":["post-7116","post","type-post","status-publish","format-standard","hentry","category-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7116","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=7116"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7116\/revisions"}],"predecessor-version":[{"id":7118,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7116\/revisions\/7118"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}