Skip to content

Commit 3518cd1

Browse files
committed
[LeetCode Sync] Runtime - 36 ms (74.08%), Memory - 17.9 MB (6.23%)
1 parent af44459 commit 3518cd1

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<p>You are given a string <code>s</code> of lowercase English letters and an array <code>widths</code> denoting <strong>how many pixels wide</strong> each lowercase English letter is. Specifically, <code>widths[0]</code> is the width of <code>&#39;a&#39;</code>, <code>widths[1]</code> is the width of <code>&#39;b&#39;</code>, and so on.</p>
2+
3+
<p>You are trying to write <code>s</code> across several lines, where <strong>each line is no longer than </strong><code>100</code><strong> pixels</strong>. Starting at the beginning of <code>s</code>, write as many letters on the first line such that the total width does not exceed <code>100</code> pixels. Then, from where you stopped in <code>s</code>, continue writing as many letters as you can on the second line. Continue this process until you have written all of <code>s</code>.</p>
4+
5+
<p>Return <em>an array </em><code>result</code><em> of length 2 where:</em></p>
6+
7+
<ul>
8+
<li><code>result[0]</code><em> is the total number of lines.</em></li>
9+
<li><code>result[1]</code><em> is the width of the last line in pixels.</em></li>
10+
</ul>
11+
12+
<p>&nbsp;</p>
13+
<p><strong class="example">Example 1:</strong></p>
14+
15+
<pre>
16+
<strong>Input:</strong> 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 = &quot;abcdefghijklmnopqrstuvwxyz&quot;
17+
<strong>Output:</strong> [3,60]
18+
<strong>Explanation:</strong> You can write s as follows:
19+
abcdefghij // 100 pixels wide
20+
klmnopqrst // 100 pixels wide
21+
uvwxyz // 60 pixels wide
22+
There are a total of 3 lines, and the last line is 60 pixels wide.</pre>
23+
24+
<p><strong class="example">Example 2:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong> 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 = &quot;bbbcccdddaaa&quot;
28+
<strong>Output:</strong> [2,4]
29+
<strong>Explanation:</strong> You can write s as follows:
30+
bbbcccdddaa // 98 pixels wide
31+
a // 4 pixels wide
32+
There are a total of 2 lines, and the last line is 4 pixels wide.</pre>
33+
34+
<p>&nbsp;</p>
35+
<p><strong>Constraints:</strong></p>
36+
37+
<ul>
38+
<li><code>widths.length == 26</code></li>
39+
<li><code>2 &lt;= widths[i] &lt;= 10</code></li>
40+
<li><code>1 &lt;= s.length &lt;= 1000</code></li>
41+
<li><code>s</code> contains only lowercase English letters.</li>
42+
</ul>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def numberOfLines(self, widths: List[int], s: str) -> List[int]:
3+
last, lines = 0, 1
4+
for width in map(lambda x: widths[ord(x) - ord('a')], s):
5+
if last + width <= 100:
6+
last += width
7+
else:
8+
lines += 1
9+
last = width
10+
11+
return [lines, last]

0 commit comments

Comments
 (0)