题目:
解题思路:
倒序遍历找到每种垃圾车停的最远的房子,同时每处房子每种垃圾1分钟处理。
函数garbageLastCollectionDistance() 通过倒序遍历 garbage找到垃圾车走到的最远的房子,并计算花费的时间
int garbageLastCollectionDistance(char** garbage, int garbageSize, int* travel, char type) {
int result = 0;
for(int j = garbageSize-1; j > 0; j--) {
if(strchr(garbage[j], type) != NULL) {
for(int k = 0; k < j; k++) {
result += travel[k];
}
break;
}
}
return result;
}
int garbageCollection(char** garbage, int garbageSize, int* travel, int travelSize) {
int result = 0;
for(int i = 0; i < garbageSize; i++) {
result += strlen(garbage[i]);
}
result += garbageLastCollectionDistance(garbage, garbageSize, travel, 'G');
result += garbageLastCollectionDistance(garbage, garbageSize, travel, 'P');
result += garbageLastCollectionDistance(garbage, garbageSize, travel, 'M');
return result;
}