精華區beta Marginalman 關於我們 聯絡資訊
769. Max Chunks To Make Sorted ## 思路 [1,0,2,3,4] -> [1,0], [2], [3], [4] # stack [1,2,3,4] [2,0,1] -> [2,0,1] # stack [2] mono increasing stack 不過加進stack的值是目前遇到的最大值 最後stack的大小就是chunk數 ## Code ```python class Solution: def maxChunksToSorted(self, arr: List[int]) -> int: stack = [] max_num = 0 for num in arr: max_num = max(num, max_num) while stack and stack[-1] > num: stack.pop() stack.append(max_num) return len(stack) ``` -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 94.156.205.165 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1734607113.A.482.html
sustainer123: 今天還好 12/19 19:22
DJYOMIYAHINA: 我已經不會寫程式了 12/19 19:30
JIWP: 捲死我了 12/19 19:38