-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
70 lines (69 loc) · 1.91 KB
/
Copy pathsolution.java
File metadata and controls
70 lines (69 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// 68. Text Justification
// https://leetcode.com/problems/text-justification/
// Hard | Java | Accepted 2026-08-11
// Runtime 1 ms | Memory 43.2 MB
class Solution {
public List<String> fullJustify(String[] words, int maxWidth) {
int i = 0;
List<String> ans = new ArrayList<>();
while(i<words.length)
{
int count = words[i].length();
List<String> temp = new ArrayList<>();
temp.add(words[i]);
while(i+1<words.length && count+words[i+1].length()<maxWidth && maxWidth-count-words[i+1].length()>=temp.size())
{
temp.add(words[i+1]); //This, is, an 3 exam, 12
count+=words[i+1].length(); //8
i++;
}
StringBuilder tt = new StringBuilder();
int res = maxWidth-count; //16-12 = 4
int size = temp.size()-1; //2;
if(i==words.length-1)
{
for(String p : temp)
{
tt.append(p);
if(!temp.get(temp.size()-1).equals(p))
{
tt.append(" ");
}
}
int rest = maxWidth - tt.length();
tt.append(" ".repeat(rest));
}
else if(size==0)
{
tt.append(temp.get(0));
int rest = maxWidth-temp.get(0).length();
tt.append(" ".repeat(rest));
}
else
{
for(String t : temp)
{
tt.append(t); //This is
int numSpaces = 0;
if(res>0&&size>0)
{
if(res%size!=0)
{
numSpaces = res/size+1; //2
}
else
{
numSpaces = res/size; //1
}
res-=numSpaces;
tt.append(" ".repeat(numSpaces));
} //2
size--;
}
}
ans.add(tt.toString());
i++;
}
return ans;
}
}