Skip to content

Commit 8adb015

Browse files
authored
2022-09-30 update: added "Number of Lines To Write String" (#118)
1 parent e1d4031 commit 8adb015

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
154154
| 783. Minimum Distance Between BST Nodes | [Link](https://leetcode.com/problems/minimum-distance-between-bst-nodes/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/easy/MinimumDistanceBetweenBSTNodes.java) |
155155
| 796. Rotate String | [Link](https://leetcode.com/problems/rotate-string/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/easy/RotateString.java) |
156156
| 804. Unique Morse Code Words | [Link](https://leetcode.com/problems/unique-morse-code-words/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/easy/UniqueMorseCodeWords.java) |
157+
| 806. Number of Lines To Write String | [Link](https://leetcode.com/problems/number-of-lines-to-write-string/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/easy/NumberOfLinesToWriteString.java) |
157158
| 812. Largest Triangle Area | [Link](https://leetcode.com/problems/largest-triangle-area/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/easy/LargestTriangleArea.java) |
158159
| 819. Most Common Word | [Link](https://leetcode.com/problems/most-common-word/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/easy/MostCommonWord.java) |
159160
| 821. Shortest Distance to a Character | [Link](https://leetcode.com/problems/shortest-distance-to-a-character/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/easy/ShortestDistanceToACharacter.java) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.smlnskgmail.jaman.leetcodejava.easy;
2+
3+
// https://leetcode.com/problems/number-of-lines-to-write-string/
4+
public class NumberOfLinesToWriteString {
5+
6+
private final int[] widths;
7+
private final String s;
8+
9+
public NumberOfLinesToWriteString(int[] widths, String s) {
10+
this.widths = widths;
11+
this.s = s;
12+
}
13+
14+
public int[] solution() {
15+
int lines = 1;
16+
int width = 0;
17+
for (int i = 0; i < s.length(); i++) {
18+
char c = s.charAt(i);
19+
int w = widths[c - 'a'];
20+
width += w;
21+
if (width > 100) {
22+
lines++;
23+
width = w;
24+
}
25+
}
26+
return new int[]{lines, width};
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.smlnskgmail.jaman.leetcodejava.easy;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertArrayEquals;
6+
7+
public class NumberOfLinesToWriteStringTest {
8+
9+
@Test
10+
public void defaultTest() {
11+
assertArrayEquals(
12+
new int[]{3, 60},
13+
new NumberOfLinesToWriteString(
14+
new int[]{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},
15+
"abcdefghijklmnopqrstuvwxyz"
16+
).solution()
17+
);
18+
}
19+
20+
}

0 commit comments

Comments
 (0)