일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- Writing
- 스탭퍼
- 30분
- 3줄정리
- 파비최
- 영어공부
- 뭐든
- 읽기
- 괜찮음
- Daily Challenge
- 리얼 클래스
- leetcode
- English
- 운동
- 쓰릴오브파이트
- 10분
- 영어원서읽기
- 매일
- 만화도
- 개발자
- 사이드
- FIT XR
- Problem Solving
- 화상영어
- 프로젝트
- 링피트
- 월간
- 잡생각
- realclass
- 미드시청
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