Skip to content

Commit 3fbd84c

Browse files
committed
finish 68
1 parent b6d46e0 commit 3fbd84c

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

68. Text Justification.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/**
2+
* 68. Text Justification
3+
*
4+
* Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified.
5+
*
6+
* You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when necessary so that each line has exactly maxWidth characters.
7+
*
8+
* Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.
9+
*
10+
* For the last line of text, it should be left justified and no extra space is inserted between words.
11+
*
12+
* Note:
13+
*
14+
* A word is defined as a character sequence consisting of non-space characters only.
15+
* Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.
16+
* The input array words contains at least one word.
17+
*
18+
* Example 1:
19+
*
20+
* Input:
21+
* words = ["This", "is", "an", "example", "of", "text", "justification."]
22+
* maxWidth = 16
23+
* Output:
24+
* [
25+
* "This is an",
26+
* "example of text",
27+
* "justification. "
28+
* ]
29+
* Example 2:
30+
*
31+
* Input:
32+
* words = ["What","must","be","acknowledgment","shall","be"]
33+
* maxWidth = 16
34+
* Output:
35+
* [
36+
* "What must be",
37+
* "acknowledgment ",
38+
* "shall be "
39+
* ]
40+
* Explanation: Note that the last line is "shall be " instead of "shall be",
41+
* because the last line must be left-justified instead of fully-justified.
42+
* Note that the second line is also left-justified becase it contains only one word.
43+
* Example 3:
44+
*
45+
* Input:
46+
* words = ["Science","is","what","we","understand","well","enough","to","explain",
47+
* "to","a","computer.","Art","is","everything","else","we","do"]
48+
* maxWidth = 20
49+
* Output:
50+
* [
51+
* "Science is what we",
52+
* "understand well",
53+
* "enough to explain to",
54+
* "a computer. Art is",
55+
* "everything else we",
56+
* "do "
57+
* ]
58+
*/
59+
60+
/**
61+
* @param {string[]} words
62+
* @param {number} maxWidth
63+
* @return {string[]}
64+
*/
65+
var fullJustify = function(words, maxWidth) {
66+
var len = words.length;
67+
var arr = [];
68+
var width = 0;
69+
var item = null;
70+
var addLen = 0;
71+
var res = [];
72+
73+
for (var i = 0; i < len; i++) {
74+
item = words[i];
75+
addLen = width === 0 ? item.length : (item.length + 1);
76+
77+
if (width + addLen > maxWidth) {
78+
res.push(helper(arr, maxWidth - width, false));
79+
arr = [];
80+
width = 0;
81+
addLen = item.length;
82+
}
83+
84+
arr.push(item);
85+
width += addLen;
86+
}
87+
88+
res.push(helper(arr, maxWidth - width, true));
89+
90+
return res;
91+
};
92+
93+
var helper = function (arr, left, isLast) {
94+
var len = arr.length;
95+
var num = 0;
96+
var rem = 0;
97+
var res = '';
98+
99+
if (len === 1 || isLast) {
100+
return arr.join(' ') + ' '.repeat(left);
101+
}
102+
103+
num = Math.floor(left / (len - 1));
104+
rem = left % (len - 1);
105+
for (var i = 0; i < len; i++) {
106+
res += arr[i];
107+
if (i < len - 1) res += ' '.repeat(num + 1);
108+
if (i < rem) res += ' ';
109+
}
110+
111+
return res;
112+
};

0 commit comments

Comments
 (0)