-
Notifications
You must be signed in to change notification settings - Fork 5
/
WordWrap.java
71 lines (69 loc) · 1.99 KB
/
WordWrap.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*https://practice.geeksforgeeks.org/problems/word-wrap1646/1*/
//tle
class Solution
{
int minCost,n;
int[] dp;
public int solveWordWrap (int[] nums, int k)
{
// Code here
n = nums.length;
minCost = Integer.MAX_VALUE;
dp = new int[n+1];
Arrays.fill(dp,Integer.MAX_VALUE);
wordWrapSolver(nums, k, 0, 0);
return minCost;
}
public void wordWrapSolver(int[] nums, int k, int index, int currCost)
{
if (index >= n)
{
if (currCost < minCost) minCost = currCost;
return;
}
int charCount = 0, cost;
boolean startWord = true;
while (index < n && ((startWord && charCount+nums[index] <= k) || (!startWord && charCount+nums[index]+1 <= k)))
{
charCount += nums[index];
if (!startWord) ++charCount;
startWord = false;
++index;
if (index < n) cost = (k-charCount)*(k-charCount); else cost = 0;
if (currCost+cost < minCost && currCost+cost <= dp[index])
{
dp[index] = currCost+cost;
wordWrapSolver(nums, k, index, currCost+cost);
}
}
}
}
//efficient
class Solution
{
int n;
int[] dp;
public int solveWordWrap (int[] nums, int k)
{
// Code here
n = nums.length;
dp = new int[n+1];
Arrays.fill(dp,-1);
return wordWrapSolver(nums, k, 0);
}
public int wordWrapSolver(int[] nums, int k, int index)
{
if (index >= n) return 0;
if (dp[index] != -1) return dp[index];
int charCount = 0, cost = Integer.MAX_VALUE, i;
for (i = index; i < n; ++i)
{
if (i > index) ++charCount;
charCount += nums[i];
if (charCount > k) break;
if (i < n-1) cost = Math.min(cost,(k-charCount)*(k-charCount)+wordWrapSolver(nums, k, i+1)); else cost = 0;
}
dp[index] = cost;
return cost;
}
}