返回介绍

solution / 0600-0699 / 0664.Strange Printer / README_EN

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

664. Strange Printer

中文文档

Description

There is a strange printer with the following two special properties:

  • The printer can only print a sequence of the same character each time.
  • At each turn, the printer can print new characters starting from and ending at any place and will cover the original existing characters.

Given a string s, return _the minimum number of turns the printer needed to print it_.

 

Example 1:

Input: s = "aaabbb"
Output: 2
Explanation: Print "aaa" first and then print "bbb".

Example 2:

Input: s = "aba"
Output: 2
Explanation: Print "aaa" first and then print "b" from the second place of the string, which will cover the existing character 'a'.

 

Constraints:

  • 1 <= s.length <= 100
  • s consists of lowercase English letters.

Solutions

Solution 1: Dynamic Programming

We define $f[i][j]$ as the minimum operations to print $s[i..j]$, with the initial value $f[i][j]=\infty$, and the answer is $f[0][n-1]$, where $n$ is the length of string $s$.

Consider $f[i][j]$, if $s[i] = s[j]$, we can print $s[j]$ when print $s[i]$, so we can ignore $s[j]$ and continue to print $s[i+1..j-1]$. If $s[i] \neq s[j]$, we need to print the substring separately, i.e. $s[i..k]$ and $s[k+1..j]$, where $k \in [i,j)$. So we can have the following transition equation:

$$ f[i][j]= \begin{cases} 1, & \text{if } i=j \ f[i][j-1], & \text{if } s[i]=s[j] \ \min_{i \leq k < j} {f[i][k]+f[k+1][j]}, & \text{otherwise} \end{cases} $$

We can enumerate $i$ from large to small and $j$ from small to large, so that we can ensure that $f[i][j-1]$, $f[i][k]$ and $f[k+1][j]$ have been calculated when we calculate $f[i][j]$.

The time complexity is $O(n^3)$ and the space complexity is $O(n^2)$. Where $n$ is the length of string $s$.

class Solution:
  def strangePrinter(self, s: str) -> int:
    n = len(s)
    f = [[inf] * n for _ in range(n)]
    for i in range(n - 1, -1, -1):
      f[i][i] = 1
      for j in range(i + 1, n):
        if s[i] == s[j]:
          f[i][j] = f[i][j - 1]
        else:
          for k in range(i, j):
            f[i][j] = min(f[i][j], f[i][k] + f[k + 1][j])
    return f[0][-1]
class Solution {
  public int strangePrinter(String s) {
    final int inf = 1 << 30;
    int n = s.length();
    int[][] f = new int[n][n];
    for (var g : f) {
      Arrays.fill(g, inf);
    }
    for (int i = n - 1; i >= 0; --i) {
      f[i][i] = 1;
      for (int j = i + 1; j < n; ++j) {
        if (s.charAt(i) == s.charAt(j)) {
          f[i][j] = f[i][j - 1];
        } else {
          for (int k = i; k < j; ++k) {
            f[i][j] = Math.min(f[i][j], f[i][k] + f[k + 1][j]);
          }
        }
      }
    }
    return f[0][n - 1];
  }
}
class Solution {
public:
  int strangePrinter(string s) {
    int n = s.size();
    int f[n][n];
    memset(f, 0x3f, sizeof(f));
    for (int i = n - 1; ~i; --i) {
      f[i][i] = 1;
      for (int j = i + 1; j < n; ++j) {
        if (s[i] == s[j]) {
          f[i][j] = f[i][j - 1];
        } else {
          for (int k = i; k < j; ++k) {
            f[i][j] = min(f[i][j], f[i][k] + f[k + 1][j]);
          }
        }
      }
    }
    return f[0][n - 1];
  }
};
func strangePrinter(s string) int {
  n := len(s)
  f := make([][]int, n)
  for i := range f {
    f[i] = make([]int, n)
    for j := range f[i] {
      f[i][j] = 1 << 30
    }
  }
  for i := n - 1; i >= 0; i-- {
    f[i][i] = 1
    for j := i + 1; j < n; j++ {
      if s[i] == s[j] {
        f[i][j] = f[i][j-1]
      } else {
        for k := i; k < j; k++ {
          f[i][j] = min(f[i][j], f[i][k]+f[k+1][j])
        }
      }
    }
  }
  return f[0][n-1]
}
function strangePrinter(s: string): number {
  const n = s.length;
  const f: number[][] = new Array(n).fill(0).map(() => new Array(n).fill(1 << 30));
  for (let i = n - 1; i >= 0; --i) {
    f[i][i] = 1;
    for (let j = i + 1; j < n; ++j) {
      if (s[i] === s[j]) {
        f[i][j] = f[i][j - 1];
      } else {
        for (let k = i; k < j; ++k) {
          f[i][j] = Math.min(f[i][j], f[i][k] + f[k + 1][j]);
        }
      }
    }
  }
  return f[0][n - 1];
}

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

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

发布评论

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