-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnew-year-chaos.js
More file actions
74 lines (58 loc) · 1.44 KB
/
new-year-chaos.js
File metadata and controls
74 lines (58 loc) · 1.44 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
69
70
71
72
/*
*
* ------------------
* | NEW YEAR CHAOS | INCOMPLETE
* ------------------
*
* minimumBribes takes in an array of integers
* each integer represents a person in a queu
* the value of each integer is the starting position
* of each of the persons in line each person in line can
* bribe the the person right infront of them to
* switch spots a person can only bribe a maximum
* of 2 times
*
* complete minimumBribes so that it returns the
* minimum number of bribes that is needed to reach
* the current order of q if the q given cannot be made
* without violating the ruls log "too chaotic!"
*
*
* */
/*
*
* ***** SOLUTION
*
* */
function minimumBribes(q) {
let bribes = 0;
for(let i = q.length - 1; i >= 0; i--) {
const difference = q[i] - (i + 1);
if(difference > 2) {
console.log("too chaotic");
return
}
// console.log(`q.slice(0, q[i] -- ${q.slice(0, q[i])}`))
//console.log( `j -- ${ q.slice( 0, (q[i] - 1) )}` );
for(let j = 1; j < i; j++) {
// console.log("j", j);
// console.log("q[j]", q[j]);
// console.log("q[i]", q[i]);
if(q[j] > q[i]) {
bribes ++;
};
};
};
console.log("bribes", bribes);
}
/*
*
* ***** TESTS
*
* */
const queu1 = [1, 2, 5, 4, 3]; // 2
const queu2 = [5, 1, 2, 3, 7, 8, 6, 4]; // Too chaotic
const queu3 = [1, 2, 5, 3, 7, 8, 6, 4]; // 7
// minimumBribes(queu1); // 2
// minimumBribes(queu2); // Too chattic
minimumBribes(queu1); // 7