#806

Number of Lines To Write String

pupil · 330 · lc easy +22 · verified · 72.2% accepted · 669 likes · top 81%

Description

You are given a string s of lowercase English letters and an integer array widths describing the pixel width of each letter. Specifically, widths[0] is the width of 'a', widths[1] is the width of 'b', and so on.

You are writing s across lines, where no line may exceed 100 pixels wide. Starting from the beginning of s, greedily fit as many characters as possible on each line before starting the next.

Return an array result of length 2 where:

- result[0] is the total number of lines used.

- result[1] is the pixel width of the last line.

Example 1:

Input: widths = [10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10], s = "abcdefghijklmnopqrstuvwxyz"
Output: [3,60]
Explanation: You can write s as follows:
abcdefghij // 100 pixels wide
klmnopqrst // 100 pixels wide
uvwxyz // 60 pixels wide
There are a total of 3 lines, and the last line is 60 pixels wide.

Example 2:

Input: widths = [4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10], s = "bbbcccdddaaa"
Output: [2,4]
Explanation: You can write s as follows:
bbbcccdddaa // 98 pixels wide
a // 4 pixels wide
There are a total of 2 lines, and the last line is 4 pixels wide.

Code

1
2
3