您的位置:首页 > 产品设计 > UI/UE

LeetCode.347(692) Top K Frequent Elements && Top K Frequent Words

2017-11-02 19:02 405 查看
题目():

Given a non-empty array of integers, return the k most frequent elements.

For example,

Given 
[1,1,1,2,2,3]
 and k = 2, return 
[1,2]
.

Note: 

You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
Your algorithm's time complexity must be better than O(n log n), where n is the array's size.

分析(推荐-参考答案):
class Solution {
public List<Integer> topKFrequent(int[] nums, int k) {
//给定非空数组,返回其出现频率前k个,k满足:1<=k<=不同元素的个数
//注意:要求时间复杂度小于o(nlogn)
//思路:先求出range范围,然后分别求次数,之后List判断是否大于k,决定是否添加进List
List<Integer> list = new ArrayList<>();
List<Integer>[] bucket = new List[nums.length + 1];
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
//求出最小值和最大值
for (int n : nums) {
if (n > max) {
max = n;
}
if (n < min) {
min = n;
}
}
//创建范围数组
int[] range = new int[max - min + 1];
//分别求对应次数
for (int n : nums) {
range[n - min]++;
}

//将元素添加进list,通过下标排好序了
for (int i = 0; i < range.length; i++) {
if (range[i] > 0) {
if (bucket[range[i]] == null) {
bucket[range[i]] = new ArrayList<>();
}
//range[i]表示次数,这里通过将次数多的放在后面,完成排序
bucket[range[i]].add(i + min);
}

}

//倒序求出
for (int i = bucket.length - 1; i >= 0; i--) {
if (list.size() >=k) break;

if (bucket[i] == null) continue;
list.addAll(bucket[i]);

}
return list;
}
}
分析2:

class Solution {
public List<Integer> topKFrequent(int[] nums, int k) {
//给定非空数组,返回其出现频率前k个,k满足:1<=k<=不同元素的个数
//注意:要求时间复杂度小于o(nlogn)
//思路:使用HashMap存入数据,最后对数据进行排序,取前k个

List<Integer> [] bucket=new List[nums.length+1];
HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>();

List<Integer> list=new ArrayList<Integer>();

for(int num:nums){
hm.put(num,hm.getOrDefault(num,0)+1);
}

for(int key:hm.keySet()){
int value=hm.get(key);
//以频率作为值存储
if(bucket[value]==null){
//创建List集合
bucket[value]=new ArrayList<>();
}
//将数据大小作为标识
bucket[value].add(key);
}

//求满足k条件的值
for(int pos=bucket.length-1;pos>=0&&list.size()<k;pos--){
if(bucket[pos]!=null){
list.addAll(bucket[pos]);
}
}

return list;
}
}


题目(692):

Given a non-empty list of words, return the k most frequent elements.

Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower alphabetical order comes first.

Example 1:

Input: ["i", "love", "leetcode", "i", "love", "coding"], k = 2
Output: ["i", "love"]
Explanation: "i" and "love" are the two most frequent words.
Note that "i" comes before "love" due to a lower alphabetical order.


Example 2:

Input: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4
Output: ["the", "is", "sunny", "day"]
Explanation: "the", "is", "sunny" and "day" are the four most frequent words,
with the number of occurrence being 4, 3, 2 and 1 respectively.


Note:

You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
Input words contain only lowercase letters.

Follow up:

Try to solve it in O(n log k) time and O(n) extra space.

分析(原创-易理解):
class Solution {
public List<String> topKFrequent(String[] words, int k) {
//给定单词数组,返回出现最多频率最多的前k个单词,如果存在频率相同的单词,则按单词顺序排序
//要求:输入的单词均为小写,k一定在有效范围呢。时间复杂度O(nlogk),n个额外空间
//思路:使用HashMap<String,Integer>的String作为key,数组下标作为value和List<String>集合数组来存储以频率作为下标

List<String>[]
4000
bucket = new List[words.length + 1];
HashMap<String, Integer> hm = new HashMap<String, Integer>();

for (int i = 0; i < words.length; i++) {
//因为key是String类型,不能用contains这种对int的判断方法判断
hm.put(words[i], hm.getOrDefault(words[i], 0) + 1);
}

//将频数作为下标放入数组
for (String key : hm.keySet()) {
int value = hm.get(key);
//判断是否之前已经存储了单词,没有的话新创建list集合
if (bucket[value] == null) {
bucket[value] = new ArrayList<>();
}
bucket[value].add(key);
}

//将结果输出
List<String> list = new ArrayList<>();
for (int i = bucket.length - 1; i >= 0 && list.size() < k; i--) {
if (bucket[i] != null) {
//说明里面有元素
String[] strs = bucket[i].toArray(new String[bucket[i].size()]);
//将里面的单词排序
Arrays.sort(strs);
for (String word : strs) {
if (list.size() == k) break;
list.add(word);
}
}
}
return list;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: