Practice/Google/Leetcode 806. Number of Lines To Write String
CodingMust
You are designing a text rendering system that needs to calculate how many lines a string will occupy when displayed. Each lowercase letter has a specific pixel width, and each line can hold a maximum of 100 pixels.
Given an array widths where widths[0] is the pixel width of the letter 'a', widths[1] is the pixel width of 'b', and so on through widths[25] for 'z', and a string s containing only lowercase letters, determine:
Characters must be placed sequentially without reordering. When adding the next character would cause the current line to exceed 100 pixels, start a new line with that character.
Return your answer as an array [total_lines, last_line_width].
widths.length == 262 <= widths[i] <= 101 <= s.length <= 1000s contains only lowercase English lettersExample 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 = "abcdefghij" Output: [1, 100] Explanation: Each letter has width 10. All 10 letters fit exactly on one line (10 × 10 = 100 pixels).
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:
Example 3:
Input: widths = [20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20], s = "zzzzz" Output: [1, 100] Explanation: Five 'z' characters each with width 20 fit exactly on one line (5 × 20 = 100 pixels).