{"id":6530,"date":"2020-03-22T12:57:18","date_gmt":"2020-03-22T19:57:18","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6530"},"modified":"2020-03-22T12:57:40","modified_gmt":"2020-03-22T19:57:40","slug":"leetcode-1387-sort-integers-by-the-power-value","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1387-sort-integers-by-the-power-value\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1387. Sort Integers by The Power Value"},"content":{"rendered":"\n<p>The power of an integer&nbsp;<code>x<\/code>&nbsp;is defined as the number of steps needed to transform&nbsp;<code>x<\/code>&nbsp;into&nbsp;<code>1<\/code>&nbsp;using the following steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>if&nbsp;<code>x<\/code>&nbsp;is even then&nbsp;<code>x = x \/ 2<\/code><\/li><li>if&nbsp;<code>x<\/code>&nbsp;is odd then&nbsp;<code>x = 3 * x + 1<\/code><\/li><\/ul>\n\n\n\n<p>For example, the power of x = 3 is 7 because 3 needs 7 steps to become 1 (3 &#8211;&gt; 10 &#8211;&gt; 5 &#8211;&gt; 16 &#8211;&gt; 8 &#8211;&gt; 4 &#8211;&gt; 2 &#8211;&gt; 1).<\/p>\n\n\n\n<p>Given three integers&nbsp;<code>lo<\/code>,&nbsp;<code>hi<\/code>&nbsp;and&nbsp;<code>k<\/code>. The task is to sort all integers in the interval&nbsp;<code>[lo, hi]<\/code>&nbsp;by the power value in&nbsp;<strong>ascending order<\/strong>, if two or more integers have&nbsp;<strong>the same<\/strong>&nbsp;power value sort them by&nbsp;<strong>ascending order<\/strong>.<\/p>\n\n\n\n<p>Return the&nbsp;<code>k-th<\/code>&nbsp;integer in the range&nbsp;<code>[lo, hi]<\/code>&nbsp;sorted by the power value.<\/p>\n\n\n\n<p>Notice that for any&nbsp;integer&nbsp;<code>x<\/code>&nbsp;<code>(lo &lt;= x &lt;= hi)<\/code>&nbsp;it is&nbsp;<strong>guaranteed<\/strong>&nbsp;that&nbsp;<code>x<\/code>&nbsp;will transform into&nbsp;<code>1<\/code>&nbsp;using these steps and that the power of&nbsp;<code>x<\/code>&nbsp;is will&nbsp;<strong>fit<\/strong>&nbsp;in 32 bit signed integer.<\/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> lo = 12, hi = 15, k = 2\n<strong>Output:<\/strong> 13\n<strong>Explanation:<\/strong> The power of 12 is 9 (12 --&gt; 6 --&gt; 3 --&gt; 10 --&gt; 5 --&gt; 16 --&gt; 8 --&gt; 4 --&gt; 2 --&gt; 1)\nThe power of 13 is 9\nThe power of 14 is 17\nThe power of 15 is 17\nThe interval sorted by the power value [12,13,14,15]. For k = 2 answer is the second element which is 13.\nNotice that 12 and 13 have the same power value and we sorted them in ascending order. Same for 14 and 15.\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> lo = 1, hi = 1, k = 1\n<strong>Output:<\/strong> 1\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> lo = 7, hi = 11, k = 4\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong> The power array corresponding to the interval [7, 8, 9, 10, 11] is [16, 3, 19, 6, 14].\nThe interval sorted by power is [8, 10, 11, 7, 9].\nThe fourth number in the sorted array is 7.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> lo = 10, hi = 20, k = 5\n<strong>Output:<\/strong> 13\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> lo = 1, hi = 1000, k = 777\n<strong>Output:<\/strong> 570\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= lo &lt;= hi &lt;= 1000<\/code><\/li><li><code>1 &lt;= k &lt;= hi - lo + 1<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Precompute + quick select<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(nlogn) + 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\nclass Solution {\npublic:\n  int getKth(int lo, int hi, int k) {\n    vector<pair<int, int>> vals;\n    for (int n = lo; n <= hi; ++n) {\n      int p = 0;\n      int x = n;\n      while (x != 1) {\n        if (x &#038; 1) x = 3 * x + 1;\n        else x \/= 2;\n        ++p;\n      }\n      vals.emplace_back(p, n);\n    }\n    nth_element(begin(vals), begin(vals) + k - 1, end(vals));\n    return vals[k - 1].second;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The power of an integer&nbsp;x&nbsp;is defined as the number of steps needed to transform&nbsp;x&nbsp;into&nbsp;1&nbsp;using the following steps: if&nbsp;x&nbsp;is even then&nbsp;x = x \/ 2 if&nbsp;x&nbsp;is&#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":[573,179],"class_list":["post-6530","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-quickselect","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6530","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=6530"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6530\/revisions"}],"predecessor-version":[{"id":6532,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6530\/revisions\/6532"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6530"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}