Imagine there are n points along a straight trail, while a runner run sprints of intervals between those point.
The training plan is an array a[], which implies the runner should run from point a[i] to point a[i+1].
For example, given n = 10, a = [2, 4, 1, 2].
The runner should run from point 2 to point 4,
then turn back from point 4 to point 1,
and then from point 1 to point 2.
Find the point that visited the most by runner after he finished training, i.e. in above example, point 2 is the most visited.
If more than one point are visited the most, find the point with minimum index.
I think this can be solved by simulating the moves in the array and keeping track of which positions are visited the most (O(n * len(a)) approach). I was wondering if there was a faster way to solve this problem? I'm mainly looking for the intuition and main idea behind the optimal approach as opposed to the code.
Also are there any similar questions to the one above on Leetcode?