File tree Expand file tree Collapse file tree 3 files changed +66
-2
lines changed
solution/3000-3099/3009.Maximum Number of Intersections on the Chart Expand file tree Collapse file tree 3 files changed +66
-2
lines changed Original file line number Diff line number Diff line change 53
53
```
54
54
55
55
``` 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
+ }
57
78
```
58
79
59
80
``` cpp
Original file line number Diff line number Diff line change 49
49
```
50
50
51
51
``` 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
+ }
53
74
```
54
75
55
76
``` cpp
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments