返回题库

每个元素最多出现 K 次的最长子数组

Length of Longest Subarray With at Most K Frequency

专题
Algorithmic Programming / 算法编程
难度
L3
来源
Citadel

题目详情

问题:每个元素最多出现 K 次的最长子数组

考察:数组、哈希表、滑动窗口

来源:DSA Prep / Citadel

链接:https://leetcode.com/problems/length-of-longest-subarray-with-at-most-k-frequency

Problem: Length of Longest Subarray With at Most K Frequency

Patterns: Array, Hash Table, Sliding Window

Recency: 2yr

Link: https://leetcode.com/problems/length-of-longest-subarray-with-at-most-k-frequency

Source: https://www.dsaprep.dev/blog/citadel-coding-interview-questions/

解析

思路:滑动窗口维护每个数字在窗口内的出现次数。右端扩展后若当前数字次数超过 k,就移动左端直到所有频次合法,并更新窗口长度。

复杂度:时间 O(n),空间 O(u)。