Re: [閒聊] 每日leetcode

作者: DJYOSHITAKA (Evans)   2024-03-28 20:40:56
2958. Length of Longest Subarray With at Most K Frequency
用window,同時記錄window內的cnt並更新
當cnt>k時,滑動左邊讓window內的cnt再次符合條件
寫起來跟昨天87%像
int maxSubarrayLength(vector<int>& nums, int k) {
int l=0;
int r=0;
int ans = 0;
unordered_map<int,int> cnt;
for(; r<nums.size(); r++)
{
// update cnt
if(cnt.find(nums[r]) != cnt.end())
{
cnt[nums[r]]++;
}
else
{
cnt[nums[r]] = 1;
}
// check
while(cnt[nums[r]]>k && l<=r)
{
cnt[nums[l]]
作者: an94mod0 (an94mod0)   2024-03-28 20:41:00
大師
作者: argorok (s.green)   2024-03-28 20:41:00
大師

Links booklink

Contact Us: admin [ a t ] ucptt.com