일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 | 31 |
Tags
- 30분
- 사이드
- 파비최
- leetcode
- 읽기
- realclass
- 운동
- 화상영어
- 쓰릴오브파이트
- 월간
- Problem Solving
- 미드시청
- 리얼 클래스
- 만화도
- 뭐든
- 프로젝트
- 스탭퍼
- 10분
- 3줄정리
- English
- 영어공부
- Daily Challenge
- FIT XR
- 잡생각
- 링피트
- 매일
- 괜찮음
- 개발자
- Writing
- 영어원서읽기
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.11.21 Today's Challenge 본문
Frequency of the Most Frequent Element - LeetCode
Can you solve this real interview question? Frequency of the Most Frequent Element - The frequency of an element is the number of times it occurs in an array. You are given an integer array nums and an integer k. In one operation, you can choose an index o
leetcode.com
문제를 딱 봤을 때 슬라이딩 윈도우로 푸는거 같았는데, 역시나.
class Solution:
def maxFrequency(self, nums: List[int], k: int) -> int:
nums.sort()
left, ans, curr = 0, 0, 0
for right in range(len(nums)):
target = nums[right]
curr += target
while (right - left + 1) * target - curr > k:
curr -= nums[left]
left += 1
ans = max(ans, right - left + 1)
return ans
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.11.23 Today's Challenge (1) | 2023.11.23 |
---|---|
2023.11.22 Today's Challenge (0) | 2023.11.22 |
2023.11.20 Today's Challenge (1) | 2023.11.20 |
2023.11.19 Today's Challenge (0) | 2023.11.19 |
2023.11.18 Today's Challenge (0) | 2023.11.18 |
Comments