LeetCode version Problem
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 ListLintCode version ProblemtopKFrequent(String[] words, int k) { List res = new ArrayList<>(); if (words.length < k) return res; Map map = new HashMap<>(); for (String word: words) { if (!map.containsKey(word)) map.put(word, 1); else map.put(word, map.get(word)+1); } PriorityQueue > queue = new PriorityQueue<>( (a, b) -> a.getValue() == b.getValue() ? b.getKey().compareTo(a.getKey()) : a.getValue() - b.getValue() ); for (Map.Entry entry: map.entrySet()) { queue.offer(entry); if (queue.size() > k) queue.poll(); } while (!queue.isEmpty()) { res.add(0, queue.poll().getKey()); } return res; } }
Find top k frequent words with map reduce framework.
The mapper"s key is the document id, value is the content of the document, words in a document are split by spaces.
For reducer, the output should be at most k key-value pairs, which are the top k words and their frequencies in this reducer. The judge will take care about how to merge different reducers" results to get the global top k frequent words, so you don"t need to care about that part.
The k is given in the constructor of TopK class.
NoticeFor the words with same frequency, rank them with alphabet.
/** * Definition of OutputCollector: * class OutputCollectorExample{ * public void collect(K key, V value); * // Adds a key/value pair to the output buffer * } * Definition of Document: * class Document { * public int id; * public String content; * } */
Given document A =
lintcode is the best online judge
I love lintcode
and document B =
lintcode is an online judge for coding interview
you can test your code online at lintcode
The top 2 words and their frequencies should be
lintcode, 4
online, 3
Map Reduce
Solution// Use Pair to store k-v pair class Pair { String key; int value; Pair(String k, int v) { this.key = k; this.value = v; } } public class TopKFrequentWords { public static class Map { public void map(String _, Document value, OutputCollectoroutput) { // Output the results into output buffer. // Ps. output.collect(String key, int value); String content = value.content; String[] words = content.split(" "); for (String word : words) { if (word.length() > 0) { output.collect(word, 1); } } } } public static class Reduce { private PriorityQueue Q = null; private int k; private Comparator pairComparator = new Comparator () { public int compare(Pair o1, Pair o2) { if (o1.value != o2.value) { return o1.value - o2.value; } //if the values are equal, compare keys return o2.key.compareTo(o1.key); } }; public void setup(int k) { // initialize your data structure here this.k = k; Q = new PriorityQueue (k, pairComparator); } public void reduce(String key, Iterator values) { int sum = 0; while (values.hasNext()) { sum += values.next(); } Pair pair = new Pair(key, sum); if (Q.size() < k) { Q.add(pair); } else { Pair peak = Q.peek(); if (pairComparator.compare(pair, peak) > 0) { Q.poll(); Q.add(pair); } } } public void cleanup(OutputCollector output) { // Output the top k pairs into output buffer. // Ps. output.collect(String key, Integer value); List pairs = new ArrayList (); while (!Q.isEmpty()) { pairs.add(Q.poll()); } // reverse result int n = pairs.size(); for (int i = n - 1; i >= 0; --i) { Pair pair = pairs.get(i); output.collect(pair.key, pair.value); } // while (!Q.isEmpty()) { // Pair pair = Q.poll(); // output.collect(pair.key, pair.value); // } } } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/68159.html
Problem Given two sentences words1, words2 (each represented as an array of strings), and a list of similar word pairs pairs, determine if two sentences are similar. For example, great acting skills a...
摘要:使用,利用其按層次操作的性質(zhì),可以得到最優(yōu)解。這樣可以保證這一層被完全遍歷。每次循環(huán)取出的元素存為新的字符串。一旦找到和相同的字符串,就返回轉(zhuǎn)換序列長(zhǎng)度操作層數(shù),即。 Problem Given two words (start and end), and a dictionary, find the length of shortest transformation sequence...
摘要:描述給定一個(gè)非空的整數(shù)數(shù)組,返回其中出現(xiàn)頻率前高的元素。然后以元素出現(xiàn)的次數(shù)為值,統(tǒng)計(jì)該次數(shù)下出現(xiàn)的所有的元素。從最大次數(shù)遍歷到次,若該次數(shù)下有元素出現(xiàn),提取該次數(shù)下的所有元素到結(jié)果數(shù)組中,知道提取到個(gè)元素為止。 Description Given a non-empty array of integers, return the k most frequent elements. E...
Problem Given a non-empty array of integers, return the k most frequent elements. 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 e...
摘要:題目要求假設(shè)有一個(gè)非空的整數(shù)數(shù)組,從中獲得前個(gè)出現(xiàn)頻率最多的數(shù)字。先用來統(tǒng)計(jì)出現(xiàn)次數(shù),然后將其丟到對(duì)應(yīng)的桶中,最后從最高的桶開始向低的桶逐個(gè)遍歷,取出前個(gè)頻率的數(shù)字。 題目要求 Given a non-empty array of integers, return the k most frequent elements. For example, Given [1,1,1,2,2,...
閱讀 1259·2021-11-11 16:54
閱讀 1780·2021-10-13 09:40
閱讀 976·2021-10-08 10:05
閱讀 3536·2021-09-22 15:50
閱讀 3741·2021-09-22 15:41
閱讀 1892·2021-09-22 15:08
閱讀 2376·2021-09-07 10:24
閱讀 3603·2019-08-30 12:52