It is leetcode's question #630 - course schedule iii.
I am using priority queue and greedy approach to solve the problem. I was almost done until a weird bug showed up out of nowhere. The bug is titled above.
I am providing the code below. Pls someone help me to get rid of this bug.
typedef vector<int> VI;
bool sortbysec(const vector<int>& v1, const vector<int>& v2)
{
return v1[1]<=v2[1];
}
class Solution {
public:
int scheduleCourse(vector<vector<int>>& courses) {
int n=courses.size();
sort(courses.begin(),courses.end(),sortbysec);
priority_queue<VI> pq;
int sum=0;
for(int i=0;i<n;i++)
{
vector<int> current=courses[i];
if(sum+current[0]<=current[1])
{
pq.push(current);
sum+=current[0];
}else{
if(!pq.empty())
{
vector<int> v=pq.top();
if(v[0]>=current[0])
{
sum=sum-v[0]+current[0];
pq.pop();
pq.push(current);
}
}
}
}
return pq.size();
}
};