#196. 计算路径高度变化总和
计算路径高度变化总和
Calculate Path Height Change Sum
Background
During a hike, you need to calculate the total sum of height changes along a path.
Problem Description
We have an array of heights, representing the altitude along a walking trail. Given start/end indexes into the array, return the sum of the changes for a walk beginning at the start index and ending at the end index, however increases in height count double. For example, with the heights and , yields a sum of . The start and end index will both be valid indexes into the array with .
Input Format
The input is given from standard input in the following format.
heights: An integer array representing altitudes along the path.start: An integer, the starting index.end: An integer, the ending index.
Output Format
Output an integer to standard output in the following format.
sum: An integer, the calculated total sum of height changes.
Sample
[5 3 6 7 2] 2 4
7
[5 3 6 7 2] 0 1
2
[5 3 6 7 2] 0 4
15
Sample Explanation
For the first sample [5 3 6 7 2] 2 4:
heights[2] = 6,heights[3] = 7,heights[4] = 2.- From index to : height increases from to , change is . Since it's an increase, it counts as .
- From index to : height decreases from to , change is . Since it's a decrease, it counts as .
- Total sum is .
Constraints
Time limit: 1s, Memory limit: 1024KiB.