{"id":4959,"date":"2019-03-09T22:01:54","date_gmt":"2019-03-10T06:01:54","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4959"},"modified":"2019-03-10T00:48:50","modified_gmt":"2019-03-10T08:48:50","slug":"leetcode-1006-clumsy-factorial","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1006-clumsy-factorial\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1006. Clumsy Factorial"},"content":{"rendered":"\n<p>Normally, the factorial of a positive integer&nbsp;<code>n<\/code>&nbsp;is the product of all positive integers less than or equal to&nbsp;<code>n<\/code>.&nbsp; For example,&nbsp;<code>factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1<\/code>.<\/p>\n\n\n\n<p>We instead make a&nbsp;<em>clumsy factorial:<\/em>&nbsp;using the integers in decreasing order, we&nbsp;swap out the multiply operations for a fixed rotation of operations:&nbsp;multiply (*), divide (\/), add (+) and subtract (-) in this order.<\/p>\n\n\n\n<p>For example,&nbsp;<code>clumsy(10) = 10 * 9 \/ 8 + 7 - 6 * 5 \/ 4 + 3 - 2 * 1<\/code>.&nbsp; However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right.<\/p>\n\n\n\n<p>Additionally, the division that we use is&nbsp;<em>floor division<\/em>&nbsp;such that&nbsp;<code>10 * 9 \/ 8<\/code>&nbsp;equals&nbsp;<code>11<\/code>.&nbsp; This guarantees the result is&nbsp;an integer.<\/p>\n\n\n\n<p><code>Implement the&nbsp;clumsy<\/code>&nbsp;function&nbsp;as defined above: given an integer&nbsp;<code>N<\/code>, it returns the clumsy factorial of&nbsp;<code>N<\/code>.<\/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>4\n<strong>Output:<\/strong>&nbsp;7\n<strong>Explanation:<\/strong> 7 = 4 * 3 \/ 2 + 1\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>10\n<strong>Output: <\/strong>12\n<strong>Explanation: <\/strong>12 = 10 * 9 \/ 8 + 7 - 6 * 5 \/ 4 + 3 - 2 * 1\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= N &lt;= 10000<\/code><\/li><li><code>-2^31 &lt;= answer &lt;= 2^31 - 1<\/code>&nbsp; (The answer is guaranteed to fit within a 32-bit integer.)<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\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 4ms 8.2 MB\nclass Solution {\npublic:\n  int clumsy(int N) {\n    if (N <= 0) return 0;\n    if (N == 1) return 1;\n    if (N == 2) return 2 * 1;\n    if (N == 3) return 3 * 2 \/ 1;\n    \n    int ans = N * (N - 1) \/ (N - 2) + (N - 3);\n    \n    while ((N -= 4) >= 4)\n      ans = ans - N * (N - 1) \/ (N - 2) + (N - 3);\n    \n    return ans - clumsy(N);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Normally, the factorial of a positive integer&nbsp;n&nbsp;is the product of all positive integers less than or equal to&nbsp;n.&nbsp; For example,&nbsp;factorial(10) = 10 * 9 *&#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":[31,177,179],"class_list":["post-4959","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-math","tag-medium","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4959","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=4959"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4959\/revisions"}],"predecessor-version":[{"id":4970,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4959\/revisions\/4970"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}