Most Visited Sprint Marker
Problem statement
Pat is training to become a great runner. Pat runs sprints along a straight trail with markers numbered from 1 to n. The coach gives a list sprints of markers that Pat must reach in order.
The training is a series of sprints. Pat runs from sprints[0] to sprints[1], then from sprints[1] to sprints[2], and so on until the last marker in the list. A sprint can move toward higher or lower marker numbers.
- A marker is visited if Pat starts at it, stops at it, or passes through it during a sprint.
- Visits add up over all sprints. A marker where one sprint stops and the next sprint starts is counted once for each of those two sprints.
Return the lowest-numbered marker that Pat visits the most times.
Function
getMostVisited(n: int, sprints: int[]) → intExamples
Example 1
n = 5sprints = [2,4,1,3]return = 2Pat runs three sprints.
- The sprint from
2to4visits markers2,3, and4. - The sprint from
4to1visits markers4,3,2, and1. - The sprint from
1to3visits markers1,2, and3.
The visit counts for markers 1 through 5 are 2, 3, 3, 2, and 0. Markers 2 and 3 tie with 3 visits, so the answer is the lower one, 2.
Example 2
n = 7sprints = [6,3,5]return = 3Pat runs two sprints.
- The sprint from
6to3visits markers6,5,4, and3. - The sprint from
3to5visits markers3,4, and5.
The visit counts for markers 1 through 7 are 0, 0, 2, 2, 2, 1, and 0. Markers 3, 4, and 5 tie with 2 visits, so the answer is 3.
Constraints
2 <= n <= 10^52 <= sprints.length <= 2 * 10^41 <= sprints[i] <= nsprints[i] != sprints[i + 1]