力扣题目:收集垃圾的最少总时间 题目链接: 2391.收集垃圾的最少总时间 题目描述 代码纯享版 class Solution { public int garbageCollection(String[] garbage, int[] travel) { int sum = 0; int last_M = -1,last_P = -1, last_G = -1; for(int i = 0; i < garbage.length; i++){ String str = garbage[i]; sum += str.length(); for(int j = 0; j < garbage[i].length(); j++){ char a = garbage[i].charAt(j); if(a == 'M') last_M = i; else if(a == 'P') last_P = i; else last_G = i; } } for(int i = 1; i <= Math.max(Math.max(last_M, last_P), last_G); i++){ if(last_M >= i) sum += travel[i - 1]; if(last_P >= i) sum += travel[i - 1]; if(last_G >= i) sum += travel[i - 1]; } return sum; } }