← 返回 google 的题目列表Piano Hand Movement Grouping
类型:qbank
Given a sequence of integer piano-key positions and a maximum hand span k, compute the minimum number of hand lifts needed to play the sequence. Another version asks to print each group of notes playable from one hand position.
Requirements
Input: an integer array of piano key positions, with positions starting at 1.
A hand placement covers a contiguous range whose leftmost and rightmost keys differ by at most k.
Play the notes in sequence and return the minimum number of times the hand must be lifted and repositioned.
Another version omits an explicit k: return how many times the hand moves, then print the previous hand-position group whenever a move occurs and print the final group at the end.
Examples
With notes = [1, 3, 5, 7, 9, 11] and k = 4, the stated output is 2.
Earlier examples use [1,2,3,4,5] -> 0 and [5,9,1] -> 1, without stating the reach parameter.
Notes
Clarify whether a placement is fixed until the hand lifts, whether the covered interval may slide without counting as a lift, and whether the initial placement counts. These details materially change the answer.
The six-note example's explanation says a placement at 7 covers keys 7–11 but still counts another lift for 9 and 11. Treat that as an ambiguity to resolve before coding rather than silently assuming the output convention.
Maintain the feasible placement interval for the current group. The follow-up printout should reuse the same grouping state instead of duplicating the movement logic.
Preparation
Practice turning the physical reach rule into an explicit interval invariant before writing code.
Implement a one-pass grouping scan, then extend it to emit group boundaries without changing the core state transition.