|
| 1 | +# Text Justification with Dynamic Programming |
| 2 | + |
| 3 | +## English |
| 4 | + |
| 5 | +Text justification is a classic problem in document formatting and typesetting. Dynamic programming provides an optimal solution by minimizing the penalty of uneven spacing, improving readability and aesthetics. |
| 6 | + |
| 7 | +This challenge involves implementing a dynamic programming algorithm to justify text optimally, minimizing penalties for irregular spaces. |
| 8 | + |
| 9 | +### Relevant Code Snippet |
| 10 | + |
| 11 | +```javascript |
| 12 | +class TextJustifier { |
| 13 | + constructor(words, maxWidth) { |
| 14 | + this.words = words; |
| 15 | + this.maxWidth = maxWidth; |
| 16 | + this.n = words.length; |
| 17 | + this.dp = new Array(this.n + 1).fill(Infinity); |
| 18 | + this.dp[this.n] = 0; |
| 19 | + this.breaks = new Array(this.n + 1).fill(-1); |
| 20 | + } |
| 21 | + |
| 22 | + _cost(i, j) { |
| 23 | + const length = |
| 24 | + this.words.slice(i, j + 1).reduce((acc, w) => acc + w.length, 0) + |
| 25 | + (j - i); |
| 26 | + if (length > this.maxWidth) { |
| 27 | + return Infinity; |
| 28 | + } |
| 29 | + return Math.pow(this.maxWidth - length, 3); |
| 30 | + } |
| 31 | + |
| 32 | + justify() { |
| 33 | + for (let i = this.n - 1; i >= 0; i--) { |
| 34 | + for (let j = i; j < this.n; j++) { |
| 35 | + const cost = this._cost(i, j); |
| 36 | + if (cost === Infinity) { |
| 37 | + break; |
| 38 | + } |
| 39 | + if (this.dp[j + 1] + cost < this.dp[i]) { |
| 40 | + this.dp[i] = this.dp[j + 1] + cost; |
| 41 | + this.breaks[i] = j; |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + const lines = []; |
| 47 | + let i = 0; |
| 48 | + while (i < this.n) { |
| 49 | + const j = this.breaks[i]; |
| 50 | + const lineWords = this.words.slice(i, j + 1); |
| 51 | + const line = this._justifyLine(lineWords); |
| 52 | + lines.push(line); |
| 53 | + i = j + 1; |
| 54 | + } |
| 55 | + return lines; |
| 56 | + } |
| 57 | + |
| 58 | + _justifyLine(lineWords) { |
| 59 | + if (lineWords.length === 1) { |
| 60 | + return lineWords[0] + " ".repeat(this.maxWidth - lineWords[0].length); |
| 61 | + } |
| 62 | + |
| 63 | + const totalSpaces = |
| 64 | + this.maxWidth - lineWords.reduce((acc, w) => acc + w.length, 0); |
| 65 | + const spacesBetweenWords = lineWords.length - 1; |
| 66 | + const space = Math.floor(totalSpaces / spacesBetweenWords); |
| 67 | + const extra = totalSpaces % spacesBetweenWords; |
| 68 | + |
| 69 | + let line = ""; |
| 70 | + for (let i = 0; i < lineWords.length - 1; i++) { |
| 71 | + line += lineWords[i] + " ".repeat(space + (i < extra ? 1 : 0)); |
| 72 | + } |
| 73 | + line += lineWords[lineWords.length - 1]; |
| 74 | + return line; |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +### History |
| 80 | + |
| 81 | +Text justification has been extensively studied in document formatting and typesetting, with dynamic programming providing an optimal solution to improve readability and aesthetics. |
| 82 | + |
| 83 | +--- |
| 84 | + |
| 85 | +## Español |
| 86 | + |
| 87 | +Justificación de Texto con Programación Dinámica |
| 88 | + |
| 89 | +La justificación de texto es un problema clásico en el formateo y composición de documentos. La programación dinámica proporciona una solución óptima al minimizar la penalización por espacios desiguales, mejorando la legibilidad y estética. |
| 90 | + |
| 91 | +Este reto consiste en implementar un algoritmo de programación dinámica para justificar texto de manera óptima, minimizando penalizaciones por espacios irregulares. |
| 92 | + |
| 93 | +### Fragmento de Código Relevante |
| 94 | + |
| 95 | +```javascript |
| 96 | +class TextJustifier { |
| 97 | + constructor(words, maxWidth) { |
| 98 | + this.words = words; |
| 99 | + this.maxWidth = maxWidth; |
| 100 | + this.n = words.length; |
| 101 | + this.dp = new Array(this.n + 1).fill(Infinity); |
| 102 | + this.dp[this.n] = 0; |
| 103 | + this.breaks = new Array(this.n + 1).fill(-1); |
| 104 | + } |
| 105 | + |
| 106 | + _cost(i, j) { |
| 107 | + const length = |
| 108 | + this.words.slice(i, j + 1).reduce((acc, w) => acc + w.length, 0) + |
| 109 | + (j - i); |
| 110 | + if (length > this.maxWidth) { |
| 111 | + return Infinity; |
| 112 | + } |
| 113 | + return Math.pow(this.maxWidth - length, 3); |
| 114 | + } |
| 115 | + |
| 116 | + justify() { |
| 117 | + for (let i = this.n - 1; i >= 0; i--) { |
| 118 | + for (let j = i; j < this.n; j++) { |
| 119 | + const cost = this._cost(i, j); |
| 120 | + if (cost === Infinity) { |
| 121 | + break; |
| 122 | + } |
| 123 | + if (this.dp[j + 1] + cost < this.dp[i]) { |
| 124 | + this.dp[i] = this.dp[j + 1] + cost; |
| 125 | + this.breaks[i] = j; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + const lines = []; |
| 131 | + let i = 0; |
| 132 | + while (i < this.n) { |
| 133 | + const j = this.breaks[i]; |
| 134 | + const lineWords = this.words.slice(i, j + 1); |
| 135 | + const line = this._justifyLine(lineWords); |
| 136 | + lines.push(line); |
| 137 | + i = j + 1; |
| 138 | + } |
| 139 | + return lines; |
| 140 | + } |
| 141 | + |
| 142 | + _justifyLine(lineWords) { |
| 143 | + if (lineWords.length === 1) { |
| 144 | + return lineWords[0] + " ".repeat(this.maxWidth - lineWords[0].length); |
| 145 | + } |
| 146 | + |
| 147 | + const totalSpaces = |
| 148 | + this.maxWidth - lineWords.reduce((acc, w) => acc + w.length, 0); |
| 149 | + const spacesBetweenWords = lineWords.length - 1; |
| 150 | + const space = Math.floor(totalSpaces / spacesBetweenWords); |
| 151 | + const extra = totalSpaces % spacesBetweenWords; |
| 152 | + |
| 153 | + let line = ""; |
| 154 | + for (let i = 0; i < lineWords.length - 1; i++) { |
| 155 | + line += lineWords[i] + " ".repeat(space + (i < extra ? 1 : 0)); |
| 156 | + } |
| 157 | + line += lineWords[lineWords.length - 1]; |
| 158 | + return line; |
| 159 | + } |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +### Historia |
| 164 | + |
| 165 | +La justificación de texto ha sido ampliamente estudiada en el formateo y composición de documentos, y la programación dinámica proporciona una solución óptima para mejorar la legibilidad y estética. |
0 commit comments