# Autumn leaves collection

difficulty**secondary**

Xiaokou went out for an autumn outing. On the way, he collected some red and yellow leaves. He used these leaves to organize a collection of autumn leaves`leaves`

, string`leaves`

Contains only lowercase characters`r`

and`y`

, where characters`r`

Represents a red leaf`y`

A yellow leaf is a leaf.

For the sake of beauty and tidiness, Xiaokou wants to adjust the arrangement of leaves in the collection into three parts: red, yellow and red. The number of leaves in each part may not be equal, but they should be greater than or equal to 1. With each adjustment, the button can replace a red leaf with a yellow leaf or a yellow leaf with a red leaf. I would like to ask the minimum number of small button adjustment operations in order to complete the adjustment of autumn leaf collection.

Example 1:

Input: leaves = “rrryyyryryyrr”

Output: 2

Explanation: adjust twice and replace the two red leaves in the middle with yellow leaves to get “rrryyyyyyyyrr”

Example 2:

Input: leaves = “RyR”

Output: 0

Explanation: the requirements have been met and no additional operation is required

Tips:

3 <= leaves.length <= 10^5

Leaves contains only the character ‘R’ and the character ‘y’

# thinking

At the beginning, the idea was simply to find out the relevant information of R and Y on the left and R and Y on the right through several iterations, and then discussed all the situations. It was found that there were a lot of situations and a lot of judgment was needed

After a long struggle, it is found that the most appropriate method is dynamic programming,**I feel that DP can be used as long as the least or most of the problems appear, because the original intention of dynamic programming is to find an optimal solution through some calculated information.****Since we want to use DP, we need to find the state, because we need to integrate the string into [red, yellow, red], so we can define three states: 0 and 2 represent the front red and the following red, and 1 represents the middle yellow**

- Then we can define a two-dimensional array
`int[leaves.length()][3]`

，**DP [i] [J] denotes leaves [0 The minimum number of operations with string status J for.. I]** **The first is the state 0, that is, the front part is [red], then the current**`dp[i][0]`

It depends on the minimum number of operations of the previous character in state 0 plus whether the standard is red!**Then there is state 1, that is, the front part is [red, yellow], which is currently**`dp[i][1]`

That is to take the minimum value of the previous character in state 0 and state 1（*Because the front is [red] or [red, yellow] can, after all, the standard will be adjusted to yellow, and make 0 . I must be state 1!*）Plus whether the standard is yellow!**Finally, it is state 2, that is, the front part is [red, yellow, red]!, then the current**`dp[i][2]`

It is to take the minimum value of the previous character in state 1 and state 2 (* because state 1 is [red, yellow], and state 2 is [red, yellow, red]. After all, the standard will be adjusted to red, making 0 I must satisfy [red, yellow, red], but state 0 is not allowed, because no matter how to adjust the situation of [red], it can not be adjusted to [red, yellow, red]! *) plus whether the standard is red!

#### be careful

Since the number of leaves in each state must be at least 1, we should not only pay special attention to the state transition equation when J = 2, but also consider whether each state itself meets the requirements.**For state DP [i] [J], it includes leaves [0.. I] with I + 1 leaves and j + 1 states, so the number of leaves must be greater than or equal to the number of States, that is, I ≥ J. In this way, all the state DP [i] [J] of I < J is not qualified.**Looking at the above state transition equation, we take the minimum value in each step of transition, so we can set all the states that do not meet the requirements to a maximum value (such as N + 1 or upper limit of integer type, etc.).

#### This method uses the previous state information to accelerate the state collection on the standard, and finally find the optimal solution is DP, so it is very important to find out the correct state and state equation!

Here’s the code:

```
class Solution {
public int minimumOperations(String leaves) {
int len = leaves.length();
int[][] dp = new int[len][3];
dp[0][0] = leaves.charAt(0) == 'y' ? 1 : 0;
dp[0][1] = dp[0][2] = dp[1][2] = Integer.MAX_VALUE;//Note that the number of leaves must be greater than or equal to the total number of States!
for (int i = 1; i < n; ++i) {
int isRed = leaves.charAt(i) == 'r' ? 1 : 0;
int isYellow = leaves.charAt(i) == 'y' ? 1 : 0;
//The following three steps need to be well experienced!
dp[i][0] = dp[i - 1][0] + isYellow;
dp[i][1] = Math.min(dp[i - 1][0], dp[i - 1][1]) + isRed;
if (i >= 2) {
dp[i][2] = Math.min(dp[i - 1][1], dp[i - 1][2]) + isYellow;
}
}
return dp[len - 1][2];
}
}
```

This work adoptsCC agreementThe author and the link to this article must be indicated in the reprint