Leetcode
2020.04.15 14:08

# 692. Top K Frequent Words

조회 수 87 추천 수 0 댓글 0
?

#### 단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

#### 단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

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:

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

1. 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) {
HashMap<String, Integer> hmap = new HashMap<>();
/*
for(int i=0; i<words.length; i++){
if(hmap.containsKey(words[i])){
hmap.put(words[i], hmap.get(words[i])+1);
}else{
hmap.put(words[i], 1);
}
}
*/
for(String word : words){
hmap.put(word, hmap.getOrDefault(word, 0) + 1);
}

//PriorityQueue<Map.Entry<String, Integer>> queue = new PriorityQueue<>(Comparator.comparing(e -> e.getValue()));
PriorityQueue<Map.Entry<String, Integer>> queue = new PriorityQueue<>(new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> item1, Map.Entry<String, Integer> item2){
if(item1.getValue() == item2.getValue()){
return item1.getKey().compareTo(item2.getKey());
}else{
return item2.getValue() - item1.getValue();
}
}
});

for(Map.Entry<String, Integer> item : hmap.entrySet()){
queue.offer(item);
}

List<String> list = new ArrayList<>();
for(int i=0; i<k; i++){
}

return list;
}
}```

```class Solution {
public List<String> topKFrequent(String[] words, int k) {
HashMap<String, Integer> hmap = new HashMap<>();
/* //before
for(int i=0; i<words.length; i++){
if(hmap.containsKey(words[i])){
hmap.put(words[i], hmap.get(words[i])+1);
}else{
hmap.put(words[i], 1);
}
}
PriorityQueue<String> queue = new PriorityQueue<>(
(s1, s2) -> {
if(hmap.get(s1) == hmap.get(s2)){
return s2.compareTo(s1);
}else{
return hmap.get(s1) - hmap.get(s2);
}
}
);
*/

for(String word : words){
hmap.put(word, hmap.getOrDefault(word, 0) + 1);
}

PriorityQueue<String> queue = new PriorityQueue<>(
(s1, s2) -> hmap.get(s1) == hmap.get(s2) ? s2.compareTo(s1) : hmap.get(s1) - hmap.get(s2)
);

for (String word: hmap.keySet()) {
queue.offer(word);
if (queue.size() > k){
queue.poll();
}
}

List<String> list = new ArrayList<>();
for(int i=0; i<k; i++){
}

Collections.reverse(list);

return list;
}
}```

```

class Solution {
public List<String> topKFrequent(String[] words, int k) {
HashMap<String, Integer> hmap = new HashMap();

/* //before
for(int i=0; i<words.length; i++){
if(hmap.containsKey(words[i])){
hmap.put(words[i], hmap.get(words[i])+1);
}else{
hmap.put(words[i], 1);
}
}
List<String> list = new ArrayList(hmap.keySet());
Collections.sort(
list, (s1, s2) -> {
if(hmap.get(s1) == hmap.get(s2)){
return s1.compareTo(s2);
}else{
return hmap.get(s2) - hmap.get(s1);
}
}
);
*/

for (String word: words) {
hmap.put(word, hmap.getOrDefault(word, 0) + 1);
}

Collections.sort(
list, (s1, s2) -> hmap.get(s1) == hmap.get(s2) ? s1.compareTo(s2) : hmap.get(s2) - hmap.get(s1)
);

return list.subList(0, k);
}
}```

?

 제목+내용제목내용댓글이름닉네임아이디태그
List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
» Leetcode 692. Top K Frequent Words 2020.04.15 87
73 Leetcode 2. Add Two Numbers 2020.04.14 90
72 Leetcode 731. My Calendar II 2020.04.15 94
71 Leetcode 75. Sort Colors 2020.04.14 95
70 Leetcode 347. Top K Frequent Elements 2020.04.14 96
69 Leetcode 430. Flatten a Multilevel Doubly Linked List 2020.04.06 98
68 Leetcode 23. Merge k Sorted Lists 2020.04.14 98
67 Leetcode 729. My Calendar I 2020.04.15 98
66 Leetcode 253. Meeting Rooms II 2020.04.15 98
65 Leetcode 20. Valid Parentheses 2020.04.25 98
64 Leetcode 62. Unique Paths 2020.04.09 99
63 Leetcode 7. Reverse Integer 2020.04.28 100
62 Leetcode 946. Validate Stack Sequences 2020.04.08 101
61 Leetcode 973. K Closest Points to Origin 2020.04.15 101
60 Leetcode 852. Peak Index in a Mountain Array 2020.04.28 101
59 Leetcode 350. Intersection of Two Arrays II 2020.04.28 102
58 Leetcode 443. String Compression 2020.04.25 103
57 Leetcode 38. Count and Say 2020.04.25 103
56 Leetcode 53. Maximum Subarray 2020.04.28 106
55 Leetcode 3. Longest Substring Without Repeating Characters 2020.04.09 107
Board Pagination Prev 1 2 3 4 Next
/ 4