Skip to content

Commit 2baa306

Browse files
add 806
1 parent 1abc4fb commit 2baa306

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2424
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
25+
|806|[Number of Lines To Write String](https://leetcode.com/problems/number-of-lines-to-write-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_806.java) | O(n) | O(1) | |Easy|
2526
|799|[Champagne Tower](https://leetcode.com/problems/champagne-tower/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_799.java) | O(r^2) or O(1) | O(r^2) or O(1) | |Medium|
2627
|796|[Rotate String](https://leetcode.com/problems/rotate-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_796.java) | O(n) | O(1) | |Easy|
2728
|791|[Custom Sort String](https://leetcode.com/problems/custom-sort-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_791.java) | O(n+m) | O(1) | |Medium|
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 806. Number of Lines To Write String
5+
6+
We are to write the letters of a given string S, from left to right into lines.
7+
Each line has maximum width 100 units, and if writing a letter would cause the width of the line to exceed 100 units, it is written on the next line.
8+
We are given an array widths, an array where widths[0] is the width of 'a', widths[1] is the width of 'b', ..., and widths[25] is the width of 'z'.
9+
10+
Now answer two questions:
11+
how many lines have at least one character from S,
12+
and what is the width used by the last such line?
13+
Return your answer as an integer list of length 2.
14+
15+
Example :
16+
Input:
17+
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]
18+
S = "abcdefghijklmnopqrstuvwxyz"
19+
Output: [3, 60]
20+
Explanation:
21+
All letters have the same length of 10. To write all 26 letters,
22+
we need two full lines and one line with 60 units.
23+
24+
Example :
25+
Input:
26+
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]
27+
S = "bbbcccdddaaa"
28+
Output: [2, 4]
29+
Explanation:
30+
All letters except 'a' have the same length of 10, and
31+
"bbbcccdddaa" will cover 9 * 10 + 2 * 4 = 98 units.
32+
For the last 'a', it is written on the second line because
33+
there is only 2 units left in the first line.
34+
So the answer is 2 lines, plus 4 units in the second line.
35+
36+
Note:
37+
The length of S will be in the range [1, 1000].
38+
S will only contain lowercase letters.
39+
widths is an array of length 26.
40+
widths[i] will be in the range of [2, 10].
41+
42+
*/
43+
public class _806 {
44+
public static class Solution1 {
45+
public int[] numberOfLines(int[] widths, String S) {
46+
int numOfLines = 1;
47+
int offsetInCurrentLine = 0;
48+
for (char c : S.toCharArray()) {
49+
if (offsetInCurrentLine + widths[c - 'a'] < 100) {
50+
offsetInCurrentLine += widths[c - 'a'];
51+
} else if (offsetInCurrentLine + widths[c - 'a'] == 100) {
52+
numOfLines++;
53+
offsetInCurrentLine = 0;
54+
} else {
55+
numOfLines++;
56+
offsetInCurrentLine = widths[c - 'a'];
57+
}
58+
}
59+
return new int[] {numOfLines, offsetInCurrentLine};
60+
}
61+
}
62+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._806;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _806Test {
10+
private static _806.Solution1 solution1;
11+
private static int[] widths;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _806.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
widths =
21+
new int[] {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
22+
10, 10, 10, 10, 10, 10};
23+
assertArrayEquals(new int[] {3, 60}, solution1.numberOfLines(widths, "abcdefghijklmnopqrstuvwxyz"));
24+
}
25+
26+
@Test
27+
public void test2() {
28+
widths =
29+
new int[] {4, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
30+
10, 10, 10, 10, 10, 10};
31+
assertArrayEquals(new int[] {2, 4}, solution1.numberOfLines(widths, "bbbcccdddaaa"));
32+
}
33+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy