Leetcode 1488 Solution

This article provides solution to leetcode question 1488 (sort-integers-by-the-power-value)

https://leetcode.com/problems/sort-integers-by-the-power-value

Solution

class Solution:
    def getKth(self, lo: int, hi: int, k: int) -> int:
        memo = {}
        def get_power(v):
            nonlocal memo

            if v == 1:
                return 0

            if v in memo:
                return memo[v]

            if v % 2 == 0:
                memo[v] = get_power(v // 2) + 1
            else:
                memo[v] = get_power(3 * v + 1) + 1

            return memo[v]

        data = [(get_power(i), i) for i in range(lo, hi + 1)]
        data.sort()

        return data[k - 1][1]