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.
1using System;
2using System.Collections.Generic;
3
4public class Solution {
5 public int MinTimeToCollectGarbage(IList<string> garbage, IList<int> travel) {
6 int[] last = new int[256];
7 int[] prefixSum = new int[travel.Count + 1];
8
9 for (int i = 1; i <= travel.Count; ++i) {
10 prefixSum[i] = prefixSum[i - 1] + travel[i - 1];
11 }
12
13 int totalTime = 0;
14
15 for (int i = 0; i < garbage.Count; i++) {
16 foreach (char g in garbage[i]) {
17 totalTime++;
18 last[g] = i;
19 }
20 }
21
22 foreach (char g in "MPG") {
23 totalTime += prefixSum[last[g]];
24 }
25
26 return totalTime;
27 }
28
29 public static void Main(string[] args) {
30 var garbage = new List<string> { "G", "P", "GP", "GG" };
31 var travel = new List<int> { 2, 4, 3 };
32 Solution sol = new Solution();
33 Console.WriteLine(sol.MinTimeToCollectGarbage(garbage, travel)); // Output: 21
34 }
35}
C# uses arrays to track the prefix sums and last indices. It processes each string in the garbage list and updates the last occurrence index for each type as it counts the total garbage units. After processing, it sums the travel times required by checking the last indices for each type.
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.