← 返回 google 的题目列表Min Hand Moves to Play a Sequence on a 1..∞ Keyboard (Segment Grouping)
类型:online_judge
You are given an integer sequence a[0..n-1] of keyboard positions, numbered from 1 to infinity. You play the sequence in order using one hand. You may start with your hand at any position.
If a consecutive chunk of notes can be played without moving your hand, call that chunk one "hand position". When the next note cannot be played under the current hand position, you must move your hand to a new position and continue.
Tasks:
Compute the minimum number of hand moves needed to play the entire sequence.
Follow-up: each time you move your hand, print the previous hand position (the segment you just played), and also print the final hand position at the end.
Examples (from the interview notes):
a = [1,2,3,4,5] => 0 moves
a = [5,9,1] => 1 move
Note: the exact definition of a "hand position" (e.g., whether it is an interval [L,R], whether there is a fixed hand span, etc.) is underspecified in the notes and should be clarified during the interview.
Example
Input
5
1 2 3 4 5
Output
0