I was trying to solve this problem in leetcode. problem: heap problem
this is my code:
class Solution {
public:
vector<string> findRelativeRanks(vector<int>& score) {
priority_queue<pair<int,int>,vector<pair<int,int>>>mxheap;
for(int i=0; i<score.size(); i++){
mxheap.push({score[i],i});
}
vector<string>ans(score.size());
int place = 1;
while(!mxheap.empty()){
switch(place){
case 1:
ans[mxheap.top().second] = "Gold Medal";
mxheap.pop();
break;
case 2:
ans[mxheap.top().second] = "Silver Medal";
mxheap.pop();
break;
case 3:
ans[mxheap.top().second] = "Bronze Medal";
mxheap.pop();
break;
default:
ans[mxheap.top().second] = to_string(place);
}
place++;
}
return ans;
}
};
I don't know why I am getting the time limit exceeds I also tried removing the pair and using a map but that also didn't work. I also saw some answers in discuss section those answers were also of (nlogn) time complexity but they worked fine mine is also (nlogn) still its not working can someone please tell me what am I doing wrong here.