Sponsored
Sponsored
The goal is to calculate the minimum time required for each truck to collect all the garbage of its type. A key observation is that the truck only needs to travel as far as the last house that has a specific kind of garbage. We use a prefix sum approach to calculate travel times efficiently. Each truck collects the garbage as it travels to each house that contains its type of garbage.
Time Complexity: O(n * m) where n is the number of houses and m is the average number of garbage types per house.
Space Complexity: O(1) aside from the input storage.
1function minTimeToCollectGarbage(garbage, travel) {
2 const last = {'M': 0, 'P': 0, 'G': 0};
3 const prefixSum = [0];
4
5 for (let i = 0; i < travel.length; i++) {
6 prefixSum[i + 1] = prefixSum[i] + travel[i];
7 }
8
9 let totalTime = 0;
10
11 for (let i = 0; i < garbage.length; i++) {
12 for (const char of garbage[i]) {
13 totalTime++;
14 if (i > last[char]) {
15 last[char] = i;
16 }
17 }
18 }
19
20 for (const type of ['M', 'P', 'G']) {
21 totalTime += prefixSum[last[type]];
22 }
23
24 return totalTime;
25}
26
27const garbage = ["G", "P", "GP", "GG"];
28const travel = [2, 4, 3];
29console.log(minTimeToCollectGarbage(garbage, travel)); // Output: 21
JavaScript employs object literals for mapping the last occurrence indices and an array for computing prefix sums. The method iterates through the garbage data, incrementing a total count and updating the last known index of each garbage type. Ensuring all necessary travel times are included by using the prefix array, it guarantees a minimum runtime.
Instead of combining the travel times, here we individually compute the travel needed for each garbage type truck. This method essentially iterates through the garbage list and sums up the necessary travel times and garbage pickup times separately for 'M', 'P', and 'G'. Once that's done, the total represents the minimum time required.
Time Complexity: O(n * m) for each separate call, leading to overall O(3 * n * m).
Space Complexity: O(1) aside from input size.
using System.Collections.Generic;
public class SolutionSep {
private int GetTime(List<string> garbage, List<int> travel, char type) {
int last = -1;
int time = 0;
for (int i = 0; i < garbage.Count; i++) {
string house = garbage[i];
foreach (char ch in house) {
if (ch == type) {
time++;
}
}
if (house.Contains(type)) {
if (i > 0 && last != -1) {
time += travel[last];
}
last = i - 1;
}
}
return time;
}
public int MinTimeToCollectGarbage(List<string> garbage, List<int> travel) {
return GetTime(garbage, travel, 'M') +
GetTime(garbage, travel, 'P') +
GetTime(garbage, travel, 'G');
}
public static void Main(string[] args) {
var garbage = new List<string> { "MMM", "PGM", "GP" };
var travel = new List<int> { 3, 10 };
SolutionSep sol = new SolutionSep();
Console.WriteLine(sol.MinTimeToCollectGarbage(garbage, travel)); // Output: 37
}
}
This C# implementation, similar to others, defines a generic method for calculating the total operational time for each garbage truck type. It uses standard iteration and traversal of string lists to determine travel necessity and garbage count, looping over all potential truck types distinctly.