LeetCode664(2021.5.24)

LC664. 奇怪的打印机

class Solution {
public:
    int strangePrinter(string s) {
        int n = s.length();
        vector<vector<int>> f(n, vector<int>(n));
        for (int i = n - 1; i >= 0; 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 {
                    f[i][j] = 110;
                    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];
    }
};

发表评论