Skip to content

Commit

Permalink
feature: partition labels
Browse files Browse the repository at this point in the history
  • Loading branch information
solairerove committed Nov 6, 2023
1 parent 7aaf014 commit e5bc6a0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions arrays/ContainerWithMostWater.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import List

# TODO: two pointers

# O(n) time || O(1) space
def max_area(self, height: List[int]) -> int:
Expand Down
16 changes: 16 additions & 0 deletions arrays/PartitionLabels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from typing import List


# O(n) time || O(n) space
def partition_labels(self, s: str) -> List[int]:
last_occurrence = {c: i for i, c in enumerate(s)}
res = []
low = high = 0
for i, c in enumerate(s):
high = max(high, last_occurrence[c])

if i == high:
res.append(high - low + 1)
low = high + 1

return res
15 changes: 15 additions & 0 deletions arrays/test/test_partition_labels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import unittest

from arrays.PartitionLabels import partition_labels


class MyTestCase(unittest.TestCase):
def test_partition_labels(self):
self.assertEqual([9, 7, 8], partition_labels(self, s="ababcbacadefegdehijhklij"))

def test_partition_labels_1(self):
self.assertEqual([10], partition_labels(self, s="eccbbbbdec"))


if __name__ == '__main__':
unittest.main()

0 comments on commit e5bc6a0

Please sign in to comment.