class Solution {
private:
static bool cmp(const int& a, const int& b)
{
return a >= b;
}
public:
long long function(vector<int>& nums) {
sort(nums.begin(), nums.end(), cmp);
return 0;
}
};
Above code results in heap buffer over-flow if nums is:
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
but works fine if nums is:
[1,1,1,1,1,1,1,1,1,1,1,1,1]
I resolved this issue by making:
return a > b;
in cmp function instead of >=
But Can anyone tell me why >= is not correct?
New contributor
Shashank Bhari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.