Minimum Cost to Separate Sentence Into Rows Problem
Description
LeetCode Problem 2052.
You are given a string sentence containing words separated by spaces, and an integer k. Your task is to separate sentence into rows where the number of characters in each row is at most k. You may assume that sentence does not begin or end with a space, and the words in sentence are separated by a single space.
You can split sentence into rows by inserting line breaks between words in sentence. A word cannot be split between two rows. Each word must be used exactly once, and the word order cannot be rearranged. Adjacent words in a row should be separated by a single space, and rows should not begin or end with spaces.
The cost of a row with length n is (k - n)^2, and the total cost is the sum of the costs for all rows except the last one.
- For example if
sentence = "i love leetcode"andk = 12:- Separating
sentenceinto"i","love", and"leetcode"has a cost of(12 - 1)^2 + (12 - 4)^2 = 185. - Separating
sentenceinto"i love", and"leetcode"has a cost of(12 - 6)^2 = 36. - Separating
sentenceinto"i", and"love leetcode"is not possible because the length of"love leetcode"is greater thank.
- Separating
Return the minimum possible total cost of separating sentence into rows.
Example 1:
Input: sentence = "i love leetcode", k = 12
Output: 36
Explanation:
Separating sentence into "i", "love", and "leetcode" has a cost of (12 - 1)^2 + (12 - 4)^2 = 185.
Separating sentence into "i love", and "leetcode" has a cost of (12 - 6)^2 = 36.
Separating sentence into "i", "love leetcode" is not possible because "love leetcode" has length 13.
36 is the minimum possible total cost so return it.
Example 2:
Input: sentence = "apples and bananas taste great", k = 7
Output: 21
Explanation
Separating sentence into "apples", "and", "bananas", "taste", and "great" has a cost of (7 - 6)^2 + (7 - 3)^2 + (7 - 7)^2 + (7 - 5)^2= 21.
21 is the minimum possible total cost so return it.
Example 3:
Input: sentence = "a", k = 5
Output: 0
Explanation:
The cost of the last row is not included in the total cost, and since there is only one row, return 0.
Constraints:
1 <= sentence.length <= 50001 <= k <= 5000- The length of each word in
sentenceis at mostk. sentenceconsists of only lowercase English letters and spaces.sentencedoes not begin or end with a space.- Words in
sentenceare separated by a single space.
Solution
Approach: Prefix Sum + Memoized Search
Split the sentence into words and keep a prefix sum s of the word lengths, so that the length of words i..j-1 placed on one row is s[j] - s[i] + (j - i - 1) (the last term counts the spaces).
Let dfs(i) be the minimum cost of arranging the words from index i to the end. If all remaining words fit on one row (length at most k), they form the free last row and the cost is 0. Otherwise try every j > i such that words i..j-1 fit on one row of length m; that row costs (k - m)^2, and the rest costs dfs(j). Take the minimum over j. Memoize dfs so each start index is solved once. The answer is dfs(0).
Complexity: time O(n^2), space O(n), where n is the number of words.
from math import inf
from itertools import accumulate
from functools import lru_cache
class Solution:
def minimumCost(self, sentence: str, k: int) -> int:
@lru_cache(None)
def dfs(i: int) -> int:
if s[n] - s[i] + n - i - 1 <= k:
return 0
ans = inf
j = i + 1
while j < n and (m := s[j] - s[i] + j - i - 1) <= k:
ans = min(ans, dfs(j) + (k - m) ** 2)
j += 1
return ans
nums = [len(s) for s in sentence.split()]
n = len(nums)
s = list(accumulate(nums, initial=0))
return dfs(0)