man.god96's blog

By man.god96, history, 4 years ago, In English

So here's the problem -

I need to find the minimum divisions required with a divisor 'd', to equalize at least k elements in the array.

For eg. for array {64,25,33,30}, divisor=2 and k=2 ->

Divide 64 two times to get 16 and 33 one time to get 16. So array becomes {16,25,16,30} which has k=2 elements equal. And so minimum divisions required = 3.

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By man.god96, history, 4 years ago, In English

This is regarding https://leetcode.com/problems/coin-change/. It is a simple DP problem. And I wrote the following code for it using map for memoization. But it gave Time limit extended -

class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        int n = coins.size();
        map<int,int> dp;
        dp[0]=0;
        for(int i=1;i<=amount;i++) {
            dp[i]=amount+1;
            for(int j=0;j<n;j++) {
                if(i>=coins[j]) dp[i] = min(dp[i],dp[i-coins[j]]+1);
            }
        }
        return dp[amount]>amount ? -1:dp[amount];        
    }
};

But when I changed map to vector, it gets ACCEPTED (Following code got accepted)-

class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        int Max = amount + 1;
        vector<int> dp(amount + 1, Max);
        dp[0] = 0;
        for (int i = 1; i <= amount; i++) {
            for (int j = 0; j < coins.size(); j++) {
                if (coins[j] <= i) {
                    dp[i] = min(dp[i], dp[i - coins[j]] + 1);
                }
            }
        }
        return dp[amount] > amount ? -1 : dp[amount];
    }
};

So why map gave TLE and not vector. When should we use map and when vector while solving DP problems? Any help will be appreciated.

Full text and comments »

  • Vote: I like it
  • -14
  • Vote: I do not like it

By man.god96, history, 5 years ago, In English

It getting difficult find solutions of C and D of lot ABCs. And to that, I am thinking of doing only those ABCs having english editorials. So has anyone collected them anywhere? Sorry for incorrect english.

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it