-
Notifications
You must be signed in to change notification settings - Fork 14
/
word wrap.java
56 lines (45 loc) · 1.8 KB
/
word wrap.java
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
/*package whatever //do not write package name here */
import java.io.*;
import java.util.Arrays;
public class WordWrapDpMemo {
private int solveWordWrap(int[] nums, int k) {
int[][] memo = new int[nums.length][k + 1];
for (int i = 0; i < nums.length; i++) {
Arrays.fill(memo[i], -1);
}
return solveWordWrapUsingMemo(nums, nums.length, k, 0, k, memo);
}
private int solveWordWrap(int[] words, int n, int length, int wordIndex, int remLength, int[][] memo) {
//base case for last word
if (wordIndex == n - 1) {
memo[wordIndex][remLength] = words[wordIndex] < remLength ? 0 : square(remLength);
return memo[wordIndex][remLength];
}
int currWord = words[wordIndex];
//if word can fit in the remaining line
if (currWord < remLength) {
return Math.min(
//if word is kept on same line
solveWordWrapUsingMemo(words, n, length, wordIndex + 1, remLength == length ? remLength - currWord : remLength - currWord - 1, memo),
//if word is kept on next line
square(remLength) + solveWordWrapUsingMemo(words, n, length, wordIndex + 1, length - currWord, memo)
);
} else {
//if word is kept on next line
return square(remLength) + solveWordWrapUsingMemo(words, n, length, wordIndex + 1, length - currWord, memo);
}
}
private int solveWordWrapUsingMemo(int[] words, int n, int length, int wordIndex, int remLength, int[][] memo) {
if (memo[wordIndex][remLength] != -1) {
return memo[wordIndex][remLength];
}
memo[wordIndex][remLength] = solveWordWrap(words, n, length, wordIndex, remLength, memo);
return memo[wordIndex][remLength];
}
private int square(int n) {
return n * n;
}
public static void main(String[] args) {
System.out.println(new WordWrapDpMemo().solveWordWrap(new int[]{3, 2, 2, 5}, 6));
}
}