Skip to content

Commit 872436e

Browse files
committed
Add solution #806
1 parent e569297 commit 872436e

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,7 @@
615615
803|[Bricks Falling When Hit](./0803-bricks-falling-when-hit.js)|Hard|
616616
804|[Unique Morse Code Words](./0804-unique-morse-code-words.js)|Easy|
617617
805|[Split Array With Same Average](./0805-split-array-with-same-average.js)|Hard|
618+
806|[Number of Lines To Write String](./0806-number-of-lines-to-write-string.js)|Easy|
618619
819|[Most Common Word](./0819-most-common-word.js)|Easy|
619620
821|[Shortest Distance to a Character](./0821-shortest-distance-to-a-character.js)|Easy|
620621
824|[Goat Latin](./0824-goat-latin.js)|Easy|
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* 806. Number of Lines To Write String
3+
* https://leetcode.com/problems/number-of-lines-to-write-string/
4+
* Difficulty: Easy
5+
*
6+
* You are given a string s of lowercase English letters and an array widths denoting how many
7+
* pixels wide each lowercase English letter is. Specifically, widths[0] is the width of 'a',
8+
* widths[1] is the width of 'b', and so on.
9+
*
10+
* You are trying to write s across several lines, where each line is no longer than 100 pixels.
11+
* Starting at the beginning of s, write as many letters on the first line such that the total
12+
* width does not exceed 100 pixels. Then, from where you stopped in s, continue writing as many
13+
* letters as you can on the second line. Continue this process until you have written all of s.
14+
*
15+
* Return an array result of length 2 where:
16+
* - result[0] is the total number of lines.
17+
* - result[1] is the width of the last line in pixels.
18+
*/
19+
20+
/**
21+
* @param {number[]} widths
22+
* @param {string} s
23+
* @return {number[]}
24+
*/
25+
var numberOfLines = function(widths, s) {
26+
let lines = 1;
27+
let width = 0;
28+
29+
for (const char of s) {
30+
const charWidth = widths[char.charCodeAt(0) - 97];
31+
32+
if (width + charWidth > 100) {
33+
lines++;
34+
width = charWidth;
35+
} else {
36+
width += charWidth;
37+
}
38+
}
39+
40+
return [lines, width];
41+
};

0 commit comments

Comments
 (0)