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;
};
``````

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;
for (int i = 0; i < N; ++i)

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

return ans;
}
}
``````

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!

## 11 Tips That Make You a Better Typescript Programmer

### 1 Think in {Set}

Type is an everyday concept to programmers, but itβs surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

### #2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

...