I have code it using binary search.
complexity can be O(nlog(1e9)).
still it gives TLE. while other users having same approach got it accepted.
Please help where i am missing something?
Thanks in advance.
Question link : minimum-initial-energy-to-finish-tasks
class Solution {
public:
int minimumEffort(vector<vector<int>>& a) {
sort(a.begin(),a.end(),[&](vector<int> p,vector<int> q){
if(p[1]-p[0] > q[1]-q[0])
return true;
return false;
});
auto okk = [&](int m){
for(auto v:a)
{
if(m < v[1])
return false;
m -= v[0];
}
return true;
};
int l = 1,r = 1e9;
for(auto v:a)
l = max(l,v[1]);
int ans;
while(l < r)
{
int m = (l+r)/2;
if(okk(m))
{
r = m;
ans = m;
}
else
{
l = m+1;
}
// cout<<l<<" "<<r<<" "<<m<<endl;
}
return ans;
}
};