Участник:UlitinAleksander/partion-labels

Материал из DISCOPAL
Перейти к: навигация, поиск

python3

https://leetcode.com/problems/partition-labels/

class Solution:
    def partitionLabels(self, S: str) -> List[int]:
        p = {}
        for i,c in enumerate(S):
            if c in p:
                p[c] = (p[c][0], i)
            else:
                p[c] = (i,i) 
 
 
        bounderies = sorted(p.values())
 
        partitions = []
 
        i, j = 0,1
        boundary_size = len(bounderies) 
        while j < boundary_size:
            first_x, first_y = bounderies[i]
            second_x, second_y = bounderies[j]
 
            if second_x >= first_x and second_x <= first_y:
                second_x = first_x 
                second_y = max(first_y, second_y) 
                bounderies[j] = (second_x, second_y) 
            else:
                partitions.append((first_y - first_x) + 1)
 
            i+=1
            j+=1
        else:
            partitions.append((second_y-second_x) + 1) 
 
        return partitions