{"id":9302,"date":"2021-12-31T05:14:17","date_gmt":"2021-12-31T13:14:17","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9302"},"modified":"2021-12-31T05:17:29","modified_gmt":"2021-12-31T13:17:29","slug":"leetcode-1936-add-minimum-number-of-rungs","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1936-add-minimum-number-of-rungs\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1936. Add Minimum Number of Rungs"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>strictly increasing<\/strong>&nbsp;integer array&nbsp;<code>rungs<\/code>&nbsp;that represents the&nbsp;<strong>height<\/strong>&nbsp;of rungs on a ladder. You are currently on the&nbsp;<strong>floor<\/strong>&nbsp;at height&nbsp;<code>0<\/code>, and you want to reach the last rung.<\/p>\n\n\n\n<p>You are also given an integer&nbsp;<code>dist<\/code>. You can only climb to the next highest rung if the distance between where you are currently at (the floor or on a rung) and the next rung is&nbsp;<strong>at most<\/strong>&nbsp;<code>dist<\/code>. You are able to insert rungs at any positive&nbsp;<strong>integer<\/strong>&nbsp;height if a rung is not already there.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>minimum<\/strong>&nbsp;number of rungs that must be added to the ladder in order for you to climb to the last rung.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> rungs = [1,3,5,10], dist = 2\n<strong>Output:<\/strong> 2\n<strong>Explanation:\n<\/strong>You currently cannot reach the last rung.\nAdd rungs at heights 7 and 8 to climb this ladder. \nThe ladder will now have rungs at [1,3,5,7,8,10].\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> rungs = [3,6,8,10], dist = 3\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong>\nThis ladder can be climbed without adding additional rungs.\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> rungs = [3,4,6,7], dist = 2\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong>\nYou currently cannot reach the first rung from the ground.\nAdd a rung at height 1 to climb this ladder.\nThe ladder will now have rungs at [1,3,4,6,7].\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= rungs.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= rungs[i] &lt;= 10<sup>9<\/sup><\/code><\/li><li><code>1 &lt;= dist &lt;= 10<sup>9<\/sup><\/code><\/li><li><code>rungs<\/code>&nbsp;is&nbsp;<strong>strictly increasing<\/strong>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math<\/strong><\/h2>\n\n\n\n<p>Check two consecutive rungs, if their diff is &gt; dist, we need insert (diff &#8211; 1) \/ dist rungs in between.<br>ex1 5 -&gt; 11, diff = 6, dist = 2, (diff &#8211; 1) \/ dist = (6 &#8211; 1) \/ 2 = 2. =&gt; 5, <span style=\"text-decoration: underline;\">7, 9<\/span>, 11.<br>ex2 0 -&gt; 3, diff = 3, dist = 1, (diff &#8211; 1) \/ dist = (3 &#8211; 1) \/ 1 = 2 =&gt; 0, <span style=\"text-decoration: underline;\">1, 2,<\/span> 3<\/p>\n\n\n\n<p>Time complexity: O(n)<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  int addRungs(vector<int>& rungs, int dist) {\n    int ans = 0;\n    for (size_t i = 0; i < rungs.size(); ++i)\n      ans += (rungs[i] - (i ? rungs[i - 1] : 0) - 1) \/ dist;    \n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def addRungs(self, rungs: List[int], dist: int) -> int:\n    return sum((r - 1 - (rungs[i - 1] if i else 0)) \/\/ dist for i, r in enumerate(rungs))\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;strictly increasing&nbsp;integer array&nbsp;rungs&nbsp;that represents the&nbsp;height&nbsp;of rungs on a ladder. You are currently on the&nbsp;floor&nbsp;at height&nbsp;0, and you want to reach the last&#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":[613,31,177],"class_list":["post-9302","post","type-post","status-publish","format-standard","hentry","category-math","tag-diff","tag-math","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9302","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=9302"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9302\/revisions"}],"predecessor-version":[{"id":9304,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9302\/revisions\/9304"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}