763. Partition Labels

2021. 4. 13. 11:25·Algorithm
 

Partition Labels - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

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  (2) 2021.04.11
338. Counting Bits  (0) 2021.04.09
310. Minimum Height Trees  (0) 2021.04.08
'Algorithm' 카테고리의 다른 글
  • 283. Move Zeroes
  • 787. Cheapest Flights Within K Stops
  • 743. Network Delay Time
  • 338. Counting Bits
Doljae
Doljae
  • Doljae
    Zero to Hero
    Doljae
  • 전체
    오늘
    어제
    • 분류 전체보기 (350)
      • Programming (54)
      • Algorithm (161)
      • Review (103)
      • Career (8)
      • Diary (18)
      • Shorts (4)
      • Temp (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
    • 글 쓰기
    • 관리
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    한빛미디어
    나는리뷰어다
    2023
    컨퍼런스
    나는 리뷰어다
    mysql
    프로그래머스
    AI
    leetcode
    BOJ
    database
    jpa
    공채
    2021
    sql
    2022
    PYTHON
    db
    개발자
    라인
    ChatGPT
    백준
    인프콘
    sql튜닝
    line
    회고
    java
    코딩테스트
    면접
    코딩
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.4
Doljae
763. Partition Labels
상단으로

티스토리툴바