java.lang.Object
g1301_1400.s1387_sort_integers_by_the_power_value.Solution

public class Solution extends Object
1387 - Sort Integers by The Power Value.<p>Medium</p> <p>The power of an integer <code>x</code> is defined as the number of steps needed to transform <code>x</code> into <code>1</code> using the following steps:</p> <ul> <li>if <code>x</code> is even then <code>x = x / 2</code></li> <li>if <code>x</code> is odd then <code>x = 3 * x + 1</code></li> </ul> <p>For example, the power of <code>x = 3</code> is <code>7</code> because <code>3</code> needs <code>7</code> steps to become <code>1</code> (<code>3 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1</code>).</p> <p>Given three integers <code>lo</code>, <code>hi</code> and <code>k</code>. The task is to sort all integers in the interval <code>[lo, hi]</code> by the power value in <strong>ascending order</strong> , if two or more integers have <strong>the same</strong> power value sort them by <strong>ascending order</strong>.</p> <p>Return the <code>k<sup>th</sup></code> integer in the range <code>[lo, hi]</code> sorted by the power value.</p> <p>Notice that for any integer <code>x</code> <code>(lo <= x <= hi)</code> it is <strong>guaranteed</strong> that <code>x</code> will transform into <code>1</code> using these steps and that the power of <code>x</code> is will <strong>fit</strong> in a 32-bit signed integer.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> lo = 12, hi = 15, k = 2</p> <p><strong>Output:</strong> 13</p> <p><strong>Explanation:</strong> The power of 12 is 9 (12 &ndash;> 6 &ndash;> 3 &ndash;> 10 &ndash;> 5 &ndash;> 16 &ndash;> 8 &ndash;> 4 &ndash;> 2 &ndash;> 1)</p> <p>The power of 13 is 9</p> <p>The power of 14 is 17</p> <p>The power of 15 is 17</p> <p>The interval sorted by the power value [12,13,14,15]. For k = 2 answer is the second element which is 13.</p> <p>Notice that 12 and 13 have the same power value and we sorted them in ascending order. Same for 14 and 15.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> lo = 7, hi = 11, k = 4</p> <p><strong>Output:</strong> 7</p> <p><strong>Explanation:</strong> The power array corresponding to the interval [7, 8, 9, 10, 11] is [16, 3, 19, 6, 14].</p> <p>The interval sorted by power is [8, 10, 11, 7, 9].</p> <p>The fourth number in the sorted array is 7.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= lo <= hi <= 1000</code></li> <li><code>1 <= k <= hi - lo + 1</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • getKth

      public int getKth(int lo, int hi, int k)