LeetCode #: 1184
Difficulty: Easy.
Topics: Array.
The idea:
- Create prefix sum of
distance
. - With the prefix sum, you can easily get the distance between
start
anddestination
in one direction (either clockwise or counterclockwise). - To get the distance for the other direction, you can subtract the distance above from the total sum distance.
Time complexity: O(n) since we loop through all elements in distance
exactly once.
Space complexity: O(1). Extra space used is always the same.