Trains the technique from
LeetCode 806Number of Lines To Write StringThis 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 typesetter fills rows exactly 100 units wide. Letter 'a' takes widths[0] units, letter 'b' takes widths[1], and so on through the alphabet.
The letters of s are set in order. A letter goes on the current row when it still fits within 100 units; otherwise a new row is started and the letter goes at its beginning. No letter is ever split across two rows.
Return a list of two numbers: how many rows were used, and how many units the last row holds.
Example 1
Every letter takes 10 units, so ten of them exactly fill the first row and the eleventh starts a second row holding 10 units.
Example 2
A single letter of width 2 sits on the first row, which then holds 2 units.
Example 3
Fifty letters of width 2 fill the first row exactly, so the fifty-first starts a second row.
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 rows_needed(widths: list[int], s: str) -> list[int]:public int[] rowsNeeded(int[] widths, String s)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.