返回介绍

solution / 2000-2099 / 2052.Minimum Cost to Separate Sentence Into Rows / README_EN

发布于 2024-06-17 01:03:11 字数 7373 浏览 0 评论 0 收藏 0

2052. Minimum Cost to Separate Sentence Into Rows

中文文档

Description

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" and k = 12:
    • 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", and "love leetcode" is not possible because the length of "love leetcode" is greater than k.

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 <= 5000
  • 1 <= k <= 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.

Solutions

Solution 1

class Solution:
  def minimumCost(self, sentence: str, k: int) -> int:
    @cache
    def dfs(i):
      if s[-1] - s[i] + n - i - 1 <= k:
        return 0
      ans, j = inf, i + 1
      while j < n and (t := s[j] - s[i] + j - i - 1) <= k:
        ans = min(ans, (k - t) ** 2 + dfs(j))
        j += 1
      return ans

    t = [len(w) for w in sentence.split()]
    n = len(t)
    s = list(accumulate(t, initial=0))
    return dfs(0)
class Solution {
  private static final int INF = Integer.MAX_VALUE;
  private int[] memo;
  private int[] s;
  private int n;

  public int minimumCost(String sentence, int k) {
    String[] words = sentence.split(" ");
    n = words.length;
    s = new int[n + 1];
    for (int i = 0; i < n; ++i) {
      s[i + 1] = s[i] + words[i].length();
    }
    memo = new int[n];
    Arrays.fill(memo, INF);
    return dfs(0, k);
  }

  private int dfs(int i, int k) {
    if (memo[i] != INF) {
      return memo[i];
    }
    if (s[n] - s[i] + n - i - 1 <= k) {
      memo[i] = 0;
      return 0;
    }
    int ans = INF;
    for (int j = i + 1; j < n; ++j) {
      int t = s[j] - s[i] + j - i - 1;
      if (t <= k) {
        ans = Math.min(ans, (k - t) * (k - t) + dfs(j, k));
      }
    }
    memo[i] = ans;
    return ans;
  }
}
class Solution {
public:
  const int inf = INT_MAX;
  int n;

  int minimumCost(string sentence, int k) {
    istringstream is(sentence);
    vector<string> words;
    string word;
    while (is >> word) words.push_back(word);
    n = words.size();
    vector<int> s(n + 1);
    for (int i = 0; i < n; ++i) s[i + 1] = s[i] + words[i].size();
    vector<int> memo(n, inf);
    return dfs(0, k, s, memo);
  }

  int dfs(int i, int k, vector<int>& s, vector<int>& memo) {
    if (memo[i] != inf) return memo[i];
    if (s[n] - s[i] + n - i - 1 <= k) {
      memo[i] = 0;
      return 0;
    }
    int ans = inf;
    for (int j = i + 1; j < n; ++j) {
      int t = s[j] - s[i] + j - i - 1;
      if (t <= k) ans = min(ans, (k - t) * (k - t) + dfs(j, k, s, memo));
    }
    memo[i] = ans;
    return ans;
  }
};
func minimumCost(sentence string, k int) int {
  words := strings.Split(sentence, " ")
  n := len(words)
  inf := math.MaxInt32
  s := make([]int, n+1)
  for i, word := range words {
    s[i+1] = s[i] + len(word)
  }
  memo := make([]int, n)
  for i := range memo {
    memo[i] = inf
  }
  var dfs func(int) int
  dfs = func(i int) int {
    if memo[i] != inf {
      return memo[i]
    }
    if s[n]-s[i]+n-i-1 <= k {
      memo[i] = 0
      return 0
    }
    ans := inf
    for j := i + 1; j < n; j++ {
      t := s[j] - s[i] + j - i - 1
      if t <= k {
        ans = min(ans, (k-t)*(k-t)+dfs(j))
      }
    }
    memo[i] = ans
    return ans
  }
  return dfs(0)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文