Given an array of integers nums, sort the array in increasing order based on the frequency of the values. If multiple values have the same frequency, sort them in decreasing order.
Return the sorted array.
Example 1:
Input: nums = [1,1,2,2,2,3] Output: [3,1,1,2,2,2] Explanation: '3' has a frequency of 1, '1' has a frequency of 2, and '2' has a frequency of 3.
Example 2:
Input: nums = [2,3,1,3,2] Output: [1,3,3,2,2] Explanation: '2' and '3' both have a frequency of 2, so they are sorted in decreasing order.
Example 3:
Input: nums = [-1,1,-6,4,5,-6,1,4,1] Output: [5,-1,4,4,-6,-6,1,1,1]
Constraints:
1 <= nums.length <= 100-100 <= nums[i] <= 100When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for sorting by increasing frequency means we try out all possible arrangements of the items. We'll check each arrangement to see if it follows the 'increasing frequency' rule. If it does, we keep it; otherwise, we discard it and try a different arrangement until we find a valid solution.
Here's how the algorithm would work step-by-step:
import itertools
def sort_by_increasing_frequency_brute_force(items):
all_permutations = list(itertools.permutations(items))
valid_solutions = []
for permutation in all_permutations:
item_counts = {}
for item in permutation:
item_counts[item] = item_counts.get(item, 0) + 1
is_sorted_by_frequency = True
frequency = -1
# Check increasing frequency for validity
for item in permutation:
current_frequency = item_counts[item]
if current_frequency < frequency:
is_sorted_by_frequency = False
break
frequency = current_frequency
# Store valid permutation
if is_sorted_by_frequency:
valid_solutions.append(list(permutation))
if valid_solutions:
return valid_solutions[0]
else:
return []The trick to efficiently sorting the array is to count how often each number appears, then use that count to rearrange the numbers. We will sort the numbers based on their frequencies, placing the least frequent numbers first.
Here's how the algorithm would work step-by-step:
def sort_by_frequency(numbers):
frequency_map = {}
for number in numbers:
frequency_map[number] = frequency_map.get(number, 0) + 1
# Sort by frequency, then descending value
sorted_numbers = sorted(numbers, key=lambda number: (frequency_map[number], -number))
# Build the result array based on the sorted frequencies
result = []
number_counts = {}
for number in numbers:
number_counts[number] = number_counts.get(number, 0) + 1
# Ensure correct ordering by frequencies
unique_numbers = []
for number in numbers:
if number not in unique_numbers:
unique_numbers.append(number)
unique_numbers = sorted(unique_numbers, key=lambda number: (frequency_map[number], -number))
# Construct the sorted array
for number in unique_numbers:
result.extend([number] * frequency_map[number])
return result
def sort_array_by_increasing_frequency(numbers):
# Count the frequency of each number.
number_frequencies = {}
for number in numbers:
number_frequencies[number] = number_frequencies.get(number, 0) + 1
# Sort based on frequency then value.
# Important for stability with same frequencies.
sorted_numbers = sorted(numbers, key=lambda number: (number_frequencies[number], -number))
# Create a map to efficiently count processed elements.
seen_numbers = {}
# Sort unique elements based on frequencies and values.
unique_numbers = []
for number in sorted_numbers:
if number not in seen_numbers:
unique_numbers.append(number)
seen_numbers[number] = True
# Construct sorted array.
result_array = []
for number in unique_numbers:
# Use Extend to duplicate numbers the number of times they appear.
result_array.extend([number] * number_frequencies[number])
return result_array| Case | How to Handle |
|---|---|
| Null or empty input array | Return an empty list or throw an IllegalArgumentException, depending on the desired behavior. |
| Array with one element | Return the array itself as it's already sorted. |
| Array with all identical elements | The frequency count will be equal for all elements, resulting in either the original order or any other valid permutation that preserves the original order. |
| Array with maximum possible size allowed by memory constraints | Ensure the frequency counting data structure (e.g., HashMap) has sufficient capacity to handle the number of unique elements efficiently to avoid performance degradation. |
| Array containing negative numbers | The solution should correctly count frequencies and sort based on frequency, regardless of whether numbers are negative or positive. |
| Array with extreme boundary values (e.g., Integer.MAX_VALUE, Integer.MIN_VALUE) | Ensure the frequency count doesn't lead to integer overflow or other data type limitations during count increment or comparison. |
| Array with very large range of numbers, but relatively small number of unique elements | The frequency counting and sorting algorithm should perform efficiently based on the number of unique elements, not the range of values. |
| Multiple elements with the same frequency | Sort the elements with same frequency in decreasing order of their numerical value to achieve desired behavior. |