Advertisement
thewitchking

Untitled

Mar 20th, 2025
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.56 KB | None | 0 0
  1.  public static long getMaxCost(String s) {
  2.         long cost = 0;
  3.         long ones = 0;
  4.         long zeroes = 0;
  5.  
  6.         // Remove leading zeros and append "1" to handle final group
  7.         s = s.replaceAll("^0+", "") + "1";
  8.  
  9.         for (char ch : s.toCharArray()) {
  10.             if (ch == '1') {
  11.                 if (zeroes > 0) {
  12.                     cost += ones * (zeroes + 1);
  13.                 }
  14.                 ones += 1;
  15.                 zeroes = 0;
  16.             } else {
  17.                 zeroes += 1;
  18.             }
  19.         }
  20.  
  21.         return cost;
  22.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement