Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Sort the points by x-coordinate in non-decreasing order and break the tie by sorting the y-coordinate in non-increasing order.
Now consider two points upper-left corner <code>points[i]</code> and lower-right corner <code>points[j]</code>, such that <code>i < j</code> and <code>points[i][0] <= points[j][0]</code> and <code>points[i][1] >= points[j][1]</code>.
Instead of brute force looping, we can save the largest y-coordinate that is no larger than <code>points[i][1]</code> when looping on <code>j</code>, say the value is <code>m</code>. And if <code>m < points[j][1]</code>, the upper-left and lower-right corner pair is valid.
The actual values don’t matter, we can compress all x-coordinates and y-coordinates to the range <code>[1, n]</code>. Can we use prefix sum now?