Leet Code Problem: 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.
public class SprintVisitsProblem {
public static int getmostvisited(int n, int[] sprints) {
int[] arr = new int[n+2];
for (int i = 0; i < sprints.length-1; i++) {
int start = Math.min(sprints[i], sprints[i+1]);
int end = Math.max(sprints[i], sprints[i+1]);
arr[start] += 1;
arr[end + 1] -= 1;
}
int ans = -1;
int s = 0;
int maxi = -1;
for (int i = 1; i <= n; i++) {
arr[i] += s;
s = arr[i];
if (s > maxi) {
maxi = s;
ans = i;
}
}
return ans;
}
}
Comments
Post a Comment