Connection Status:
Competition Arena > RedAndGreen
SRM 521 · 2011-05-25 · by soul-net · Greedy
Class Name: RedAndGreen
Return Type: int
Method Name: minPaints
Arg Types: (string)
Problem Statement

Problem Statement

You have several squares arranged in a single row. Each square is currently painted red or green. You can choose any of the squares and paint it over with either color. The goal is that, after painting, every red square is further to the left than any of the green squares. We want you to do it repainting the minimum possible number of squares.

Squares are numbered from left to right. You will be given the initial arrangement as a String row, such that character i is 'R' if square i is red or 'G' if square i is green. Return the minimum number of repaints needed to achieve the goal.

Constraints

  • row will contain between 1 and 50 characters, inclusive.
  • Each character of row will be either 'R' or 'G'.
Examples
0)
"RGRGR"
Returns: 2

Paint the squares in the marked positions in the picture below with the opposite color. There are other ways with 2 total paints. RGRGR | | RRRGG

1)
"RRRGGGGG"
Returns: 0

There is no need to paint anything.

2)
"GGGGRRR"
Returns: 3

Paint all the red squares green.

3)
"RGRGRGRGRGRGRGRGR"
Returns: 8
4)
"RRRGGGRGGGRGGRRRGGRRRGR"
Returns: 9

Submissions are judged against all 167 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class RedAndGreen with a public method int minPaints(string row) · 167 test cases · 2 s / 256 MB per case

Submitting as anonymous