{"id":1445,"date":"2018-01-01T22:46:46","date_gmt":"2018-01-02T06:46:46","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1445"},"modified":"2018-01-02T02:19:48","modified_gmt":"2018-01-02T10:19:48","slug":"leetcode-754-reach-a-number","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-754-reach-a-number\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 754. Reach a Number"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 754. Reach a Number  - \u5237\u9898\u627e\u5de5\u4f5c EP147\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/Bdw2Y9FrqcU?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><\/p>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u7b2ci\u65f6\u523b\u53ef\u4ee5\u79fb\u52a8+i,-i\u5355\u4f4d\u8ddd\u79bb\u3002\u521d\u59cb\u5728\u539f\u70b9\uff0c\u95ee\u6700\u5c11\u5bf9\u5c11\u65f6\u95f4\u53ef\u4ee5\u79fb\u52a8\u5230target\u5750\u6807\u3002<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>You are standing at position\u00a0<code>0<\/code>\u00a0on an infinite number line. There is a goal at position\u00a0<code>target<\/code>.<\/p>\n<p>On each move, you can either go left or right. During the\u00a0<i>n<\/i>-th move (starting from 1), you take\u00a0<i>n<\/i>\u00a0steps.<\/p>\n<p>Return the minimum number of steps required to reach the destination.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: target = 3\r\nOutput: 2\r\nExplanation:\r\nOn the first move we step from 0 to 1.\r\nOn the second step we step from 1 to 3.\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">Input: target = 2\r\nOutput: 3\r\nExplanation:\r\nOn the first move we step from 0 to 1.\r\nOn the second move we step  from 1 to -1.\r\nOn the third move we step from -1 to 2.\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ul>\n<li><code>target<\/code>\u00a0will be a non-zero integer in the range\u00a0<code>[-10^9, 10^9]<\/code>.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><\/p>\n<p><ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"7983117522\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>Math<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1456\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/755-ep147.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/755-ep147.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/755-ep147-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/755-ep147-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Time complexity: O(sqrt(target))<\/p>\n<p>Space complexity: O(1)<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 3 ms\r\nclass Solution {\r\npublic:\r\n    int reachNumber(int target) {\r\n        target = std::abs(target);\r\n        int k = 0;\r\n        int sum = 0;\r\n        while (sum &lt; target) sum += (++k);\r\n        const int d = sum - target;\r\n        if (d % 2 == 0) return k;\r\n        return k + 1 + (k % 2);\r\n    }\r\n};<\/pre>\n<p>O(1)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 3 ms\r\nclass Solution {\r\npublic:\r\n    int reachNumber(int target) {\r\n        target = std::abs(target);\r\n        int k = sqrt(target * 2);\r\n        while (sum(k) &lt; target) ++k;\r\n        int d = sum(k) - target;\r\n        if (d % 2 == 0) return k;\r\n        return k + 1 + (k % 2);\r\n    }\r\nprivate:\r\n    int sum(int k) {\r\n        return k * (k + 1) \/ 2;\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7b2ci\u65f6\u523b\u53ef\u4ee5\u79fb\u52a8+i,-i\u5355\u4f4d\u8ddd\u79bb\u3002\u521d\u59cb\u5728\u539f\u70b9\uff0c\u95ee\u6700\u5c11\u5bf9\u5c11\u65f6\u95f4\u53ef\u4ee5\u79fb\u52a8\u5230target\u5750\u6807\u3002 Problem: You are standing at position\u00a00\u00a0on an infinite number line. There is a goal at position\u00a0target. On each move, you can either go left&#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":[31],"class_list":["post-1445","post","type-post","status-publish","format-standard","hentry","category-math","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1445","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=1445"}],"version-history":[{"count":11,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1445\/revisions"}],"predecessor-version":[{"id":1459,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1445\/revisions\/1459"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}