LeetCode | 1387. Sort Integers by The Power Value將整數按權重排序【Python】

LeetCode 1387. Sort Integers by The Power Value將整數按權重排序【Medium】【Python】【排序】

Problem

LeetCode

The power of an integer x is defined as the number of steps needed to transform x into 1 using the following steps:

  • if x is even then x = x / 2
  • if x is odd then x = 3 * x + 1

For example, the power of x = 3 is 7 because 3 needs 7 steps to become 1 (3 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1).

Given three integers lo, hi and k. The task is to sort all integers in the interval [lo, hi] by the power value in ascending order, if two or more integers have the same power value sort them by ascending order.

Return the k-th integer in the range [lo, hi] sorted by the power value.

Notice that for any integer x (lo <= x <= hi) it is guaranteed that x will transform into 1 using these steps and that the power of x is will fit in 32 bit signed integer.

Example 1:

Input: lo = 12, hi = 15, k = 2
Output: 13
Explanation: The power of 12 is 9 (12 --> 6 --> 3 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1)
The power of 13 is 9
The power of 14 is 17
The power of 15 is 17
The interval sorted by the power value [12,13,14,15]. For k = 2 answer is the second element which is 13.
Notice that 12 and 13 have the same power value and we sorted them in ascending order. Same for 14 and 15.

Example 2:

Input: lo = 1, hi = 1, k = 1
Output: 1

Example 3:

Input: lo = 7, hi = 11, k = 4
Output: 7
Explanation: The power array corresponding to the interval [7, 8, 9, 10, 11] is [16, 3, 19, 6, 14].
The interval sorted by power is [8, 10, 11, 7, 9].
The fourth number in the sorted array is 7.

Example 4:

Input: lo = 10, hi = 20, k = 5
Output: 13

Example 5:

Input: lo = 1, hi = 1000, k = 777
Output: 570

Constraints:

  • 1 <= lo <= hi <= 1000
  • 1 <= k <= hi - lo + 1

問題

力扣

我們將整數 x 的 權重 定義爲按照下述規則將 x 變成 1 所需要的步數:

  • 如果 x 是偶數,那麼 x = x / 2
  • 如果 x 是奇數,那麼 x = 3 * x + 1

比方說,x=3 的權重爲 7 。因爲 3 需要 7 步變成 1 (3 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1)。

給你三個整數 lo, hi 和 k 。你的任務是將區間 [lo, hi] 之間的整數按照它們的權重 升序排序 ,如果大於等於 2 個整數有 相同 的權重,那麼按照數字自身的數值 升序排序 。

請你返回區間 [lo, hi] 之間的整數按權重排序後的第 k 個數。

注意,題目保證對於任意整數 x (lo <= x <= hi) ,它變成 1 所需要的步數是一個 32 位有符號整數。

示例 1:

輸入:lo = 12, hi = 15, k = 2
輸出:13
解釋:12 的權重爲 9(12 --> 6 --> 3 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1)
13 的權重爲 9
14 的權重爲 17
15 的權重爲 17
區間內的數按權重排序以後的結果爲 [12,13,14,15] 。對於 k = 2 ,答案是第二個整數也就是 13 。
注意,12 和 13 有相同的權重,所以我們按照它們本身升序排序。14 和 15 同理。

示例 2:

輸入:lo = 1, hi = 1, k = 1
輸出:1

示例 3:

輸入:lo = 7, hi = 11, k = 4
輸出:7
解釋:區間內整數 [7, 8, 9, 10, 11] 對應的權重爲 [16, 3, 19, 6, 14] 。
按權重排序後得到的結果爲 [8, 10, 11, 7, 9] 。
排序後數組中第 4 個數字爲 7 。

示例 4:

輸入:lo = 10, hi = 20, k = 5
輸出:13

示例 5:

輸入:lo = 1, hi = 1000, k = 777
輸出:570

提示:

  • 1 <= lo <= hi <= 1000
  • 1 <= k <= hi - lo + 1

思路

排序

將數值、權重構造成字典,然後按照先 value 再 key 排序。
Python3代碼
class Solution:
    def getKth(self, lo: int, hi: int, k: int) -> int:
        nums, weight = [], []
        for x in range(lo, hi + 1):
            nums.append(x)
            weight.append(self.step(x))
        # 將兩個列表合併成字典
        dic = dict(zip(nums, weight))
        
        # 先根據權重排序,再根據數值排序
        res = sorted(dic.items(), key=lambda x: (x[1],x[0]))
        return res[k-1][0]
    
    def step(self, x):
        cnt = 0
        if x == 1:
            return cnt
        while x != 1:
            if x % 2:
                x = 3 * x + 1
            else:
                x = x / 2
            cnt += 1
        return cnt

GitHub鏈接

Python

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章