package leetcode;

import java.util.List;

/**
 * https://leetcode.com/problems/top-k-frequent-words/
 */
public class Problem692 {
    public List<String> topKFrequent(String[] words, int k) {
        // TODO
        return null;
    }

    public static void main(String[] args) {
        Problem692 prob = new Problem692();
        System.out.println(prob.topKFrequent(new String[]{
            "i", "love", "leetcode", "i", "love", "coding"}, 2)); // ["i", "love"]
        System.out.println(prob.topKFrequent(new String[]{
            "the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"
        }, 4)); // ["the", "is", "sunny", "day"]
    }
}

@@ -33,13 +33,119 @@
 til.
+Array
 List;
+%0Aimport java.util.Collections;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;
 %0A%0A/*
@@ -305,36 +305,946 @@
     
-// TODO%0A        return null;
+Map%3CString, WordCount%3E wordCounts = new HashMap%3C%3E();%0A        for (String word : words) %7B%0A            if (!wordCounts.containsKey(word)) %7B%0A                wordCounts.put(word, new WordCount(word));%0A            %7D else %7B%0A                wordCounts.get(word).count++;%0A            %7D%0A        %7D%0A        List%3CWordCount%3E values = new ArrayList%3C%3E(wordCounts.values());%0A        Collections.sort(values, (a, b) -%3E %7B%0A            int cmp = Integer.compare(b.count, a.count);%0A            if (cmp == 0) %7B%0A                return a.word.compareTo(b.word);%0A            %7D%0A            return cmp;%0A        %7D);%0A        List%3CString%3E result = new ArrayList%3C%3E();%0A        for (int i = 0; i %3C k; i++) %7B%0A            result.add(values.get(i).word);%0A        %7D%0A        return result;%0A    %7D%0A%0A    private static class WordCount %7B%0A        private final String word;%0A        private int count;%0A%0A        public WordCount(String word) %7B%0A            this.word = word;%0A        %7D
 %0A   

Update problem 692