3011. Find if Array Can Be Sorted ## 思路 相鄰同bits的值可以互換 = 根據bits數partition後, 每一組內的值都要比前一組的最大值還大 記錄目前的bits數跟最大值, bits數不同就更新prev_max 然後檢查num是否比prev_max大 e.g. [2,4,1] [6,3,5] -> [1,2,4] [3,5,6] -- False ([4] > 3) [2,4] [6,5] [7,11] -> [2,4] [5,6] [7,11] -- True ## Code ```python class Solution: def canSortArray(self, nums: List[int]) -> bool: curr_bits = curr_max = 0 prev_max = 0 for num in nums: if num.bit_count() != curr_bits: curr_bits = num.bit_count() prev_max, curr_max = curr_max, num else: curr_max = max(curr_max, num) if prev_max > num: return False return True ``` -- ※ 發信站: 批踢踢實業坊(ptt-club.com.tw), 來自: 94.156.205.178 (臺灣) ※ 文章網址: https://ptt-club.com.tw/Marginalman/M.1730891473.A.220