{"id":8642,"date":"2021-10-25T20:10:37","date_gmt":"2021-10-26T03:10:37","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8642"},"modified":"2021-10-25T20:11:41","modified_gmt":"2021-10-26T03:11:41","slug":"leetcode-2048-next-greater-numerically-balanced-number","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-2048-next-greater-numerically-balanced-number\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2048. Next Greater Numerically Balanced Number"},"content":{"rendered":"\n<p>An integer&nbsp;<code>x<\/code>&nbsp;is&nbsp;<strong>numerically balanced<\/strong>&nbsp;if for every digit&nbsp;<code>d<\/code>&nbsp;in the number&nbsp;<code>x<\/code>, there are&nbsp;<strong>exactly<\/strong>&nbsp;<code>d<\/code>&nbsp;occurrences of that digit in&nbsp;<code>x<\/code>.<\/p>\n\n\n\n<p>Given an integer&nbsp;<code>n<\/code>, return&nbsp;<em>the&nbsp;<strong>smallest numerically balanced<\/strong>&nbsp;number&nbsp;<strong>strictly greater<\/strong>&nbsp;than&nbsp;<\/em><code>n<\/code><em>.<\/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> n = 1\n<strong>Output:<\/strong> 22\n<strong>Explanation:<\/strong> \n22 is numerically balanced since:\n- The digit 2 occurs 2 times. \nIt is also the smallest numerically balanced number strictly greater than 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> n = 1000\n<strong>Output:<\/strong> 1333\n<strong>Explanation:<\/strong> \n1333 is numerically balanced since:\n- The digit 1 occurs 1 time.\n- The digit 3 occurs 3 times. \nIt is also the smallest numerically balanced number strictly greater than 1000.\nNote that 1022 cannot be the answer because 0 appeared more than 0 times.\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 = 3000\n<strong>Output:<\/strong> 3133\n<strong>Explanation:<\/strong> \n3133 is numerically balanced since:\n- The digit 1 occurs 1 time.\n- The digit 3 occurs 3 times.\nIt is also the smallest numerically balanced number strictly greater than 3000.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>0 &lt;= n &lt;= 10<sup>6<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Permutation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(log(n)!)<br>Space complexity: O(log(n)) ?<\/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 nextBeautifulNumber(int n) {\n    const string t = to_string(n);\n    vector<int> nums{1,\n                     22,\n                     122, 333,\n                     1333, 4444,\n                     14444, 22333, 55555,\n                     122333, 155555, 224444, 666666};\n    int ans = 1224444;\n    for (int num : nums) {\n      string s = to_string(num);\n      if (s.size() < t.size()) continue;\n      else if (s.size() > t.size()) {\n        ans = min(ans, num);\n      } else { \/\/ same length \n        do {\n          if (s > t) ans = min(ans, stoi(s));\n        } while (next_permutation(begin(s), end(s)));\n      }\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>An integer&nbsp;x&nbsp;is&nbsp;numerically balanced&nbsp;if for every digit&nbsp;d&nbsp;in the number&nbsp;x, there are&nbsp;exactly&nbsp;d&nbsp;occurrences of that digit in&nbsp;x. Given an integer&nbsp;n, return&nbsp;the&nbsp;smallest numerically balanced&nbsp;number&nbsp;strictly greater&nbsp;than&nbsp;n. Example 1: Input: n&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[177,121,42],"class_list":["post-8642","post","type-post","status-publish","format-standard","hentry","category-searching","tag-medium","tag-permutation","tag-search","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8642","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=8642"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8642\/revisions"}],"predecessor-version":[{"id":8644,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8642\/revisions\/8644"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}