1 class Solution { 2 public: 3 int minimumOperations(string leaves) { 4 int n = leaves.size(); 5 vector<vector<int>> f(n, vector<int>(3)); 6 f[0][0] = (leaves[0] == 'y'); 7 f[0][1] = f[0][2] = f[1][2] = INT_MAX; 8 for (int i = 1; i < n; ++i) { 9 int isRed = (leaves[i] == 'r'); 10 int isYellow = (leaves[i] == 'y'); 11 f[i][0] = f[i - 1][0] + isYellow; 12 f[i][1] = min(f[i - 1][0], f[i - 1][1]) + isRed; 13 if (i >= 2) { 14 f[i][2] = min(f[i - 1][1], f[i - 1][2]) + isYellow; 15 } 16 } 17 return f[n - 1][2]; 18 } 19 };
看懂了 简单