Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a122bf9

Browse files
committed
line cover max
1 parent 7897a56 commit a122bf9

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

‎src/main/java/grey/algorithm/code11_heap/Code_0005_NowCoder_LineCoverMax.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@ public static void main(String[] args) {
2828
// lines[i][0]: 第i条线的终点
2929
// 时间复杂度O(n*(max - min))
3030
public static int maxCover(int[][] lines) {
31-
if(null == lines || lines.length < 1) {
31+
if(null == lines || lines.length < 1) {
3232
return 0;
3333
}
3434
int min = lines[0][0];
3535
int max = lines[0][1];
3636
for (int i = 1; i < lines.length; i++) {
37-
min = Math.min(lines[i][0],min);
38-
max = Math.max(lines[i][1],max);
37+
min = Math.min(lines[i][0],min);
38+
max = Math.max(lines[i][1],max);
3939
}
4040
int maxCover = 0;
41-
for (int i = min; i <= max; i++) {
41+
for (int i = min; i <= max; i++) {
4242
int cover = 0;
4343
for (int[] line : lines) {
44-
if (line[0] <= i && i <= line[1]){
44+
if (line[0] <= i && i <= line[1]){
4545
cover++;
4646
}
4747
}
@@ -52,27 +52,29 @@ public static int maxCover(int[][] lines) {
5252

5353
// 堆解法
5454
// O(N*logN)
55-
public static int maxCover3(int[][] lines) {
56-
if (null == lines || lines.length == 0) {
55+
public static int maxCover2(int[][] lines) {
56+
if (lines == null || lines.length < 1) {
5757
return 0;
5858
}
59-
// 开始位置排序
60-
Arrays.sort(lines, Comparator.comparingInt(line-> line[0]));
61-
PriorityQueue<int[]> heap = new PriorityQueue<>(Comparator.comparingInt(line-> line[1]));
59+
// 按开始位置排序
60+
Arrays.sort(lines, 0, lines.length, (a, b) -> a[0] - b[0]);
61+
PriorityQueue<int[]> heap = new PriorityQueue<>((a, b) -> a[1] - b[1]);
6262
int max = 0;
6363
for (int[] line : lines) {
64-
while (!heap.isEmpty() && heap.peek()[1] < line[0]) {
64+
// 开始位置从小到大
65+
while (!heap.isEmpty() && line[0] > heap.peek()[1]) {
6566
heap.poll();
6667
}
6768
heap.offer(line);
68-
max = Math.max(heap.size(), max);
69+
max = Math.max(max, heap.size());
6970
}
7071
return max;
7172
}
7273

74+
7375
// 线段树解法
7476
// 复杂度是:O(N*logN)
75-
public static int maxCover2(int[][] lines) {
77+
public static int maxCover3(int[][] lines) {
7678
HashMap<Integer, Integer> map = index(lines);
7779
int N = map.size();
7880
SegmentTree tree = new SegmentTree(N);
@@ -102,7 +104,6 @@ public static HashMap<Integer, Integer> index(int[][] lines) {
102104
return map;
103105
}
104106

105-
106107
// [1...3],[2..6],[4..9],问:哪个区间描的最多,可以用线段树(注意离散化,注意在范围内+1以后,执行的不是querySum而是queryMax)
107108
// 注意:不管什么线段,开始位置排序,线段开始位置越早,越先处理
108109
public static class SegmentTree {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /