Question: Nvidia, Recent Online Assessment Question | Minimum health | Avoiding Landmines | Retail Inventory Management | 2023
2
Entering edit mode

ADD COMMENTlink 13 months ago Delta 2.8k
2
Entering edit mode

#include <iostream>

#include <queue>

#include <vector>

 

using namespace std;

 

long getMinimumHealth(vector<int> initialPlayers, vector<int> newPlayers, int rank)

{

    priority_queue<int, vector<int>, greater<int>> store; // Min-heap to store strengths

 

    // Add initial players to the heap

    for (int strength : initialPlayers)

    {

        store.push(strength);

    }

 

    long totalHealth = 0; // To store the total initial health

 

    for (int strength : newPlayers)

    {

        store.push(strength); // Add the new player's strength

 

        if (store.size() > rank)

        {

            totalHealth += store.top(); // Remove the weakest player from the heap

            store.pop();

        }

    }

 

    while (!store.empty() && store.size() > rank)

    {

        totalHealth += store.top(); // Remove remaining weakest players from the heap

        store.pop();

    }

 

    return totalHealth;

}

 

int main()

{

    vector<int> initialPlayers = {1, 2};

    vector<int> newPlayers = {3, 4};

    int rank = 1;

 

    long minInitialHealth = getMinimumHealth(initialPlayers, newPlayers, rank);

    cout << "Minimum Initial Health needed: " << minInitialHealth << endl;

 

    return 0;

}

 

ADD COMMENTlink 13 months ago SARTHAK NARANG • 40
0
Entering edit mode

Q1 - just return rank'th element from each array ? 

ADD COMMENTlink 13 months ago Ayush Agarwal • 20
0
Entering edit mode

#include <bits/stdc++.h>

using namespace std;

 

int findSafeWays(vector<vector<int>> &mat)

{

    int m = mat.size();

    int n = mat[0].size();

    vector<vector<int>> dp(m, vector<int>(n, -1));

    for (int i = 0; i < m; i++)

    {

        for (int j = 0; j < n; j++)

        {

            if (i == 0 && j == 0)

            {

                dp[i][j] = 1;

                continue;

            }

            int up = 0;

            int left = 0;

 

            if (i > 0)

            {

                if (mat[i - 1][j] != 0)

                    up = dp[i - 1][j];

            }

            if (j > 0)

            {

                if (mat[i][j - 1] != 0)

                    left = dp[i][j - 1];

            }

            dp[i][j] = (up + left) % 1000000007;

        }

    }

    return dp[m - 1][n - 1];

}

int main()

{

    int m = 3; // Number of rows

    int n = 4; // Number of columns

 

    vector<vector<int>> grid = {

        {1, 1, 1, 1},

        {1, 1, 1, 1},

        {1, 1, 1, 1}};

 

    int ways = findSafeWays(grid);

 

    cout << "Number of ways to reach the bottom-right cell: " << ways << endl;

 

    return 0;

}

 

ADD COMMENTlink 13 months ago SARTHAK NARANG • 40

Login before adding your answer.

Similar Posts
Loading Similar Posts