-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssesmentMC.js
More file actions
41 lines (30 loc) · 976 Bytes
/
AssesmentMC.js
File metadata and controls
41 lines (30 loc) · 976 Bytes
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
function solution(A) {
let indexA = A.map((height, index) => ({ height, index }));
indexA.sort((x, y) => y.height - x.height);
let result = new Array(A.length);
let height = A.length;
for (let i = 0; i < indexA.length; i++) {
result[i] = Math.min(height, indexA[i].height);
height--;
}
return result;
}
console.log(solution([1, 2, 3]));
console.log(solution([9, 4, 3, 7, 7]));
console.log(solution([2, 5, 4, 5, 5]));
function solution(A) {
let indexA = A.map((height, index) => ({ height, index }));
indexA.sort((x, y) => y.height - x.height);
let B = new Array(A.length);
let heights = new Set();
for (let i = 0; i < indexA.length; i++) {
let maxHeight = indexA[i].height;
let assingHeight = maxHeight;
while (heights.has(assingHeight)) {
assingHeight++;
}
B[indexA[i].index] = assingHeight;
heights.add(assingHeight);
}
return B;
}