Prashant94157's blog

By Prashant94157, history, 7 weeks ago, In English

In front of you ,there is a treasure containing many gems, soon you realize that they contain bad luck and are not precious as well. You have the power to destroy gems but can destroy 1 gem per sec. You can destroy gems in any order and wanted to destroy maximum bad luck. You have given a 2D matrix, where for i'th gem , it takes arr[i][0] seconds to give arr[i][1] units of bud luck and you can decide in which order you want to receive the bad luck. There is no time lapse in the process. Although while receiving bad luck, you can destroy bad luck. Once gem started giving bad luck , cannot be stopped until it is destroyed of its own after giving bad luck. You have to print maximum bad luck you can destroy.


1 <= n <= 10^3 1 <= arr[i][0] <= 10^3 1 <= arr[i][0] <= 10^6

Testcase 1:
0 10
1 4
1 3
1 20
Output: 30
You can get maximum 30 when you choose to destroy 0th and 3rd while receiving bad luck from 1st and 2nd gems. 2 gems require 2 seconds to destroy while 1st and 2nd take 2seconds to give bad luck.
Testcase 2:
3 5
6 5
0 8
8 8
7 4
10 3
8 6
Tags dp
  • Vote: I like it
  • +5
  • Vote: I do not like it

7 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone help in this question? I actually tried so hard in this but stuck with TLE in DP solution, may due to stack overflow. Please help me with question.

My TLE solution:

int f(int i,int move, vector<int,int> &arr, unordered_map<string, int> &mp)
   if(i== arr.size())
         return INT_MIN;
      return 0;
   string s = to_string(i) + " | " + to_string(move);
   if(mp.find(s) != mp.end())
      return mp[s];
   return mp[s] = max(arr[i][1] + f(i+1,move-1,arr,mp), f(i+1,move + arr[i][0], arr,mp));

7 weeks ago, # |
  Vote: I like it -14 Vote: I do not like it

It's just a knapsack problem. Please think before asking.


  • »
    7 weeks ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    But it is not a knapsack, if you closely observe. Even if it is, tell me the weight of the knapsack and also tell me the type of knapsack it it pls.