1+ package my .algorithm .string ;
2+
3+ import java .util .ArrayList ;
4+ import java .util .Collection ;
5+ import java .util .Collections ;
6+ import java .util .HashMap ;
7+ import java .util .List ;
8+ import java .util .Map ;
9+ import java .util .PriorityQueue ;
10+
11+ class TopKFrequent {
12+ public static List <String > topKFrequentSort (String [] words , int k ) {
13+ List <String > result = new ArrayList <>();
14+ Map <String , WordAndCount > wordAndCountMap = new HashMap <>();
15+ for (String s : words ) {
16+ if (wordAndCountMap .get (s ) != null ) {
17+ wordAndCountMap .get (s ).increaseCount ();
18+ } else {
19+ wordAndCountMap .put (s , new WordAndCount (s ));
20+ }
21+ }
22+ Collection <WordAndCount > wordAndCounts = wordAndCountMap .values ();
23+ ArrayList <WordAndCount > wordAndCountList = new ArrayList <WordAndCount >(wordAndCounts );
24+ Collections .sort (wordAndCountList );
25+ for (WordAndCount wordAndCount : wordAndCountList .subList (0 , k )) {
26+ result .add (wordAndCount .word );
27+ }
28+ return result ;
29+ }
30+
31+ public static List <String > topKFrequentHeap (String [] words , int k ) {
32+ List <String > result = new ArrayList <>();
33+ Map <String , WordAndCount > wordAndCountMap = new HashMap <>();
34+ for (String s : words ) {
35+ if (wordAndCountMap .get (s ) != null ) {
36+ wordAndCountMap .get (s ).increaseCount ();
37+ } else {
38+ wordAndCountMap .put (s , new WordAndCount (s ));
39+ }
40+ }
41+
42+ PriorityQueue <WordAndCount > heap = new PriorityQueue <WordAndCount >(wordAndCountMap .values ());
43+ for (int i =0 ; i <k ; i ++) {
44+ result .add (heap .poll ().word );
45+ }
46+
47+ return result ;
48+ }
49+
50+ public static void main (String [] args ) {
51+ // System.out.println(topKFrequentSort(new String[] {"i", "love", "leetcode", "i", "love", "coding"}, 2));
52+ // System.out.println(topKFrequentHeap(new String[] {"the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"}, 2));
53+
54+ String [] array1 = new String [] {"the" , "day" , "is" , "sunny" , "the" , "the" , "the" , "sunny" , "is" , "is" };
55+ String [] array2 = new String [] {"i" , "love" , "leetcode" , "i" , "love" , "coding" };
56+ {
57+ long start = System .currentTimeMillis ();
58+ for (int i =0 ; i <100000 ; i ++) {
59+ topKFrequentHeap (array1 , 4 );
60+ topKFrequentHeap (array2 , 3 );
61+ topKFrequentHeap (array1 , 2 );
62+ topKFrequentHeap (array2 , 2 );
63+ }
64+ long end = System .currentTimeMillis ();
65+ System .out .println (end -start );
66+ }
67+ {
68+ long start = System .currentTimeMillis ();
69+ for (int i =0 ; i <100000 ; i ++) {
70+ topKFrequentSort (array1 , 4 );
71+ topKFrequentSort (array2 , 3 );
72+ topKFrequentSort (array1 , 2 );
73+ topKFrequentSort (array2 , 2 );
74+ }
75+ long end = System .currentTimeMillis ();
76+ System .out .println (end -start );
77+ }
78+
79+ }
80+ }
81+
82+ class WordAndCount implements Comparable <WordAndCount > {
83+ int count = 1 ;
84+ String word ;
85+ public WordAndCount (String s ) {
86+ this .word = s ;
87+ }
88+ public void increaseCount () {
89+ this .count ++;
90+ }
91+ @ Override
92+ public int compareTo (WordAndCount o ) {
93+ if (this .count == o .count ) {
94+ return this .word .compareTo (o .word );
95+ }
96+ return this .count < o .count ? 1 : -1 ; //desend
97+ }
98+ @ Override
99+ public boolean equals (Object o ) {
100+ if (o == null ) return false ;
101+ if (o instanceof WordAndCount ) {
102+ return ((WordAndCount )o ).word .equals (this .word );
103+ }
104+ return false ;
105+ }
106+ @ Override
107+ public String toString () {
108+ return this .word +":" +this .count ;
109+ }
110+ }
0 commit comments