Skip to content

Commit b657b11

Browse files
feat: add java solution to lc problem: No.3009 (doocs#2395)
No.3009.Maximum Number of Intersections on the Chart
1 parent e93770e commit b657b11

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

solution/3000-3099/3009.Maximum Number of Intersections on the Chart/README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,28 @@
5353
```
5454

5555
```java
56-
56+
class Solution {
57+
public int maxIntersectionCount(int[] y) {
58+
final int n = y.length;
59+
int ans = 0;
60+
int intersectionCount = 0;
61+
TreeMap<Integer, Integer> line = new TreeMap<>();
62+
63+
for (int i = 1; i < n; ++i) {
64+
final int start = 2 * y[i - 1];
65+
final int end = 2 * y[i] + (i == n - 1 ? 0 : y[i] > y[i - 1] ? -1 : 1);
66+
line.merge(Math.min(start, end), 1, Integer::sum);
67+
line.merge(Math.max(start, end) + 1, -1, Integer::sum);
68+
}
69+
70+
for (final int count : line.values()) {
71+
intersectionCount += count;
72+
ans = Math.max(ans, intersectionCount);
73+
}
74+
75+
return ans;
76+
}
77+
}
5778
```
5879

5980
```cpp

solution/3000-3099/3009.Maximum Number of Intersections on the Chart/README_EN.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,28 @@
4949
```
5050

5151
```java
52-
52+
class Solution {
53+
public int maxIntersectionCount(int[] y) {
54+
final int n = y.length;
55+
int ans = 0;
56+
int intersectionCount = 0;
57+
TreeMap<Integer, Integer> line = new TreeMap<>();
58+
59+
for (int i = 1; i < n; ++i) {
60+
final int start = 2 * y[i - 1];
61+
final int end = 2 * y[i] + (i == n - 1 ? 0 : y[i] > y[i - 1] ? -1 : 1);
62+
line.merge(Math.min(start, end), 1, Integer::sum);
63+
line.merge(Math.max(start, end) + 1, -1, Integer::sum);
64+
}
65+
66+
for (final int count : line.values()) {
67+
intersectionCount += count;
68+
ans = Math.max(ans, intersectionCount);
69+
}
70+
71+
return ans;
72+
}
73+
}
5374
```
5475

5576
```cpp
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int maxIntersectionCount(int[] y) {
3+
final int n = y.length;
4+
int ans = 0;
5+
int intersectionCount = 0;
6+
TreeMap<Integer, Integer> line = new TreeMap<>();
7+
8+
for (int i = 1; i < n; ++i) {
9+
final int start = 2 * y[i - 1];
10+
final int end = 2 * y[i] + (i == n - 1 ? 0 : y[i] > y[i - 1] ? -1 : 1);
11+
line.merge(Math.min(start, end), 1, Integer::sum);
12+
line.merge(Math.max(start, end) + 1, -1, Integer::sum);
13+
}
14+
15+
for (final int count : line.values()) {
16+
intersectionCount += count;
17+
ans = Math.max(ans, intersectionCount);
18+
}
19+
20+
return ans;
21+
}
22+
}

0 commit comments

Comments
 (0)