Welcome to Subscribe On Youtube
2391. Minimum Amount of Time to Collect Garbage
Description
You are given a 0-indexed array of strings garbage
where garbage[i]
represents the assortment of garbage at the ith
house. garbage[i]
consists only of the characters 'M'
, 'P'
and 'G'
representing one unit of metal, paper and glass garbage respectively. Picking up one unit of any type of garbage takes 1
minute.
You are also given a 0-indexed integer array travel
where travel[i]
is the number of minutes needed to go from house i
to house i + 1
.
There are three garbage trucks in the city, each responsible for picking up one type of garbage. Each garbage truck starts at house 0
and must visit each house in order; however, they do not need to visit every house.
Only one garbage truck may be used at any given moment. While one truck is driving or picking up garbage, the other two trucks cannot do anything.
Return the minimum number of minutes needed to pick up all the garbage.
Example 1:
Input: garbage = ["G","P","GP","GG"], travel = [2,4,3] Output: 21 Explanation: The paper garbage truck: 1. Travels from house 0 to house 1 2. Collects the paper garbage at house 1 3. Travels from house 1 to house 2 4. Collects the paper garbage at house 2 Altogether, it takes 8 minutes to pick up all the paper garbage. The glass garbage truck: 1. Collects the glass garbage at house 0 2. Travels from house 0 to house 1 3. Travels from house 1 to house 2 4. Collects the glass garbage at house 2 5. Travels from house 2 to house 3 6. Collects the glass garbage at house 3 Altogether, it takes 13 minutes to pick up all the glass garbage. Since there is no metal garbage, we do not need to consider the metal garbage truck. Therefore, it takes a total of 8 + 13 = 21 minutes to collect all the garbage.
Example 2:
Input: garbage = ["MMM","PGM","GP"], travel = [3,10] Output: 37 Explanation: The metal garbage truck takes 7 minutes to pick up all the metal garbage. The paper garbage truck takes 15 minutes to pick up all the paper garbage. The glass garbage truck takes 15 minutes to pick up all the glass garbage. It takes a total of 7 + 15 + 15 = 37 minutes to collect all the garbage.
Constraints:
2 <= garbage.length <= 105
garbage[i]
consists of only the letters'M'
,'P'
, and'G'
.1 <= garbage[i].length <= 10
travel.length == garbage.length - 1
1 <= travel[i] <= 100
Solutions
-
class Solution { public int garbageCollection(String[] garbage, int[] travel) { int[] last = new int[26]; int n = garbage.length; int ans = 0; for (int i = 0; i < n; ++i) { int k = garbage[i].length(); ans += k; for (int j = 0; j < k; ++j) { last[garbage[i].charAt(j) - 'A'] = i; } } int m = travel.length; int[] s = new int[m + 1]; for (int i = 0; i < m; ++i) { s[i + 1] = s[i] + travel[i]; } for (int i : last) { ans += s[i]; } return ans; } }
-
class Solution { public: int garbageCollection(vector<string>& garbage, vector<int>& travel) { int n = garbage.size(), m = travel.size(); int last[26]{}; int ans = 0; for (int i = 0; i < n; ++i) { ans += garbage[i].size(); for (char& c : garbage[i]) { last[c - 'A'] = i; } } int s[m + 1]; s[0] = 0; for (int i = 1; i <= m; ++i) { s[i] = s[i - 1] + travel[i - 1]; } for (int i : last) { ans += s[i]; } return ans; } };
-
class Solution: def garbageCollection(self, garbage: List[str], travel: List[int]) -> int: ans = 0 last = {} for i, s in enumerate(garbage): ans += len(s) for c in s: last[c] = i s = list(accumulate(travel, initial=0)) ans += sum(s[i] for i in last.values()) return ans
-
func garbageCollection(garbage []string, travel []int) (ans int) { last := [26]int{} for i, s := range garbage { ans += len(s) for _, c := range s { last[c-'A'] = i } } s := make([]int, len(travel)+1) for i, x := range travel { s[i+1] = s[i] + x } for _, i := range last { ans += s[i] } return }
-
function garbageCollection(garbage: string[], travel: number[]): number { const n = garbage.length; const m = travel.length; let ans = 0; const last = new Array(26).fill(0); for (let i = 0; i < n; ++i) { ans += garbage[i].length; for (const c of garbage[i]) { last[c.charCodeAt(0) - 'A'.charCodeAt(0)] = i; } } const s = new Array(m + 1).fill(0); for (let i = 1; i <= m; ++i) { s[i] = s[i - 1] + travel[i - 1]; } for (const i of last) { ans += s[i]; } return ans; }
-
public class Solution { public int GarbageCollection(string[] garbage, int[] travel) { int len = garbage.Length; int res = 0; HashSet<char> s = new HashSet<char>(); for (int i = len - 1; i >= 0; i--) { foreach (char ch in garbage[i].ToCharArray()) { if (!s.Contains(ch)) s.Add(ch); } res += garbage[i].Length; res += i > 0 ? s.Count * travel[i - 1] : 0; } return res; } }
-
impl Solution { pub fn garbage_collection(garbage: Vec<String>, travel: Vec<i32>) -> i32 { let n = garbage.len(); let cs = [b'M', b'P', b'G']; let mut count = [0, 0, 0]; for s in garbage.iter() { for c in s.as_bytes().iter() { count[if c == &b'M' { 0 } else if c == &b'P' { 1 } else { 2 }] += 1; } } let mut res = 0; for i in 0..3 { for j in 0..n { let s = &garbage[j]; for c in s.as_bytes().iter() { if c == &cs[i] { res += 1; count[i] -= 1; } } if count[i] == 0 { break; } res += travel[j]; } } res } }