DEV Community

kevin074
kevin074

Posted on

Leetcode diary: 950. Reveal Cards In Increasing Order

This is a new series where I document my struggles of leetcode questions hoping seeing however small of an audience I get gives me the motivation to continue.

This is first of many to come problems that I did not know how to solve.

I'll post my code here:

/**
 * @param {number[]} deck
 * @return {number[]}
 */
var deckRevealedIncreasing = function(deck) {
    if(deck.length ===1) return deck;

    deck = deck.sort(function(a,b){
        return a > b ? 1 : -1; 
    });
    let orderedN = Math.ceil(deck.length/2);
    let numInverts = deck.length-2;

    const orderedDeck = deck.slice(0,orderedN);
    const rotateDeck = deck.slice(orderedN);

    let finalUnordered = [];
    while (numInverts--) {
        if(!orderedN) {
            finalUnordered.push(rotateDeck.shift());
        }
        rotateDeck.push(rotateDeck.shift());
        orderedN--;
    }
    finalUnordered = finalUnordered.concat(rotateDeck);

    const finalArray = [];
    orderedN = Math.ceil(deck.length/2);
    while(orderedN--) {
        finalArray.push(orderedDeck.shift());

        if(finalUnordered.length) {
            finalArray.push(finalUnordered.shift());
        }
    };

    return finalArray;
};
Enter fullscreen mode Exit fullscreen mode

Note that this is wrong. My thought process is that I know for Math.ceil(length/2) number of elements, they are simply the ordered array elements that are in 0,2,4... positions.

The problem was that i did not know how to handle the pushed to the back elements. I tried many things and came up with the mess in the middle of the code for the finalUnordered array. It did not turn up well albeit passing a couple test cases.

This is the code in the solution in Java:

class Solution {
    public int[] deckRevealedIncreasing(int[] deck) {
        int N = deck.length;
        Deque<Integer> index = new LinkedList();
        for (int i = 0; i < N; ++i)
            index.add(i);

        int[] ans = new int[N];
        Arrays.sort(deck);
        for (int card: deck) {
            ans[index.pollFirst()] = card;
            if (!index.isEmpty())
                index.add(index.pollFirst());
        }

        return ans;
    }
}
Enter fullscreen mode Exit fullscreen mode

It does not have to be a deque, for js-only developers, it's the same as using a regular array.

Upon looking at this code, it turned out I was definitely on the right track, so fucking close! However, I should not have restricted my emulation on just the later half of the ordered array. It is definitely not necessary to do so. Had I emulated the whole array I would have probably solved this problem already.

My main problem here is that I went down a rabbit hole in trying to figure out some kind of mathematical relationship in the operation when it is just easier to emulate it.

This question brings up the dilemma between mathematical approach vs simple brute force. I definitely need more practice to discern which approach I should initiate with.

Let me know anything on your mind after reading through this, THANKS!

Top comments (0)