Find the kth smallest number in at row and column sorted matrix.
Pair class to store x and y index
Put numbers in first column or first row
Poll number and add new number based on previous row, loop k-1 times
classpair{privateint x, y, val;publicpair(int x,int y,int val){this.x= x;this.y= y;this.val= val; }}publicintkthSmallest(int[][] matrix,int k) {int m =matrix.length;int n = matrix[0].length;PriorityQueue<pair> minHeap =newPriorityQueue<>(m, (a, b) ->a.val-b.val);// add numbers in first columnfor(int i=0; i<m ;i++){minHeap.add(newpair(i,0, matrix[i][0])); }for(int i=0; i<k-1; i++){ pair min =minHeap.poll();if(min.y== n-1) continue;minHeap.add(newpair(min.x,min.y+1, matrix[min.x][min.y+1])); }returnminHeap.peek().val;}
373 Find K Pairs with Smallest Sums
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k.
Define a pair (u,v) which consists of one element from the first array and one element from the second array.
Find the k pairs (u1,v1),(u2,v2) ...(uk,vk) with the smallest sums.
Use a minHeap to store sum
Pull out the sum and put new sum based on previous nums1 + 1
defkSmallestPairs(self,nums1: List[int],nums2: List[int],k:int) -> List[List[int]]:ifnot nums1 ornot nums2:return [] m, n =len(nums1),len(nums2) q = [(nums1[i]+nums2[0], i,0) for i inrange(m)]heapify(q) ans = []for i inrange(m*n): s, p1, p2 =heappop(q) ans.append([nums1[p1], nums2[p2]])iflen(ans)== k:breakif p2 == n-1:continueheappush(q, (nums1[p1] + nums2[p2+1], p1, p2+1))return ans
692 Top K frequent word
Order the words by the frequency of them in the return list, the most frequent one comes first. If two words has the same frequency, the one with lower alphabetical order come first.
PriorityQueue: put the String in reverse compareTo order if they have same frequency
Time O(nlogk)
publicString[] topKFrequentWords(String[] words,int k) {if (k ==0|| words ==null||words.length==0){returnnewString[0]; }Map<String,Integer> map =newHashMap<>();for (String word : words){map.put(word,map.getOrDefault(word,0) +1); }Comparator<String> cmp =newComparator<String>(){publicintcompare(String s1,String s2){if (map.get(s1) ==map.get(s2)){returns2.compareTo(s1);// alphabetical order in reverse order in minHeap }else{returnmap.get(s1) -map.get(s2); } } };Queue<String> minHeap =newPriorityQueue<>(k, cmp);for (String word:map.keySet()){minHeap.add(word);if (minHeap.size() > k) minHeap.poll(); }String[] ans =newString[k];for (int i=k-1; i >=0; i--){ ans[i] =minHeap.poll(); }Arrays.sort(ans);return ans;}
Bucket with list List[] bucket, bucket[i] is the frequency of string
Time avg O(nlogk), best O(klogk), worst O(nlogn)
publicList<String>topKFrequent(String[] words,int k) {Map<String,Integer> map =newHashMap<String,Integer>();for (String word:words) {map.put(word,map.getOrDefault(word,0) +1); }int N =words.length+1;List<String>[] bucket =newList[N];for (String key:map.keySet()){int freq =map.get(key);if (bucket[freq] ==null) bucket[freq] =newArrayList<String>(); bucket[freq].add(key); }List<String> ret =newArrayList<String>();for (int i = N -1; i >=0&&ret.size() < k; i--) {if (bucket[i] !=null) {Collections.sort(bucket[i]);ret.addAll(bucket[i]); } }return ((ArrayList)ret).subList(0, k);}
Trie[] bucket, bucket[i] store the strings in same frequency
Time: O(n * avgLenOfWord)
1244. TopK Leaderboard
Design a Leaderboard class, which has 3 functions:
addScore(playerId, score): Update the leaderboard by adding score to the given player's score. If there is no player with such id in the leaderboard, add him to the leaderboard with the given score.
top(K): Return the score sum of the top K players.
reset(playerId): Reset the score of the player with the given id to 0 (in other words erase it from the leaderboard). It is guaranteed that the player was added to the leaderboard before calling this function.