代码随想录刷题记录-Day13

To Do List

  • DAY 11 回顾
  • LeetCode 239. 滑动窗口最大值
  • LeetCode 347. 前 K 个高频元素
  • DAY 13 总结

DAY 11 回顾

()


LeetCode 239. 滑动窗口最大值

(带补充)

LeetCode 347. 前 K 个高频元素 / Top K Frequent Elements

1
Difficulty: Medium

Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.

Translated by zh-CN 给你一个整数数组 nums 和一个整数 k ,请你返回其中出现频率前 k 高的元素。你可以按 任意顺序 返回答案。

Example 1:

1
2
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]

Example 2:

1
2
Input: nums = [1], k = 1
Output: [1]

Constraints:

  • 1 <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4
  • k is in the range [1, the number of unique elements in the array].
  • It is guaranteed that the answer is unique.

Follow up: Your algorithm’s time complexity must be better than O(n log n), where n is the array’s size.

Translated by zh-CN k 的取值范围是 [1, 数组中不相同的元素的个数]
题目数据保证答案唯一,换句话说,数组中前 k 个高频元素的集合是唯一的
你所设计算法的时间复杂度 必须 优于 O(n log n) ,其中 n 是数组大小。

解法:

(JavaScript)