Skip to content

Commit 7189285

Browse files
refactor 806
1 parent bf70592 commit 7189285

File tree

1 file changed

+16
-56
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+16
-56
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,22 @@
11
package com.fishercoder.solutions;
22

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-
*/
433
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'];
4+
public static class Solution1 {
5+
public int[] numberOfLines(int[] widths, String S) {
6+
int numOfLines = 1;
7+
int offsetInCurrentLine = 0;
8+
for (char c : S.toCharArray()) {
9+
if (offsetInCurrentLine + widths[c - 'a'] < 100) {
10+
offsetInCurrentLine += widths[c - 'a'];
11+
} else if (offsetInCurrentLine + widths[c - 'a'] == 100) {
12+
numOfLines++;
13+
offsetInCurrentLine = 0;
14+
} else {
15+
numOfLines++;
16+
offsetInCurrentLine = widths[c - 'a'];
17+
}
18+
}
19+
return new int[]{numOfLines, offsetInCurrentLine};
5720
}
58-
}
59-
return new int[] {numOfLines, offsetInCurrentLine};
6021
}
61-
}
6222
}

0 commit comments

Comments
 (0)