You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
class Solution {
public int findMinArrowShots(int[][] points) {
// sort points, first by start, then by end
// prevent exceeding integer limit
Arrays.sort(points, (a, b) -> {
if (a[0] != b[0]) return ((long)(a[0]) - b[0]) > 0 ? 1 : -1;
return ((long)(a[1]) - b[1]) > 0 ? 1 : -1;
});
int l = 0, r = 0;
int ans = 0;
for (int i = 0; i < points.length; ++i) {
if (l == 0 && r == 0) {
if (i < points.length - 1 && points[i][1] >= points[i + 1][0]) {
l = points[i + 1][0];
r = Math.min(points[i][1], points[i + 1][1]);
++i;
}
++ans;
} else {
if (l >= points[i][0] || r >= points[i][0]) {
l = Math.max(l, points[i][0]);
r = Math.min(points[i][1], r);
} else {
--i;
l = 0;
r = 0;
}
}
}
return ans;
}
}
这道题是intervals区间合并的问题。因为我发现类似的区间合并题目都可以用这个做法,所以记录下来。
步骤:
intervals类型题目如下,并不难,大部分都是medium难度。
类似的intervals类型题目:
The text was updated successfully, but these errors were encountered: