-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordWrapProblem.java
More file actions
72 lines (63 loc) · 1.97 KB
/
WordWrapProblem.java
File metadata and controls
72 lines (63 loc) · 1.97 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
71
72
package dp;
import java.util.Arrays;
import org.junit.Test;
import junit.framework.TestCase;
/**
* Link: http://www.geeksforgeeks.org/dynamic-programming-set-18-word-wrap/
*
* @author shivam.maharshi
*/
public class WordWrapProblem extends TestCase {
@Test
public static void test() {
assertEquals(13, get(new int[] { 5, 3, 5, 8, 4, 4, 7 }, 15));
}
/**
* A pretty tricky question. The idea is to create a square matrix of length
* equals to the numbers of input words. The store the minimal left space in
* dp to where dp[i][j] signify the left spaces if word i to j belongs in a
* line. Now with this dp, it is easy to calculate an arrangement such that
* the total sum is of left over space is minimum.
*/
public static int get(int[] w, int n) {
if (w == null || w.length == 0)
return 0;
int l = w.length, index = l - 1, to = l - 1;
int[][] dp = new int[l][l];
for (int[] d : dp)
Arrays.fill(d, Integer.MAX_VALUE);
for (int i = 0; i < l; i++) {
int t = w[i];
dp[i][i] = (n - t) * (n - t);
for (int j = i + 1; j < l; j++) {
t += w[j] + 1;
if (t <= n)
dp[i][j] = (n - t) * (n - t);
else
break;
}
}
int[] r = new int[l], arr = new int[l];
Arrays.fill(r, Integer.MAX_VALUE);
r[0] = dp[0][0];
for (int i = 1; i < l; i++)
for (int j = 0; j < i; j++)
if (dp[j][i] != Integer.MAX_VALUE) {
if (j == 0) {
r[i] = Math.min(r[i], dp[j][i]);
if (r[i] == dp[j][i])
arr[i] = j;
} else {
r[i] = Math.min(r[i], r[j - 1] + dp[j][i]);
if (r[i] == r[j - 1] + dp[j][i])
arr[i] = j;
}
}
while (index > 0) {
System.out.println("From: " + arr[index] + " :: To: " + to);
to = arr[index] - 1;
index = arr[index] - 1;
}
return r[l - 1];
}
}