2391. 收集垃圾的最少总时间

收集垃圾的最少总时间

解法一: 一次遍历

go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
func garbageCollection(garbage []string, travel []int) int {
n := len(garbage)
ans := 0
cur := 0
countMap := map[byte]int{
'M': 0,
'P': 0,
'G': 0,
}
travel = append(travel, 0)
for i := 0; i < n; i++ {
m := len(garbage[i])
ans += m
for j := 0; j < m; j++ {
countMap[garbage[i][j]] = cur
}
cur += travel[i]
}
return ans + countMap['M'] + countMap['P'] + countMap['G']
}
作者

wuhunyu

发布于

2024-05-11

更新于

2025-01-15

许可协议