-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnew-year-chaos.js
49 lines (41 loc) · 954 Bytes
/
new-year-chaos.js
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
/**
* @title New Year Chaos
* @difficulty Medium
* @link https://www.hackerrank.com/challenges/new-year-chaos/problem
*/
const getDistFromIdx = (line, idx) => {
const num = idx + 1;
let dist = 1;
while (dist <= 2 && line[idx - dist] !== num) {
dist++;
}
return dist;
};
const fixLine = (line, dist, idx) => {
const fixed = line;
const num = line[idx - dist];
for (let i = dist; i > 0; i--) {
fixed[idx - i] = fixed[idx - i + 1];
}
fixed[idx] = num;
return fixed;
};
// Complete the minimumBribes function below.
const minimumBribes = query => {
const {length} = query;
let line = query;
let cnt = 0;
for (let i = length - 1; i >= 0; i--) {
if (line[i] !== i + 1) {
const dist = getDistFromIdx(line, i);
if (dist <= 2) {
cnt += dist;
line = fixLine(line, dist, i);
} else {
console.log('Too chaotic');
return;
}
}
}
console.log(cnt);
};