Skip to content

Commit ef11743

Browse files
committed
update 763
1 parent 6b5b552 commit ef11743

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,7 @@ For question details, please go to [Leetcode](https://leetcode.com/problemset/al
4646
6. [Tree](https://github.com/KaidiGuo/Algorithm-Exercises/tree/master/Linked%20List)
4747
+ Medium 105 Construct Binary Tree from Preorder and Inorder Traversal -- recursive
4848

49+
7. [Two Pointers](https://github.com/KaidiGuo/Algorithm-Exercises/tree/master/Linked%20List)
50+
+ Medium 763 Partition Labels
4951

5052

Two Pointers/763. Partition Labels.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def partitionLabels(self, s: str) -> List[int]:
3+
p1,p2=0,0
4+
most_right = { c : i for i,c in enumerate(s)}
5+
result=[]
6+
for i,c in enumerate(s):
7+
p2 = max(p2, most_right[c])
8+
if i==p2:
9+
result.append(p2-p1+1)
10+
p1=i+1
11+
return result
12+
13+
# Create a dictionary, keep track of the right most index of each character
14+
# Create two pointers, p1 and p2, starting from 0
15+
# Loop through S
16+
# Inside the loop, keep p2 always at the right most index of all looped characters (using max(p2,most_right{i}))
17+
# When i fininally catchs up p2, means all of the looped characters, their last appearences are smaller than p2
18+
# You find the first segmentation/partition, now move p1 to i+1 for nect segmentation/partition

0 commit comments

Comments
 (0)