Time Travel
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!
1Example 1
2Example 2
3Example 3
Constraints
Limits and guarantees your solution can rely on.