There is a line chart consisting of n points connected by line segments. You are given a 1-indexed integer array y. The kth point has coordinates
(k, y[k]). There are no horizontal lines; that is, no two consecutive points have the same y-coordinate.
We can draw an infinitely long horizontal line. Return themaximum number of points of intersection of the line with the chart.
****
Input: y =[1,2,1,2,1,3,2]Output: 5Explanation: As you can see in the image above, the line y =1.5 has 5 intersections with the chart(in red crosses). You can also see the line y =2 which intersects the chart in4points(in red crosses). It can be shown that there is no horizontal line intersecting the chart at more than 5 points. So the answer would be 5.
Example 2:
1
2
3
4
****
Input: y =[2,1,3,4,5]Output: 2Explanation: As you can see in the image above, the line y =1.5 has 2 intersections with the chart(in red crosses). You can also see the line y =2 which intersects the chart in2points(in red crosses). It can be shown that there is no horizontal line intersecting the chart at more than 2 points. So the answer would be 2.
Each segment between two consecutive points on the chart can be intersected by a horizontal line if the line’s y-value is strictly between the two y-values. For each such segment, the interval (min(y[i], y[i+1]), max(y[i], y[i+1])) is a possible range for intersection. The problem reduces to finding the maximum number of overlapping intervals.
classSolution {
public:int maxIntersectionCount(vector<int>& y) {
vector<pair<int, int>> events;
for (int i =0; i +1< y.size(); ++i) {
int a = min(y[i], y[i+1]), b = max(y[i], y[i+1]);
events.emplace_back(a+1, 1);
events.emplace_back(b, -1);
}
sort(events.begin(), events.end());
int ans =0, cnt =0;
for (auto& [pos, typ] : events) {
cnt += typ;
ans = max(ans, cnt);
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import"sort"funcmaxIntersectionCount(y []int) int {
events:= [][2]int{}
fori:=0; i+1 < len(y); i++ {
a, b:=y[i], y[i+1]
ifa > b { a, b = b, a }
events = append(events, [2]int{a+1, 1}, [2]int{b, -1})
}
sort.Slice(events, func(i, jint) bool { returnevents[i][0] < events[j][0] })
ans, cnt:=0, 0for_, e:=rangeevents {
cnt+=e[1]
ifcnt > ans { ans = cnt }
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.*;
classSolution {
publicintmaxIntersectionCount(int[] y) {
List<int[]> events =new ArrayList<>();
for (int i = 0; i + 1 < y.length; i++) {
int a = Math.min(y[i], y[i+1]), b = Math.max(y[i], y[i+1]);
events.add(newint[]{a+1, 1});
events.add(newint[]{b, -1});
}
events.sort(Comparator.comparingInt(a -> a[0]));
int ans = 0, cnt = 0;
for (int[] e : events) {
cnt += e[1];
ans = Math.max(ans, cnt);
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
funmaxIntersectionCount(y: IntArray): Int {
val events = mutableListOf<Pair<Int, Int>>()
for (i in0 until y.size - 1) {
val a = minOf(y[i], y[i+1])
val b = maxOf(y[i], y[i+1])
events.add(Pair(a+1, 1))
events.add(Pair(b, -1))
}
events.sortBy { it.first }
var ans = 0var cnt = 0for ((_, typ) in events) {
cnt += typ
ans = maxOf(ans, cnt)
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution:
defmaxIntersectionCount(self, y: list[int]) -> int:
events = []
for i in range(len(y)-1):
a, b = min(y[i], y[i+1]), max(y[i], y[i+1])
events.append((a+1, 1))
events.append((b, -1))
events.sort()
ans = cnt =0for _, typ in events:
cnt += typ
ans = max(ans, cnt)
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
impl Solution {
pubfnmax_intersection_count(y: Vec<i32>) -> i32 {
letmut events =vec![];
for i in0..y.len()-1 {
let (a, b) = (y[i].min(y[i+1]), y[i].max(y[i+1]));
events.push((a+1, 1));
events.push((b, -1));
}
events.sort();
letmut ans =0;
letmut cnt =0;
for (_, typ) in events {
cnt += typ;
ans = ans.max(cnt);
}
ans
}
}