Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RedAndGreen {
- public int minPaints(String row) {
- int totalRedCount = 0;
- for (char c : row.toCharArray()) {
- if (c == 'R') {
- totalRedCount++;
- }
- }
- int minRepaints = totalRedCount;
- int index = 0;
- int currentRedCount = 0;
- for (char c : row.toCharArray()) {
- if (c == 'R') {
- currentRedCount++;
- }
- int currentGreenCount = index - currentRedCount + 1;
- int remainingRedCount = totalRedCount - currentRedCount;
- int repaints = currentGreenCount + remainingRedCount;
- if (repaints < minRepaints) {
- minRepaints = repaints;
- }
- index++;
- }
- return minRepaints;
- }
- }
Add Comment
Please, Sign In to add comment