1. 문자의 첫 출현, 마지막 출현 정보 + Queue를 이용한 선형탐색
from typing import *
from collections import deque, defaultdict
class Solution:
def partitionLabels(self, s: str) -> List[int]:
answer = []
q = []
dict1 = defaultdict(list)
for i in range(len(s)):
if len(dict1[s[i]]) > 1:
dict1[s[i]].pop()
dict1[s[i]].append(i)
for key in dict1:
if len(dict1[key]) == 1:
dict1[key] = [dict1[key][0]] * 2
q.append(dict1[key])
q.sort()
q = deque(q)
while q:
cur = q.popleft()
while q and q[0][1] < cur[1]:
q.popleft()
if q and cur[1] > q[0][0]:
continue
else:
answer.append(cur[1])
aanswer = [answer[0]+1]
for i in range(1, len(answer)):
aanswer.append(answer[i] - answer[i - 1])
return aanswer
'Algorithm' 카테고리의 다른 글
283. Move Zeroes (0) | 2021.04.15 |
---|---|
787. Cheapest Flights Within K Stops (0) | 2021.04.14 |
743. Network Delay Time (0) | 2021.04.11 |
338. Counting Bits (0) | 2021.04.09 |
310. Minimum Height Trees (0) | 2021.04.08 |