[파이썬] 백준 30804 : 과일탕후루 (실버2)
풀이
방향성 생각
- 각 시작지점마다 가능한 길이를 찾는다.
- $NlogN$이하로 해야하므로, 투포인터로 찾는다.
- 각 과일 개수를 카운팅해줘야 하므로 딕셔너리 하나 사용해주기
전체코드
from collections import defaultdict as dd
N = int(input())
arr = list(map(int,input().split()))
l,r,count = 0,0,0
infos = dd(int)
answer = 0
while r<N:
if infos[arr[r]] == 0:
count += 1
infos[arr[r]] += 1
while count > 2:
infos[arr[l]] -= 1
if infos[arr[l]] == 0:
count -= 1
l += 1
answer = max(answer,r-l+1)
r += 1
print(answer)
코멘트
- 보통 이정도는 골5같은데 대회나가는 사람들이 풀어서 좀 내려간듯?
'Algorithm > etc' 카테고리의 다른 글
[파이썬] 백준 2473 : 세 용액 (골드3) (0) | 2024.08.04 |
---|---|
[파이썬] 백준 1806 : 부분합 (골드4) (0) | 2024.08.02 |
[파이썬] 백준 11660 : 구간 합 구하기 5 (실버1) (0) | 2024.08.02 |
[파이썬] 프로그래머스 : 유사 칸토어 비트열 (레벨3) (0) | 2024.06.10 |
[파이썬] 프로그래머스 : 점 찍기 (레벨2) (0) | 2024.06.10 |
[파이썬] 프로그래머스 : 가장 긴 팰린드롬 (레벨3) (0) | 2024.06.10 |
[파이썬] 백준 24041 : 성싶당 밀키트 (골드4) (0) | 2024.06.05 |
댓글