Z05
Text Justification Line Breaking Cost
Problem Statement & Objective
Determine optimal line breaks to minimize total line rag/penalty in document formatting.
Function Signature (Python)
text_justification_line_breaking_cost(words: list[str], width: int) -> intInput & Output Contract
Output Rule:Negatif olmayan tam sayı; yerleşim imkânsızsa -1. Boş liste için 0.
System Constraints
0 <= kelimeler.length <= 500
1 <= genislik <= 100
1 <= kelimeler[i].lengthExample Execution Walkthrough
kelimeler = ["merhaba","dunya"], genislik = 10
satır 1: "merhaba" (7) -> artık 3 -> maliyet 3² = 9
satır 2: "dunya" -> SON SATIR -> maliyet 0
toplam = 9
cevap: 9
kelimeler = ["aaa", "bb", "cc", "ddddd"], genislik = 6
İlk satırı dolduran açgözlü yerleşim: "aaa bb" artık 0, sonra "cc" artık 4 (maliyet 16), son satır 0. Toplam 16.
Daha iyi: "aaa" artık 3 (maliyet 9), "bb cc" artık 1 (maliyet 1), son satır 0. Toplam 10.Example Test Scenarios (4)
Example 1
Input:
[["merhaba","dunya"],10]Expected Output:
9Example 2: Single Rowa Sığan (0 Maliyet)
Input:
[["kisa","metin"],15]Expected Output:
0Example 3: İki Satıra Bölünme
Input:
[["aa","bb","cc"],5]Expected Output:
0Example : Açgözlü Satır Doldurma Yanlış
Input:
[["aaa","bb","cc","ddddd"],6]Expected Output:
10