일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 영어원서읽기
- 3줄정리
- realclass
- 리얼 클래스
- Writing
- leetcode
- 괜찮음
- 영어공부
- 링피트
- FIT XR
- 만화도
- 뭐든
- 파비최
- 매일
- 읽기
- 개발자
- 10분
- Daily Challenge
- 30분
- 잡생각
- 운동
- English
- 화상영어
- 미드시청
- 스탭퍼
- Problem Solving
- 사이드
- 프로젝트
- 월간
- 쓰릴오브파이트
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.02.07 Today's Challenge 본문
https://leetcode.com/problems/fruit-into-baskets/
Fruit Into Baskets - LeetCode
Fruit Into Baskets - You are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array fruits where fruits[i] is the type of fruit the ith tree produces. You want to collect as much frui
leetcode.com
슬라이딩 윈도우 기법이 생각보다 많이 쓰이네 -_-;
class Solution:
def totalFruit(self, fruits: List[int]) -> int:
basket = {}
max_picked = 0
left = 0
for right in range(len(fruits)):
basket[fruits[right]] = basket.get(fruits[right], 0) + 1
while len(basket) > 2:
basket[fruits[left]] -= 1
if basket[fruits[left]] == 0:
del basket[fruits[left]]
left += 1
max_picked = max(max_picked, right-left+1)
return max_picked
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.02.09 Today's Challenge (0) | 2023.02.09 |
---|---|
2023.02.08 Today's Challenge (0) | 2023.02.08 |
2023.02.06 Today's Challenge (0) | 2023.02.06 |
2023.02.05 Today's Challenge (0) | 2023.02.05 |
2023.02.04 Today's Challenge (0) | 2023.02.04 |
Comments