{"id":8195,"date":"2021-03-06T09:17:45","date_gmt":"2021-03-06T17:17:45","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8195"},"modified":"2021-03-06T09:20:17","modified_gmt":"2021-03-06T17:20:17","slug":"leetcode-1780-check-if-number-is-a-sum-of-powers-of-three","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1780-check-if-number-is-a-sum-of-powers-of-three\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1780. Check if Number is a Sum of Powers of Three"},"content":{"rendered":"\n<p>Given an integer&nbsp;<code>n<\/code>, return&nbsp;<code>true<\/code>&nbsp;<em>if it is possible to represent&nbsp;<\/em><code>n<\/code><em>&nbsp;as the sum of distinct powers of three.<\/em>&nbsp;Otherwise, return&nbsp;<code>false<\/code>.<\/p>\n\n\n\n<p>An integer&nbsp;<code>y<\/code>&nbsp;is a power of three if there exists an integer&nbsp;<code>x<\/code>&nbsp;such that&nbsp;<code>y == 3<sup>x<\/sup><\/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> n = 12\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> 12 = 3<sup>1<\/sup> + 3<sup>2<\/sup>\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 = 91\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> 91 = 3<sup>0<\/sup> + 3<sup>2<\/sup> + 3<sup>4<\/sup>\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 = 21\n<strong>Output:<\/strong> false\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<sup>7<\/sup><\/code><\/li><\/ul>\n\n\n\n<p>Solution: Greedy + Math<\/p>\n\n\n\n<p>Find the largest 3^x that &lt;= n, subtract that from n and repeat the process.<br>x should be monotonically decreasing, otherwise we have duplicate terms.<br>e.g. 12 = 3<sup>2<\/sup> + 3<sup>1<\/sup> true<br>e.g. 21 = <strong><span class=\"has-inline-color has-vivid-red-color\">3<sup>2<\/sup><\/span><\/strong> + <strong><span class=\"has-inline-color has-vivid-red-color\">3<sup>2<\/sup><\/span><\/strong>+ 3<sup>1<\/sup> false<\/p>\n\n\n\n<p>Time complexity: O(logn?)<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  bool checkPowersOfThree(int n) {\n    int last = INT_MAX;\n    while (n) {\n      int p = 0;\n      int cur = 1;\n      while (cur * 3 <= n) {\n        cur *= 3;\n        ++p;\n      }\n      if (p == last) return false;\n      last = p;    \n      n -= cur;\n    }\n    return true;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer&nbsp;n, return&nbsp;true&nbsp;if it is possible to represent&nbsp;n&nbsp;as the sum of distinct powers of three.&nbsp;Otherwise, return&nbsp;false. An integer&nbsp;y&nbsp;is a power of three if there&#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":[88,31,177],"class_list":["post-8195","post","type-post","status-publish","format-standard","hentry","category-math","tag-greedy","tag-math","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8195","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=8195"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8195\/revisions"}],"predecessor-version":[{"id":8199,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8195\/revisions\/8199"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}