new year chaos solution javascript code example
Example: New Year Chaos javascript
1 function minimumBribes(q) { 2 let swaps = 0 3 4 for (let i = 0; i < q.length; i++) { 5 let bribes = q[i] - (i + 1) 6 let maxAdvance = q[i] - 2 > 0 ? q[i] - 2 : 0 7 8 if (bribes > 2) { 9 console.log('Too chaotic')10 return11 }1213 for (let j = maxAdvance; j < i; j++) {14 if (q[j] > q[i]) swaps++15 }16 }17 console.log(swaps)18 }1920 ///////////////////////////////////////////21 22 const q = [1, 2, 5, 3, 7, 8, 6, 4]23 minimumBribes(q)24 // => 72526 ///////////////////////////////////////////2728 const q = [5, 1, 2, 3, 7, 8, 6, 4]29 minimumBribes(q)30 // => Too chaotic