Feel free to checkout the image source at the bottom of the page for the original problem statement đŚ
Imagine youâve got a fantastic time machine at your disposal, and youâre on a grand journey through history. Youâre given a list of years in which you must travel, starting from the year years[0]. Your adventure involves hopping from one year to the next in the sequence, and each leg of your journey has its own time cost: If you travel from one year to the exact same year, it takes you 0 hours (youâre already there!). If you move forward in time to a future year, it takes you 1 hour. If you journey backward to a past year, it takes you 2 hours. Your task is to calculate the total time required to complete your journey through all the years in the list, traveling in the given order. To sum up, you need to figure out how much time youâll spend traveling between each pair of years, according to these rules. Your solution should be efficient enough to handle the task within the time constraints, even if itâs not the absolute fastest. So, set your time machine to work and calculate the total time for your historical adventure!
Example 1:
Input: years = [2000, 1990, 2005, 2050]
Output: 4
Explanation:Imagine a time-travel adventure: First, you set off from the year 2000 and journey forward to 2021, a trip that takes you just 1 hour to traverse the years and witness the unfolding of the present. Then, you reverse course from 2021 back to 2005, a return journey that spans 2 hours, allowing you to revisit the past. In total, your time-travel adventure sums up to 3 hours, with each leg of the journey unfolding a different chapter of time.
Example 2:
Input: years = [2000, 2021, 2005]
Output: 3
Explanation:Feel free to checkout the image source at the bottom of the page for the original explanation đ Imagine a time-travel journey: First, you remain within the year 2021, a trip that requires no time at all, as youâre simply staying in the same year. Next, you embark on a journey from 2021 back to 2005. This trip takes you 2 hours to traverse the past. In total, your adventure through time takes 2 hours, with the first leg being instantaneous and the second a brief voyage through history.
Example 3:
Input: years = [2021, 2021, 2005]
Output: 2
Explanation:Feel free to checkout the image source at the bottom of the page for the original explanation đ First, you go from 2021 to 2021, which requires 0 hour as the trip takes plaxce within the smae year. Then you go from 2021 to 2005, which requires 2 hours. In total, you need 0 + 2 = 2 hours.
- 1 <= years.length <= 100
- 1 <= years[i] <= 104


input:
output: