Re: [闲聊] 每日leetcode

楼主: dont   2024-12-19 19:18:30
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)
```

Links booklink

Contact Us: admin [ a t ] ucptt.com