Guest User

Untitled

a guest
Feb 18th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.63 KB | None | 0 0
  1. public class RedAndGreen {
  2.     public int minPaints(String row) {
  3.         int totalRedCount = 0;
  4.         for (char c : row.toCharArray()) {
  5.             if (c == 'R') {
  6.                 totalRedCount++;
  7.             }
  8.         }
  9.  
  10.         int minRepaints = totalRedCount;
  11.         int index = 0;
  12.         int currentRedCount = 0;
  13.         for (char c : row.toCharArray()) {
  14.             if (c == 'R') {
  15.                 currentRedCount++;
  16.             }
  17.             int currentGreenCount = index - currentRedCount + 1;
  18.             int remainingRedCount = totalRedCount - currentRedCount;
  19.             int repaints = currentGreenCount + remainingRedCount;
  20.             if (repaints < minRepaints) {
  21.                 minRepaints = repaints;
  22.             }
  23.             index++;
  24.         }
  25.         return minRepaints;
  26.     }
  27. }
Add Comment
Please, Sign In to add comment