This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
TextJustification.cs
78 lines (66 loc) · 2.77 KB
/
TextJustification.cs
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
73
74
75
76
77
78
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CS.Problems.DynamicProgramming
{
/// <summary>
/// problem details below
/// https://leetcode.com/problems/text-justification/#/description
/// </summary>
public class TextJustification
{
public static int GetJustification(List<string> words, int maxLineWidth)
{
//all other lines above last line
return GetJustification(words, maxLineWidth, words.Count - 1, new Dictionary<int, int>());
}
/// <summary>
/// DP top down
/// </summary>
/// <param name="words"></param>
/// <param name="maxLineWidth"></param>
/// <param name="currentLinePos"></param>
/// <param name="nextWordIndex"></param>
/// <returns></returns>
private static int GetJustification(List<string> words, int maxLineWidth,
int nextWordIndex, Dictionary<int, int> cache)
{
//base case
if (nextWordIndex == 0)
{
var spaceLength = maxLineWidth - words[0].Length;
return spaceLength * spaceLength;
}
if(cache.ContainsKey(nextWordIndex))
{
return cache[nextWordIndex];
}
var localMin = int.MaxValue;
//track current line progress
var totalWordsInCurrentLine = 0;
var totalCharacterWidthInCurrentLine = 0;
//just simulate breaking lines between every word
//or group of words that will fit in current line
for (int i = nextWordIndex; i > 0 && totalCharacterWidthInCurrentLine < maxLineWidth; i--)
{
totalWordsInCurrentLine++;
totalCharacterWidthInCurrentLine += words[i].Length;
//empty space at the end of the line
//will equal to max line width - total word width - (width of spaces between words)
//width of spaces b/w words will equal to (total words in current line - 1)
var emptyEndSpaceWidthOnCurrentLine = (maxLineWidth - totalCharacterWidthInCurrentLine)
- (totalWordsInCurrentLine - 1);
//previos optimal result
var prevLineMin = GetJustification(words, maxLineWidth, i - 1, cache);
//use squares/or cubes of empty space lengths at the end of the line
//to amplify the spaces in each line
localMin = Math.Min(localMin , (prevLineMin
+ (emptyEndSpaceWidthOnCurrentLine * emptyEndSpaceWidthOnCurrentLine)));
}
cache.Add(nextWordIndex, localMin);
return localMin;
}
}
}