Sort by

recency

|

356 Discussions

|

  • + 1 comment

    I use a quite intuitive approach to construct solution in Python. To sort a list using swapping among 3 numbers, one may begin with the smallest number, and moving it forward to the beginning of the list every time by 2 numbers. For example: [5,4,3,2,1] --> [5,4,1,3,2] --> [1,5,4,3,2]. In this case by this iteration alone can bring the smallest number to the front. But in cases like [4,3,2,1], it will become [4,1,3,2], the last swapping moving the first number to third position, ie [1,3,4,2]. By popping out the smallest number(1), we can repeat the procedure to the second smallest(2). At the end there will be two numbers remaining, if first one is smaller than the second, the whole list is sortable, otherwise no.

    So the following code is constructed:

    def larrysArray(A):
        while len(A) > 2:
            smallest = A.index(min(A))
            while  smallest != 0:
                if smallest >= 2:
                    A = A[:smallest - 2] + [A[smallest]] + A[smallest - 2:smallest] + A[smallest + 1:]
                elif smallest == 1:
                    A = A[1:3] + [A[0]] + A[3:]
                smallest = A.index(min(A))
            A.pop(0)
        return 'YES' if A[0] < A[1] else 'NO'
    

    The code works but fails about half of the tests due to time limit issue. A review concludes that the repeated reconstructions of the list by moving the smallest number forward 2 positions is unnecessary. At the end either the smallest number, if its index is even, can be moved to position 0 without disturbing other numbers, or it can be moved to position 1 and swap the position 0 number to position 2. The following rewritten code successfully passes all the tests:

    def larrysArray(A):
        while len(A) > 2:
            smallest = A.index(min(A))
            if smallest % 2 == 0:
                A = [A[smallest]] + A[:smallest] + A[smallest + 1:]
            else:
                if smallest > 1:
                    A = [A[0]] + [A[smallest]] + A[1:smallest] + A[smallest + 1:]
                A = A[1:3] + [A[0]] + A[3:]
            A.pop(0)
        return 'YES' if A[0] < A[1] else 'NO'
    
  • + 0 comments

    Using Python :

    def larrysArray(A): # Write your code here c=0 for i in range(0,len(A)-1): for j in range(i,len(A)): if(A[i]>A[j]): c+=1 if(c%2==0): return "YES" else: return "NO"

  • + 3 comments

    my answer in Typescript

    const NO = 'NO', YES = 'YES';
    function larrysArray(A: number[]): string {
        /**
         * idea: create a [num] increament from 1, find index that contains [num] index and 
         *      always larger than 2 be cause i will rotate [index,index-2,index-1] to move
         *      [num] toward start of array. once [num] is at start of array, [num] increate
         *      and shift array. re do this till array have 2 element, if 2 element is not
         *      incrementing by 1, return NO else YES.
         */
    
        const rotate = (A: number[], index: number): number[] => {
            const a = [...A]
            return [...a.slice(0, index - 2), ...[a[index], a[index - 2], a[index - 1]], ...A.slice(index + 1)]
        }
    
        let is_larry_array = NO
        let num = 1
    
        while (true) {
            if (A.length < 3) {
                if (A[0] == A[1] - 1) is_larry_array = YES
                break
            }
    
            if (A[0] == num) { A.shift(); num++; continue }
    
            let index = A.indexOf(num); while (index < 2) index++
    
            A = rotate(A, index)
        }
    
        return is_larry_array
    }
    
  • + 0 comments

    string larrysArray(vector A) { int counter =0; for(int i=0;iA[j]){ counter++; } } } return (counter%2==0) ? "YES" : "NO"; } jinx manga

  • + 0 comments

    string larrysArray(vector A) { int counter =0; for(int i=0;iA[j]){ counter++; } } } return (counter%2==0) ? "YES" : "NO"; }