I can not understand why my submission 292439267 gives wrong answer while submission 292440081 does not. I am unable to understand the difference where i was wrong
this is the part in my WA submission
while(!pq.empty() && power < hurdle[i].S)
{
power += pq.top();
pq.pop();
items++;
}
if(pq.empty() && power < hurdle[j].S)
{
cout << -1 << endl;
return;
}
While this below one is of accepted one
while(power < hurdle[i].S)
{
if(!pq.empty())
{
power += pq.top();
pq.pop();
items++;
}
else
{
cout << -1 << endl;
return;
}
}
you need to check that the pq is not empty because it might be and this will lead to Run time error or wrong answer when doing top() or pop() for an empty pq
i am checking it
since i only do top() or pop() when !pq.empty is true
typo j->i —
hurdle[j].S
❌,hurdle[i].S
✅Yes, there is typo error.
apart from typo ->
The first code is always not correct because lets assume a test case where we used all the possible elements in the set/pq and ans exits and the set is now empty but as you have written the check outside so -1 also got executed.....but in the second code it's not the same case as it will either only print a valid ans or -1 !!
Yes, you are right. Because -1 is considered as true.