Dynamic Programming: Basics and a Practical Example

Introduction: Dynamic programming is a problem-solving technique that can seem quite intimidating at first glance. However, once you grasp its fundamentals and see it in action, you'll find it to be a powerful tool in your coding arsenal. In this blog post, we'll demystify the basics of dynamic programming and walk through a practical example. We'll provide you with clear explanations and a complete Python program, so you can understand and apply this essential concept with confidence. Understanding Dynamic Programming: At its core, dynamic programming is all about breaking down a complex problem into smaller, overlapping subproblems. It's a technique used to efficiently solve problems by storing and reusing solutions to these subproblems, rather than recomputing them. This leads to significant improvements in runtime complexity. Example: Fibonacci Sequence with Dynamic Programming Let's start by tackling a classic problem: calculating Fibonacci numbers. The Fibonac...

Climbing the Leaderboard | Hackerrank | Solution

Source Hackerrank

Alice is playing an arcade game and wants to climb to the top of the leaderboard and wants to track her ranking. The game uses Dense Ranking, so its leaderboard works like this:
  • The player with the highest score is ranked number  1 on the leaderboard.
  • Players who have equal scores receive the same ranking number, and the next player(s) receive the immediately following ranking number.

For example,
ranked = {100, 90, 90, 80}
player = {70. 80. 105}

The ranked players will have ranks , , , and , respectively. If the player's scores are ,  and , their rankings after each game are ,  and . Return .

Function Description

Complete the climbingLeaderboard function in the editor below.

climbingLeaderboard has the following parameter(s):

  • int ranked[n]: the leaderboard scores
  • int player[m]: the player's scores
Input Format
The first line contains an integer , the number of players on the leaderboard.
The next line contains  space-separated integers , the leaderboard scores in decreasing order.
The next line contains an integer, , denoting the number games Alice plays.
The last line contains  space-separated integers , the game scores.
Output Format
Print  integers. The  integer should indicate Alice's rank after playing the  game.

Sample Input 1
7 100 100 50 40 40 20 10 4 5 25 50 120
Sample Output 1
6
4
2
1
*for explanation and more test case visit above link*

Solution

static int[] climbingLeaderboard(int[] scores, int[] alice) {

        int res[] = new int[alice.length];
        List<Integer> list = new ArrayList<Integer>();
        Set<Integer> set = new HashSet<>();
        for(int i =0;i<scores.length;i++){
            if(!set.contains(scores[i])){
               list.add(scores[i]);
                set.add(scores[i]);
            }
        }
        for(int i = 0;i < alice.length; i++) {

            int rank = Collections.binarySearch(list, alice[i],  
                                  Collections.reverseOrder());

            if(rank < 0 ){
                rank = Math.abs(rank);
            } else {
                rank = rank+1;
            }
            res[i] = rank;
        }

        return res;
    }

Comments

Popular posts from this blog

Array Transformation | biweekly-contest-12 | leetcode | Java Solution

Dynamic Programming: Basics and a Practical Example

leetcode | Longest Substring Without Repeating Characters Solution