Today's challenge is: https://leetcode.com/problems/search-suggestions-system/
Here is my code using min heap:
class Solution {
public:
vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {
priority_queue <string, vector<string>, greater<string> > pq;
for(int i=0;i<products.size();i++)
pq.push(products[i]);
string pre;
vector<vector<string>>res;
for(int i=0;i<searchWord.size();i++){
pre+=searchWord[i];
vector<string>v;
while(!pq.empty() && v.size()<3){
int j=0;
bool c=true;
int n=pre.length();
string s1=pq.top();
pq.pop();
while(j<n){
if(pre[j] != s1[j])
c=false;
j++;
}
if(c)
v.push_back(s1);
}
res.push_back(v);
int n=v.size();
int j=0;
while(j<n){
pq.push(v[j]);
j++;
}
}
return res;
}
};I am getting run time error on a large test case. Why won't heap work in this?