파비의 매일매일 공부기록

2023.11.21 Today's Challenge 본문

Problem Solving/LeetCode

2023.11.21 Today's Challenge

fabichoi 2023. 11. 21. 23:45

https://leetcode.com/problems/frequency-of-the-most-frequent-element/?envType=daily-question&envId=2023-11-18

 

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