Sorting and Searching
This problem looks tricky at first sight, but is easy if you preprocess the input by sorting it. Once the array is sorted, the minimum sum of four elements and the maximum sum of four elements become obvious.
Sort the array first. After sorting, the four smallest values are at the front and the four largest are at the back. The minimum sum is the sum of the first four elements, and the maximum sum is the sum of the last four elements.
Alternatively, you can compute the total sum of all five elements, then subtract the maximum to get the minimum sum and subtract the minimum to get the maximum sum - no sorting needed at all.
⚙ Knowledge Hub: Searching and Sortingarr[0] + arr[1] + arr[2] + arr[3] (skip the largest)arr[1] + arr[2] + arr[3] + arr[4] (skip the smallest)total = sum(arr), then the minimum sum is total - max(arr) and the maximum sum is total - min(arr). This runs in O(n) instead of O(n log n).This is slightly tricky, because some of the test cases use huge strings of digits. Converting to integers for the sort requires a big integer type, and even then you may time out. It is better to work out a way of comparing the strings without needing this conversion.
Write a custom comparator that compares two numeric strings without converting them to integers. The key insight: if two numeric strings have different lengths, the longer one is always larger (because there are no leading zeroes). If they have the same length, a simple lexicographic (dictionary-order) comparison gives the correct numeric ordering.
⚙ Knowledge Hub: Searching and Sortinga and blen(a) != len(b), the shorter string represents the smaller numbers, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string. Return the sorted string. If there are multiple answers, return any of them.
This is an exercise in sorting using something other than the value as the sort key. It also includes the very common subtask of counting occurrence frequency, which we have seen many times before.
The output string groups identical characters together, with the most frequent characters appearing first.
Use a dictionary (hash map) to count the frequency of each character, then sort the unique characters by their frequency in descending order. Finally, build the result string by repeating each character according to its count.
⚙ Knowledge Hub: Using Dictionaries ⚙ Knowledge Hub: Counting Frequencies ⚙ Knowledge Hub: Searching and SortingCounter and most_common(). In other languages, sort the entries of your frequency map by value descending.nums and an integer k, return the k most frequent elements. You may return the answer in any order.
As with the previous problem, this includes counting occurrence frequencies. You can move from there to the solution using a sort. The challenge task, which is to solve in better than O(n log n) complexity, could use a heap or bucket sort rather than a full sort.
Straightforward approach: count frequencies with a dictionary, sort by frequency descending, and take the first k elements. This is O(n log n).
Faster approach: use a min-heap of size k. After counting frequencies, push each element onto the heap. If the heap exceeds size k, pop the smallest. At the end the heap contains the top k. This is O(n log k).
kk. For each (number, frequency) pair, push onto the heap. If the heap grows beyond k, pop the element with the lowest frequencyk most frequent elements