Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. function minimumBribes(q) {
  2. console.log(sort(q));
  3.  
  4. function sort(items) {
  5. let bribes = 0;
  6.  
  7. for (let i = 0; i < items.length; i++) {
  8. if (items[i] - (i + 1) > 2) return "Too chaotic";
  9. for (let j = 0; j < i; j++) {
  10. if (items[j] > items[i]) bribes++;
  11. }
  12. }
  13. return bribes;
  14. }
  15. }
  16.  
  17. minimumBribes = q => {
  18. const bribes = q.reduce( (bribes, assigned, actual, too_chaotic) => {
  19. let distance = assigned - 1 - actual
  20. if (distance>2) too_chaotic=true
  21. else return bribes + distance*(distance>0)
  22. }, 0);
  23. return isNaN(bribes) ? "Too chaotic" : bribes;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement