Knowee
Questions
Features
Study Tools

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: 21Explanation:The paper garbage truck:1. Travels from house 0 to house 12. Collects the paper garbage at house 13. Travels from house 1 to house 24. Collects the paper garbage at house 2Altogether, it takes 8 minutes to pick up all the paper garbage.The glass garbage truck:1. Collects the glass garbage at house 02. Travels from house 0 to house 13. Travels from house 1 to house 24. Collects the glass garbage at house 25. Travels from house 2 to house 36. Collects the glass garbage at house 3Altogether, 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.

Question

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: 21Explanation:The paper garbage truck:1. Travels from house 0 to house 12. Collects the paper garbage at house 13. Travels from house 1 to house 24. Collects the paper garbage at house 2Altogether, it takes 8 minutes to pick up all the paper garbage.The glass garbage truck:1. Collects the glass garbage at house 02. Travels from house 0 to house 13. Travels from house 1 to house 24. Collects the glass garbage at house 25. Travels from house 2 to house 36. Collects the glass garbage at house 3Altogether, 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.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

To solve this problem, we can use a dynamic programming approach. We will create a 3D DP table where dp[i][j][k] represents the minimum time to pick up all the garbage from house 0 to house i with j units of metal garbage and k units of paper garbage left to pick up.

Here are the steps to solve the problem:

  1. Initialize the DP table with a large value, say 1e9, because we want to find the minimum time.

  2. Set dp[0][0][0] = 0 because it takes 0 time if there is no garbage to pick up.

  3. Iterate over each house from 0 to n-1. For each house, iterate over each possible amount of metal and paper garbage that could be left after visiting this house. This is from 0 to the total amount of metal and paper garbage.

  4. For each possible state, try all three possibilities: picking up metal, paper, or glass garbage. Update the DP table accordingly.

  5. After visiting all houses, the minimum time to pick up all the garbage is the minimum value in dp[n-1][0][0], dp[n-1][1][0], and dp[n-1][0][1].

  6. Return this minimum value.

In the given example, the DP table after visiting all houses would look like this:

dp[0][0][0] = 0 dp[1][0][0] = 2 dp[2][0][0] = 6 dp[3][0][0] = 9

So, the minimum time to pick up all the garbage is 9 minutes.

This problem has been solved

Similar Questions

2391. Minimum Amount of Time to Collect GarbageMedium78786CompaniesYou 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.

You are given a string s. Simulate events at each second i:If s[i] == 'E', a person enters the waiting room and takes one of the chairs in it.If s[i] == 'L', a person leaves the waiting room, freeing up a chair.Return the minimum number of chairs needed so that a chair is available for every person who enters the waiting room given that it is initially empty

Given the array houses where houses[i] is the location of the ith house along a street and an integer k, allocate k mailboxes in the street.Return the minimum total distance between each house and its nearest mailbox.The test cases are generated so that the answer fits in a 32-bit integer.

public class Main { public static void main(String args[]) { int arr[] = {1, 2, 3}; int m = arr.length; int n = 10; System.out.println( countWays(arr, m, n)); } static long countWays(int S[], int m, int n) { long[] table = new long[n+1]; table[0] = 1; for (int i=0; i<m; i++) for (int j=S[i]; j<=n; j++) table[j] += table[j-S[i]]; return table[n]; } } i need 14 as output

In this challenge, the user enters a string and a substring. You have to print the number of times that the substring occurs in the given string. String traversal will take place from left to right, not from right to left.

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.