Practice/Meta/Leetcode 3009. Maximum Number of Intersections on the Chart
CodingOptional
You are given a 2D chart where multiple data series are plotted over the same x-coordinates. Each series is represented as a list of y-values, and all series have the same number of data points.
When you connect consecutive points in each series with straight line segments, these segments can intersect with segments from other series between adjacent x-coordinates.
Your task is to count the total number of pairwise intersections that occur between all line segments across all adjacent column pairs.
Two line segments between columns i and i+1 intersect if:
More formally, for series a and series b, the segments from (i, a[i]) to (i+1, a[i+1]) and from (i, b[i]) to (i+1, b[i+1]) intersect if and only if the relative ordering of their y-values changes between the two columns.
Example 1:
` Input: chart = [[1, 3], [2, 1], [3, 2]] Output: 3 Explanation: Between columns 0 and 1:
Example 2:
Input: chart = [[1, 2, 3], [4, 5, 6]] Output: 0 Explanation: Both series are monotonically increasing and maintain their relative order, so no crossings occur.
Example 3:
Input: chart = [[1, 5], [5, 1]] Output: 1 Explanation: The two lines cross exactly once between the two columns as their relative positions swap.