-3

There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array points where points[i] = [xstart, xend] denotes a balloon whose horizontal diameter stretches between xstart and xend. You do not know the exact y-coordinates of the balloons.

Arrows can be shot up directly vertically (in the positive y-direction) from different points along the x-axis. A balloon with xstart and xend is burst by an arrow shot at x if xstart <= x <= xend. There is no limit to the number of arrows that can be shot. A shot arrow keeps traveling up infinitely, bursting any balloons in its path.

Given the array points, return the minimum number of arrows that must be shot to burst all balloons

bool compare(vector<int> &a,vector<int> &b){
          return a[1]<=b[1];
}
class Solution {
public:
    int findMinArrowShots(vector<vector<int>>& points) {
        if(points.size()==1) return 1;
        sort(points.begin(),points.end(),compare);
        int arrows=1,end=points[0][1];
        for(int i=1;i<points.size();i++){
            if(points[i][0]>end){
                arrows++;
                end=points[i][1];
            }
        }
        return arrows;
    }
};

I am getting a runtime error:

Line 1034: Char 34: runtime error: applying non-zero offset 4 to null pointer (stl_vector.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1043:34
fabian
  • 80,457
  • 12
  • 86
  • 114
  • 2
    First of all figure out the input for your program that causes the crash. Then create a [mre]. Then build (with extra warnings enabled, treated as errors) the program locally on your own system. Lastly use a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to catch the crash and see when and where in *your* code it happens. – Some programmer dude Jan 05 '23 at 09:15
  • 2
    And as a *possible* hint about the crash: Your `compare` function doesn't satisfy [the requirements](https://en.cppreference.com/w/cpp/named_req/Compare) needed by [`std::sort`](https://en.cppreference.com/w/cpp/algorithm/sort). More specifically it doesn't follow the [string weak ordering](https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings) requirement. That leads to *undefined behavior*. – Some programmer dude Jan 05 '23 at 09:23
  • Have you thought about what happens to your code if points.size() == 0? One of the basic rules of programming : never trust your input. – Pepijn Kramer Jan 05 '23 at 09:31
  • Also note this is a group about C++. Just so you know, leetcode generally teaches more bad practices then teach you good C++. It should only be used to train your problem solving skills. – Pepijn Kramer Jan 05 '23 at 09:33
  • Please show (point out) the error line. – hyde Jan 05 '23 at 09:43
  • This question's code/phrasing suggests that it came from one of many countless coding challenge/puzzle scam sites. They take advantage of people who want to learn C++ by offering arcane coding puzzles and promising that you don't need to study and learn C++ with a good textbook, just do a bunch of meaningless coding puzzles. Everyone eventually realizes that these pointless coding puzzles are a waste of time, and there's nothing to be learned from them. But only after spending a lot of time doing them. And there's nothing to show for it. – Sam Varshavchik Jan 05 '23 at 12:06

1 Answers1

1
bool compare(vector<int> &a,vector<int> &b){
          return a[1]<=b[1];
}

should be

bool compare(const vector<int> &a, const vector<int> &b){
          return a[1]<b[1];
}

Comparators must define a strict weak ordering. One consequence of that is that they must return false for equal values, so <= is incorrect.

Looking at the error message however, I suspect that the immediate cause of your problems is that the vector points has size zero and so end=points[0][1] is an out of bounds vector access.

john
  • 85,011
  • 4
  • 57
  • 81