vector<vector<int>> merge(vector<vector<int>>& intervals) {
sort(intervals.begin(),intervals.end());
vector<vector<int>> ans;
int n=intervals.size();
ans.push_back(intervals[0]);
for(int i=1;i<n;i++){
if(ans.back()[1]>=intervals[i][0]){
ans.back()[1]=max(intervals[i][1],ans.back()[1]);
} else {
ans.push_back(intervals[i]);
}
}
return ans;
}
If I use ans[i-1][1]
it doesn't work but ans.back()[1]
works well. Why ?
It should work same but when added shows an error
Error is like Runtime Error ================================================================= ==21==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6030000000b8 at pc 0x000000356e7d bp 0x7ffd219fb8b0 sp 0x7ffd219fb8a8 READ of size 8 at 0x6030000000b8 thread T0 #3 0x7fac844cd082 (/lib/x86_64-linux-gnu/libc.so.6+0x24082) 0x6030000000b8 is located 0 bytes to the right of 24-byte region [0x6030000000a0,0x6030000000b8) allocated by thread T0 here: #4 0x7fac844cd082 (/lib/x86_64-linux-gnu/libc.so.6+0x24082) Shadow bytes around the buggy address: