Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 99f9883

Browse files
committed
add 768
1 parent 02576af commit 99f9883

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

‎LeetCodeSolutions/768.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#==================================================
2+
#==> Title: 768. 最多能完成排序的块 II
3+
#==> Author: Zhang zhen
4+
#==> Email: hustmatnoble.gmail.com
5+
#==> GitHub: https://github.com/MatNoble
6+
#==> Date: 2/6/2021
7+
#==================================================
8+
9+
"""
10+
https://leetcode-cn.com/problems/max-chunks-to-make-sorted-ii/
11+
12+
题解:
13+
https://matnoble.me/dsa/basic/array-stack-queue/#768-最多能完成排序的块-ii
14+
"""
15+
16+
class Solution:
17+
def maxChunksToSorted(self, arr: List[int]) -> int:
18+
stack = []
19+
for val in arr:
20+
if stack and val < stack[-1]:
21+
head = stack.pop()
22+
while stack and val < stack[-1]: stack.pop()
23+
stack.append(head)
24+
else:
25+
stack.append(val)
26+
return len(stack)
27+
28+
mat = Solution()
29+
arr = [2,1,3,4,4]
30+
mat.maxChunksToSorted(arr)

‎LeetCodeSolutions/769.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#==================================================
2+
#==> Title: 769. 最多能完成排序的块
3+
#==> Author: Zhang zhen
4+
#==> Email: hustmatnoble.gmail.com
5+
#==> GitHub: https://github.com/MatNoble
6+
#==> Date: 2/6/2021
7+
#==================================================
8+
9+
"""
10+
https://leetcode-cn.com/problems/max-chunks-to-make-sorted/
11+
"""
12+
13+
from typing import List
14+
class Solution:
15+
def maxChunksToSorted(self, arr: List[int]) -> int:
16+
k = 0
17+
max_ = arr[0]
18+
for idx, val in enumerate(arr):
19+
max_ = max(max_, val)
20+
if max_ <= idx: k += 1
21+
return k
22+
23+
mat = Solution()
24+
arr = [4,3,2,1,0]
25+
arr = [1,0,2,3,4]
26+
mat.maxChunksToSorted(arr)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /