{"id":3005,"date":"2018-07-07T09:52:06","date_gmt":"2018-07-07T16:52:06","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3005"},"modified":"2018-07-07T09:55:29","modified_gmt":"2018-07-07T16:55:29","slug":"leetcode-441-arranging-coins","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-441-arranging-coins\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 441. Arranging Coins"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>You have a total of\u00a0<i>n<\/i>\u00a0coins that you want to form in a staircase shape, where every\u00a0<i>k<\/i>-th row must have exactly\u00a0<i>k<\/i>coins.<\/p>\n<p>Given\u00a0<i>n<\/i>, find the total number of\u00a0<b>full<\/b>\u00a0staircase rows that can be formed.<\/p>\n<p><i>n<\/i>\u00a0is a non-negative integer and fits within the range of a 32-bit signed integer.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"crayon:false\">n = 5\r\n\r\nThe coins can form the following rows:\r\n\u00a4\r\n\u00a4 \u00a4\r\n\u00a4 \u00a4\r\n\r\nBecause the 3rd row is incomplete, we return 2.\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"crayon:false\">n = 8\r\n\r\nThe coins can form the following rows:\r\n\u00a4\r\n\u00a4 \u00a4\r\n\u00a4 \u00a4 \u00a4\r\n\u00a4 \u00a4\r\n\r\nBecause the 4th row is incomplete, we return 3.\r\n<\/pre>\n<h1><strong>Solution: Math<\/strong><\/h1>\n<p>Time complexity: O(sqrt(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 16 ms\r\nclass Solution {\r\npublic:\r\n  int arrangeCoins(int n) {\r\n    for (long i = sqrt(n) * 2 + 1; i &gt;= 0; --i)\r\n      if (i * (i + 1) \/ 2 &lt;= n) return i;\r\n    return -1;\r\n  }\r\n};<\/pre>\n<p>Time complexity: O(1)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 12 ms\r\nclass Solution {\r\npublic:\r\n  int arrangeCoins(int n) {\r\n    \/\/ x * (1 + x) \/ 2 &lt;= n\r\n    \/\/ x^2 + x - 2n &lt;= 0\r\n    \/\/ x &lt;= (-1 + sqrt(1 + 8n)) \/ 2\r\n    return (-1 + sqrt(1 + static_cast&lt;long&gt;(n) * 8)) \/ 2;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem You have a total of\u00a0n\u00a0coins that you want to form in a staircase shape, where every\u00a0k-th row must have exactly\u00a0kcoins. Given\u00a0n, find the total&#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":[222,31],"class_list":["post-3005","post","type-post","status-publish","format-standard","hentry","category-math","tag-easy","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3005","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=3005"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3005\/revisions"}],"predecessor-version":[{"id":3009,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3005\/revisions\/3009"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}