1 of 365 Coding challenge
xdmarttt
Posted on March 8, 2024
Hey! So, here's my first article, diving into this whole 365-day coding challenge thing. I'm just getting started, so go easy on me. I'm not a pro at English, so thanks for putting up with that too.
I started early in the morning, all gung-ho, but I kinda dove into the coding without really getting what the problem was asking. Ended up with a wonky solution and took me a good two hours to realize where I messed up. Not the greatest start, but I learned something.
Now, my first challenge was 'Climbing the Leaderboard,' and you can find it here: https://www.hackerrank.com/challenges/climbing-the-leaderboard/problem.
So, let's delve into my solution:
The first part involves obtaining the unique ranks:
const uniqueRanked = [...new Set(ranked)];
Additionally, I created another array that serves as a placeholder for the indices:
const rankedSet = Array.from({ length: uniqueRanked.length });
Moving on, I iterated through the player scores:
player.forEach(score => {
if (score < uniqueRanked[uniqueRanked.length - 1]) {
finalRank.push(rankedSet[rankedSet.length - 1] + 1);
} else if (score > uniqueRanked[0]) {
finalRank.push(1);
} else {
const midPoint = Math.floor((rankedSet.length + 1) / 2) - 1;
const start = score <= uniqueRanked[midPoint] ? midPoint : 0;
for (let i = start; i <= rankedSet[rankedSet.length - 1]; i++) {
if (score >= uniqueRanked[i]) {
finalRank.push(rankedSet[i]);
break;
}
}
}
});
As indicated in the code above, I excluded the first and last indices to streamline the iteration process. The nested iteration involves selecting a middle point that divides the array into two. Although not strictly necessary, I implemented this approach to optimize performance in specific cases on Hackerrank where speed is crucial. The goal is to swiftly determine the correct placement of the rank.
I know this may not be the best solution, but I just want to share my journey here :) . I'm open to any new suggestions and constructive criticism to improve my solution. Hope you have a good day! Thanks, everyone!
Posted on March 8, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
October 10, 2024
April 11, 2024