{"id":6112,"date":"2020-01-19T16:48:50","date_gmt":"2020-01-20T00:48:50","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6112"},"modified":"2020-01-20T18:23:17","modified_gmt":"2020-01-21T02:23:17","slug":"leetcode-1326-minimum-number-of-taps-to-open-to-water-a-garden","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1326-minimum-number-of-taps-to-open-to-water-a-garden\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1326. Minimum Number of Taps to Open to Water a Garden"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1326. Minimum Number of Taps to Open to Water a Garden - \u5237\u9898\u627e\u5de5\u4f5c EP301\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/G88X89Eo2C0?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>There is a one-dimensional garden on the x-axis. The garden starts at the point&nbsp;<code>0<\/code>&nbsp;and ends at the point&nbsp;<code>n<\/code>. (i.e The length of the garden is&nbsp;<code>n<\/code>).<\/p>\n\n\n\n<p>There are&nbsp;<code>n + 1<\/code>&nbsp;taps located&nbsp;at points&nbsp;<code>[0, 1, ..., n]<\/code>&nbsp;in the garden.<\/p>\n\n\n\n<p>Given an integer&nbsp;<code>n<\/code>&nbsp;and an integer array&nbsp;<code>ranges<\/code>&nbsp;of length&nbsp;<code>n + 1<\/code>&nbsp;where&nbsp;<code>ranges[i]<\/code>&nbsp;(0-indexed) means the&nbsp;<code>i-th<\/code>&nbsp;tap can water the area&nbsp;<code>[i - ranges[i], i + ranges[i]]<\/code>&nbsp;if it was open.<\/p>\n\n\n\n<p>Return&nbsp;<em>the minimum number of taps<\/em>&nbsp;that should be open to water the whole garden, If the garden cannot be watered return&nbsp;<strong>-1<\/strong>.<\/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\/01\/16\/1685_example_1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 5, ranges = [3,4,1,1,0,0]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> The tap at point 0 can cover the interval [-3,3]\nThe tap at point 1 can cover the interval [-3,5]\nThe tap at point 2 can cover the interval [1,3]\nThe tap at point 3 can cover the interval [2,4]\nThe tap at point 4 can cover the interval [4,4]\nThe tap at point 5 can cover the interval [5,5]\nOpening Only the second tap will water the whole garden [0,5]\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 3, ranges = [0,0,0,0]\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> Even if you activate all the four taps you cannot water the whole garden.\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> n = 7, ranges = [1,2,1,0,2,1,0,1]\n<strong>Output:<\/strong> 3\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> n = 8, ranges = [4,0,0,0,0,0,0,0,4]\n<strong>Output:<\/strong> 2\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 8, ranges = [4,0,0,0,4,0,0,0,4]\n<strong>Output:<\/strong> 1\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 10^4<\/code><\/li><li><code>ranges.length == n + 1<\/code><\/li><li><code>0 &lt;= ranges[i] &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Greedy<\/strong><\/h2>\n\n\n\n<p>Reduce to <a href=\"https:\/\/zxi.mytechroad.com\/blog\/leetcode\/leetcode-weekly-contest-131-1021-1022-1023-1024\/\">1024. Video Stitching<\/a><\/p>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(n)<\/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  int minTaps(int n, vector<int>& ranges) {\n    vector<pair<int, int>> t;\n    \/\/ O(n) reduction\n    for (int i = 0; i <= n; ++i)\n      t.emplace_back(max(0, i - ranges[i]), \n                     min(i + ranges[i], n));\n    \n    \/\/ 1024. Video Stiching\n    sort(begin(t), end(t));\n    int ans = 0;\n    int i = 0;\n    int l = 0;\n    int e = 0;\n    while (e < n) {\n      \/\/ Extend to the right most w.r.t t[i].first <= l\n      while (i <= n &#038;&#038; t[i].first <= l)\n        e = max(e, t[i++].second);\n      if (l == e) return -1; \/\/ Can not extend\n      l = e;\n      ++ans;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Greedy<\/strong><\/h2>\n\n\n\n<p>Reduce to <a href=\"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-45-jump-game-ii\/\">45. Jump Game II<\/a><\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/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  int minTaps(int n, vector<int>& ranges) {\n    vector<int> nums(ranges.size());\n    for (int i = 0; i <= n; ++i) {\n      int s = max(0, i - ranges[i]);      \n      nums[s] = max(nums[s], i + ranges[i]);\n    }    \n    \/\/ 45. Jump Game II\n    int steps = 0;\n    int l = 0;\n    int e = 0;\n    for (int i = 0; i <= n; ++i) {\n      if (i > e) return -1;\n      if (i > l) { ++steps; l = e; }\n      e = max(e, nums[i]);\n    }\n    return steps;\n  }\n};\n\n\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There is a one-dimensional garden on the x-axis. The garden starts at the point&nbsp;0&nbsp;and ends at the point&nbsp;n. (i.e The length of the garden is&nbsp;n).&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[284,88,376,377],"class_list":["post-6112","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-geometry","tag-greedy","tag-on","tag-onlogn","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6112","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=6112"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6112\/revisions"}],"predecessor-version":[{"id":6116,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6112\/revisions\/6116"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}