I am getting runtime error while running the following code on leetcode. When I am removing the user-defined comparator function it is working fine. But with user define comparator function it is giving runtime error as follows :
Line 924: Char 9: runtime error: reference binding to null pointer of type 'int' (stl_vector.h) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/stl_vector.h:933:9
class Solution {
private:
static bool comp (vector<int> p1, vector<int> p2) {
if(p1[0] < p2[0] || p1[1] < p2[1])
return true;
else
return false;
}
public:
int maxEnvelopes(vector<vector<int>>& envelopes) {
if (envelopes.empty())
return 0;
sort(envelopes.begin(), envelopes.end(), comp);
vector<int> dp(envelopes.size(), 1);
int res = 0;
for (int i = 0; i < envelopes.size(); i++) {
for (int j = i-1; j >=0; j--) {
if (envelopes[j][0] < envelopes[i][0] && envelopes[j][1] < envelopes[i][1] && dp[j] + 1 > dp[i])
dp[i] = 1 + dp[j];
}
res = max(res, dp[i]);
}
return res;
}
};