Tap here to switch tabs
Problem
Submissions

Problem: Minimum Cost to Separate Sentence Into Rows

Minimum Cost to Separate Sentence Into Rows asks you to arrange an ordered string of words into rows no longer than k, minimizing squared unused space on every row except the last.

Statement

You are given a string sentence containing words separated by single spaces, and an integer k.

You need to split sentence into multiple rows such that:

  • The words in each row remain in their original order.

  • The words within each row are separated by a single space.

  • The total length of any row does not exceed k.

The cost of each row with length LL is (k−L)2(k - L)^2, except for the last row which has a cost of 00.

Return the minimum total cost to divide sentence into rows.

Constraints:

  • 1≤1 \leq sentence.length ≤5000\leq 5000

  • 1≤1 \leq k ≤5000\leq 5000

  • The length of each word in sentence is at most k.

  • sentence consists of only lowercase English letters and spaces.

  • sentence does not begin or end with a space.

  • Words in sentence are separated by a single space.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Cost to Separate Sentence Into Rows

Minimum Cost to Separate Sentence Into Rows asks you to arrange an ordered string of words into rows no longer than k, minimizing squared unused space on every row except the last.

Statement

You are given a string sentence containing words separated by single spaces, and an integer k.

You need to split sentence into multiple rows such that:

  • The words in each row remain in their original order.

  • The words within each row are separated by a single space.

  • The total length of any row does not exceed k.

The cost of each row with length LL is (k−L)2(k - L)^2, except for the last row which has a cost of 00.

Return the minimum total cost to divide sentence into rows.

Constraints:

  • 1≤1 \leq sentence.length ≤5000\leq 5000

  • 1≤1 \leq k ≤5000\leq 5000

  • The length of each word in sentence is at most k.

  • sentence consists of only lowercase English letters and spaces.

  • sentence does not begin or end with a space.

  • Words in sentence are separated by a single space.