Merge bars to fit
Your task
A dashboard draws one bar per hour of a build log, but the widget can only show max_bars bars. While there are too many bars it repeatedly merges the adjacent pair whose combined height is smallest into a single bar of that combined height, and a tie goes to the leftmost such pair. Merging shortens the row by one, and a merged bar can then be merged again with a neighbour. Return the bar heights that remain, left to right. When max_bars is at least the number of bars, return the heights unchanged, and when max_bars is below 1 treat it as 1.
Examples
collapsed_bar_heights(heights: list[int], max_bars: int) -> list[int]Example 1
Input
collapsed_bar_heights([4, 1, 1, 4, 3], 3)Output
[6, 4, 3]Example 2
Input
collapsed_bar_heights([5, 2], 5)Output
[5, 2]Example 3
Input
collapsed_bar_heights([3, 3, 3, 3], 1)Output
[12]