New Year Chaos

There are a number of people queued up, and each person wears a sticker indicating their initial position in the queue. Initial positions increment by 1 from 1 at the front of the line to at the back.

Any person in the queue can bribe the person directly in front of them to swap positions. If two people swap positions, they still wear the same sticker denoting their original places in line. One person can bribe at most two others. For example, if n = 8and Person 5 bribes Person 4, the queue will look like this: 1, 2, 3, 5, 4, 6, 7, 8

Print an integer denoting the minimum number of bribes needed to get the queue into its final state. Print Too chaotic if the state is invalid, i.e. it requires a person to have bribed more than 2 people.

newYears([2, 1, 5, 3, 4]);
newYears([1, 2, 5, 3, 7, 8, 6, 4]);

Notes

  • October 31, 2020

    • This one I had to look up again because it was kicking my ass. I first thought it was an iteration up to a certain maximum and we had to look up and down. But I realized that instead it'd be better if we just look down.

    • This means that it'd be better suited for a different algorithm

    • Here's my initial submission:

    • function newYears(arr: number[]) {
        let counter = 0;
        let chaos = 0;
        for (let i = 0; i < arr.length; i++) {
          if (arr[i] !== i + 1) {
            if (arr[i] === i + 2 || arr[i] === i + 3) {
              const temp = arr[i + 1];
              arr[i + 1] = arr[i];
              arr[i] = temp;
              counter++;
            } else {
              chaos++;
            }
          }
        }
        return chaos > counter ? "Too Chaotic" : counter;
      }
    • This is okay, but the issues surrounding this was that I was swapping. And although that worked for the first test case it wasn't the solution. I shouldn't be swapping but rather just looking ahead and checking if correct.

Last updated