Trains the technique from
LeetCode 3318Find X-Sum of All K-Long Subarrays IThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A jukebox log lists what was played, plays[i] being the track number of the i-th play.
The weight of a stretch of plays is worked out like this: tally how often each track turns up in the stretch, then keep the keep tracks with the highest tallies, settling a tie in favour of the higher track number. The weight is the total of each kept track's number times its tally in the stretch. When the stretch holds keep or fewer distinct tracks they are all kept, so its weight is the total of the stretch.
Return an array whose i-th entry is the weight of the stretch of span plays starting at position i.
Example 1
Four stretches of three plays. The first tallies track 4 twice against track 5 once and keeps track 4 for 8. The second tallies all three tracks once, so the tie hands it to track 6. The third keeps track 6 with two plays for 12, and the last is three plays of track 6.
Example 2
One stretch, in which tracks 2 and 3 are both tallied twice. The tie goes to the higher track number, so track 3 is kept for two plays.
Example 3
The stretch holds only two distinct tracks and both are kept, so the weight is the total of the whole stretch.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def find_x_sum(plays: list[int], span: int, keep: int) -> list[int]:public int[] findXSum(int[] plays, int span, int keep)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.