[LeetCode]#806. Number of Lines To Write String

  • result[0] is the total number of lines.
  • result[1] is the width of the last line in pixels.
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.
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.
  1. Check s in widths value
  2. Summary it.
  3. If summary >100, and lines+=1
  4. the last is last summary (Summary2 is below example).
class Solution(object):
def numberOfLines(self, widths, S):
lines = 1
ans = 0
for i in S:
width = widths[ord(i) - ord('a')]
ans += width
if ans > 100:
lines += 1
ans = width
return [lines, ans]

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fatboy Slim

Fatboy Slim

Interesting in any computer science.