1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| class Solution { public int minCost(String s, int[] cost) { int result = 0; int n = s.length(); for (int i = 1; i < n; i++) { int j = i; while (j < n && s.charAt(j) == s.charAt(j - 1) ) { j++; } int total = 0; int max = 0; for (int k = i-1; k <= j-1; k++) { total += cost[k]; max = Math.max(max, cost[k]); } result += (total - max); i = j; } return result; } }
|