-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1051.c
More file actions
68 lines (53 loc) · 1.09 KB
/
Copy path1051.c
File metadata and controls
68 lines (53 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Height checker
// EASY
#include <stdio.h>
#include <stdlib.h>
void countSort (int A[], int n, int pos) {
int count[10];
int b[n];
for (int i = 0; i < 10; i ++) {
count[i] = 0;
}
for (int i = 0; i < n; i ++) {
count[(A[i] / pos) % 10] ++;
}
for (int i = 1; i < 10; i ++) {
count[i] += count[i - 1];
}
for (int i = n - 1; i >= 0; i --) {
b[--count[(A[i] / pos) % 10]] = A[i];
}
for (int i = 0; i < n; i ++) {
A[i] = b[i];
}
}
void radixSort (int A[], int n) {
int k = 0, max = A[0];
for (int i = 0; i < n; i ++) {
if (A[i] > max) {
max =A[i];
}
}
int temp = max;
while (temp != 0) {
k ++;
temp /= 10;
}
for (int i = 1; max / i > 0; i *= 10) {
countSort(A, n, i);
}
}
int heightChecker(int* heights, int heightsSize) {
int* array = (int*)malloc(100 * sizeof(int));
int count = 0;
for (int i = 0; i < heightsSize; i ++) {
array[i] = heights[i];
}
radixSort(array, heightsSize);
for (int i = 0; i < heightsSize; i ++) {
if (array[i] != heights[i]) {
count ++;
}
}
return count;
}