New Year Chaos

Sort by

recency

|

28 Discussions

|

  • + 0 comments

    How can the expected output from this q be 7?

    q = [1, 2, 5, 3, 7, 8, 6, 4]

  • + 0 comments

    Iterate from right to left, if you have seen 3 values lower than the current it's too chaotic, othewise add the seen list index to the bribe count.

    • j = 0, the value is smaller than any seen
    • j = 1, you have seen one value smaller
    • j = 2, you have seen 2 values smaller
    • j = 3 you have seen at least 3 values smaller, which is not valid.

    bisect used to maintain order, only need to keep the 3 smallest values seen.

    from bisect import bisect_left
    
    def minimumBribes(q):
        
        bribes = 0
        seen = []
        for i in range(len(q)-1, -1, -1):
            v = q[i]
            j = bisect_left(seen, v)
            if j >= 3:
                print('Too chaotic')
                return
            bribes += j
            seen.insert(j, v)
            seen = seen[:3]
                 
        print(bribes)
    
  • + 0 comments

    Python3: another O(N) (??? maybe ???)

    def minimumBribes(q):
        seen = set()
        bribes = {n: set() for n in range(0, len(q) + 1)}
        for n in q:
            seen.add(n)
            m = n - 1
            while m > 0 and m not in seen:
                bribes[n].add(m)
                m -= 1
            bribes[n] |= (bribes[m] - seen)
            if len(bribes[n]) > 2:
                return print('Too chaotic')
            
        print(sum(len(v) for v in bribes.values()))
    
  • + 0 comments

    Python - the key for me was to think about 'how many people to the right have numbers smaller than me' as those people must have been overtaken (bribed). Then it became a matter of doing the check in a fast enough way to handle the test cases...

    def minimumBribes(q):
        
        def find_smaller_than(n, sorted_list):
            # fast (enough) as we stop looking once n > n_bigger
            smaller_than = 0
            for number in sorted_list:
                if number < n:
                    smaller_than += 1
                else:
                    return smaller_than
            return smaller_than
    
        bribes = 0
        remaining_queue = sorted(q)
        for person in q:
            remaining_queue.remove(person)
            overtaken = find_smaller_than(person, remaining_queue)
            if overtaken > 2:
                print("Too chaotic")
                return
            bribes += overtaken
        print(bribes)
    
  • + 0 comments

    C#

        public static void minimumBribes(List<int> q)
        {
            int b = 0;
            for (int i = q.Count - 1; i > 0; i--)
            {
                for (int j = i; j > i - 2 && j > 0; j--)
                {
                    if (q[i] < q[j - 1])
                    {
                        (q[i], q[j - 1]) = (q[j - 1], q[i]);
                        b++;
                    }
                    
                }
                if (q[i] != i+1)
                {
                    Console.WriteLine("Too chaotic");
                    return;
                }
            }
            Console.WriteLine(b);
        ``}