-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_year_chaos.ts
39 lines (32 loc) · 905 Bytes
/
new_year_chaos.ts
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
const minimumBribes = (q: number[]) => {
let count: number = 0;
for (let i = 0; i < q.length; i++) {
const element = q[i];
let diff: number = element - i - 1;
// console.log(element, diff);
if (diff > 2) {
console.log("Too chaotic");
return;
}
if (diff <= 0) {
//check how many bigger numbers there is between the current one and where it should be minus 2
for (let j = Math.max(0, q[i] - 2); j < i; j++) {
if (q[j] > q[i]) {
count++;
}
}
}
}
console.log(count);
};
minimumBribes([1, 2, 5, 3, 7, 8, 6, 4]);
// minimumBribes([2, 1, 5, 3, 4]);
// minimumBribes([2, 5, 1, 3, 4]);
// console.log();
// minimumBribes([1, 2, 3, 4, 5, 8, 7, 6]);
// console.log();
// minimumBribes([1, 2, 5, 3, 4, 7, 8, 6]);
// minimumBribes([1, 2, 5, 3, 7, 8, 4, 6]);
// 1,2,3,4,5,6,7,8
// minimumBribes([1, 2, 5, 4, 3, 6, 7, 8]);
// minimumBribes([1, 5, 2, 4, 3, 6, 7, 8]);