# A headline algorithm problem, an unknown solution!

Posted by thinfile on Sat, 19 Feb 2022 09:37:24 +0100

### subject

Enter the integer array arr to find the minimum number of k. For example, if you enter 8 numbers: 4, 5, 1, 6, 2, 7, 3 and 8, the smallest 4 numbers are 1, 2, 3 and 4.

Example 1:

Input: arr = [3,2,1], k = 2 output: [1,2] or [2,1] example 2:

Input: arr = [0,1,2,1], k = 1, output: [0]

Restrictions:

0 <= k <= arr.length <= 10000 0 <= arr[i] <= 10000

Xiao Xi: there is another solution to this problem, which is counting and sorting. Let's talk about counting and sorting first

# Count sort

### Count sort animation

Counting and sorting graphic Edition

### Count sort text version

• During initialization, the subscript count of the count array is equal to 0, It means that each element appears 0 times.

• The first element 2 is placed where the subscript is 2. Since it only appears once, the count value with subscript 2 + 1

• The second element 2 is placed at the position where the subscript is 2. Count count and then + 1 becomes 2, indicating that 2 appears twice.

• The third element 1 is placed at the position where the subscript is 1. The count with subscript 1 is + 1 and becomes 1, indicating that 1 occurs once.

• The fourth element 0 is placed at the position where the subscript is 0. The count with subscript 0 is + 1 to 1, which means that 0 occurs once.

• The fifth element 1 is placed at the position where the subscript is 1. The count with subscript 1 is + 1 and becomes 2, indicating that 1 occurs twice.

• The sixth element 5 is placed at the position where the subscript is 5. The count with subscript 5 is + 1 and becomes 1, indicating that 5 appears once

• Next, process the count array, traverse the count array, and empty the original array for convenience of understanding.

• The traversal index is 0, and the count array value is 1, which means that 0 occurs once, so let 0 occupy the 1 bit of the original array.

• The traversal index is 1, and the count array value is 2, which means that 1 occurs twice, so let 1 occupy 2 bits of the original array.

• The traversal index is 2, and the count array value is 2, which means that 2 occurs twice, so let 2 occupy the 2 bits of the original array.

• The traversal index is 3, and the count array value is 0, which means that 3 occurs 0 times, so it does not occupy a bit

• The traversal index is 4, and the count array value is 0, which means that 4 occurs 0 times, so it does not occupy a bit

• The traversal index is 5, and the count array value is 1, which means that 5 occurs once, so 5 occupies 1 bit of the original array.

• Come here. The original array has become orderly!

# code

Java

```class Solution {
public int[] getLeastNumbers(int[] arr, int k) {
if (k == 0 || arr.length == 0) {
return new int[0];
}
// Count the number of occurrences of each number
int[] counter = new int[10001];
for (int num: arr) {
counter[num]++;
}
// Find k numbers from the beginning according to the counter array as the return result
int[] res = new int[k];
int idx = 0;
for (int num = 0; num < counter.length; num++) {
while (counter[num]-- > 0 && idx < k) {
res[idx++] = num;
}
if (idx == k) {
break;
}
}
return res;
}
}```

### C++

```class Solution {
public:
vector<int> getLeastNumbers(vector<int>& arr, int k) {
vector<int> temp(10000);  //The value in arr will not be greater than 10000 or less than 0, so apply for an integer array of 10000 size
for(const int& val : arr)  //The array subscript val corresponds to the number of val in arr, and temp[val] corresponds to the number of val in arr
++temp[val];
vector<int> result;  //Results to return
int count = 0;  //count
for(int i = 0; i < temp.size(); ++i)  //Traverse temp from front to back, that is, traverse val from small to large
{
if(count >= k)  //If the number stored in the result to be returned is greater than or equal to k, jump out of the loop
break;
if(temp[i])  //temp[i] is not 0, indicating that there is data there
{
while(temp[i]--)  //If several values in arr are i, put several data into result
{
result.push_back(i);
count++;  //count
}
}
}
result.resize(k);
return result;
}
};```

### Python

```def get_least_numbers(arr, k):
nums = [0] * 10000  # Request an element that contains the maximum number of elements
for a in arr:
nums[a] += 1  # Count duplicate elements
output = []
i = 0  # Traverse from 0 (traverse from minimum)
while len(output) < k:
if nums[i] >= 1:  # If the value at the index is greater than 1, it indicates that the value exists at least once, and the loop writes to the output
for j in range(nums[i]):
output.append(i)
i += 1
return output[:k]  # Be sure to pay attention to the situation that there are more than k single repeated elements```