Huawei OD computer test - Climber 2 (Java & JS & Python & C)

Question description

Climbers love to search for various maps and try to climb to the highest mountains.

The map is represented as a one-dimensional array, the index of the array represents the horizontal position, and the elements of the array represent the relative altitude. Array element 0 represents the ground.

For example: [0,1,2,4,3,1,0,0,1,2,3,1,2,1,0] represents the map as shown below. There are two mountain ranges in the map. They are 1,2,3,4,5 and 8,9,10,11,12,13, and the highest peak heights are 4,3 respectively. The highest peak positions are 3,10 respectively.

A mountain range may have multiple peaks (higher than adjacent locations, or at the map boundary with greater heights than adjacent locations).

Climbing will consume the climber's physical strength (integer),

  • When going up a mountain, consume twice the stamina of the adjacent height difference.
  • When going down a mountain, consume twice as much stamina as the difference in height between adjacent areas.
  • Flat ground does not consume physical strength

Climbers' lives are in danger when their physical exertion reaches zero.

For example, the mountain shown above:

  • From index 0 to index 1, the height difference is 1, which requires 2 * 1 = 2 physical energy.
  • From index 2 to index 3, the height difference is 2, which requires 2 * 2 = 4 physical energy.
  • From index 3 to index 4, the height difference is 1, which requires 1 * 1 = 1 physical energy.

Climbers want to evaluate how many peaks in a map can be climbed and returned to the ground safely without risking life.

For example, the array in the picture above has 3 different mountain peaks. You can climb the mountain at position 3.

Guess you like

Origin blog.csdn.net/qfc_128220/article/details/134639750