3

I'm trying to solve the following problem: You are given N items. Each item contains three tasks A, B and C. The time required to complete task A is TA, task B is TB, task C is TC. Now, we must select M items such that completing these M item's tasks take the minimum time. And here are the rules:

  1. All selected M items are operated simultaneously, i.e. tasks of all the M items are operated at the same moment
  2. The task B of any selected items cannot be started unless task A of all M items is complete
  3. The task C of any selected items cannot be started unless task B of all M items is complete

For example:

if say N = 3 and M = 2 (it means we must select M items out of 3 items in total)
         Tasks: A  B  C
       item 1 : 1  2  2
       item 2 : 3  4  1
       item 3 : 3  1  2

If we select item 1 and item 3, task A of both items gets completed after 3 units(item 1 waits for item 3 to finish), then task B of both the items gets completed after the next 2 units time. Similarly, task C gets completed after 2 units time. Hence the total time is 7(which is the minimum possible combination we can find)

I have tried thinking of a Dynammic programming solution to the problem. But am unable to get a concrete solution to the problem. Could anyone help me out by giving a valid solution to the problem.

PS: Please don't write the code. I am just looking for the logic here.

Thank you in advance.

nishant_boro
  • 374
  • 1
  • 2
  • 8
  • Constraints: 1 <= N <= 2000, 1 <= M <= N, 1 <= items[i] < = 10^9 – nishant_boro Aug 30 '20 at 06:01
  • https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions - posting what you've tried so far would help, even if you're not asking for code – Gryphon Aug 30 '20 at 06:26
  • Could you please help me understand the new subtraction method in the accepted answer? How would it work with input `[{2,2,2}, {2,2,2}, {3,3,3}, {3,3,3}], M = 2`? The way I understand it, `Item1-Item2 = 0`, but also `Item3-Item4 = 0` so how would we choose the better one, which is `(Item1, Item2)`? – גלעד ברקן Sep 06 '20 at 22:14

2 Answers2

3

Solution via Greedy Method (Weight Calculation + Deadline Sequencing)

Here is a greedy approach for the solution of this problem, I hope it helps. Good luck!

Since each task within an item takes time T to complete, we can think of these as "Deadlines" for these tasks (A, B and C) . And we can visualise these deadlines as if they were "slots" within an array/train of slots.

In order to visualize these deadlines consider these examples;

Task A of item 2;

0__A__1__A__2__A__3

Task C of item 1;

0__C__1__C__2

Let's consider this now; We have an K amount of "slots" within our hand 0__1__2__ ... __K and the problem asks us to spend minimum amount of slots as possible.

Another example from your explanation for better visualization of the problem, when you chose the item1 and item3 our slot took this form;

item1 + item3 "deadline slot occupation"

0_A_1_A_2_A_3_B_4_B_5_C_6_C_7

First three slots are occupied because item3's task A takes 3 units longer than item1. Task B can only start when this "longer" task A is done therefore starts from the slot number 3.

Therefore the problem becomes this; Fill our slot with the MINIMUM amount of slots spent. Therefore we will take a greedy approach into this problem.

  • Find individual "deadline slots" for M number of items we want to choose from N items

In the example you have provided;

For item1;

0_A_1_B_2_B_3_C_4_C_5

5 slots occupied

For item2; 8 slots occupied

For item3; 6 slots occupied

For itemX; P slots occupied and so on....

After knowing the number of slots each item demands on task times we will check M number of Subtractions as combinations of items within N number of item task times to get the SMALLEST number possible.

Example; For M items to choose when M=2;

Item1-Item2 = 5;

Item1-Item3 = 3;

Item2-Item3 = 4;

**edit; Item1 - Item2 corresponds to absolute value of subtractions within the combinations of chosen number of items; such as if M=2; |a1-a2| + |b1-b2| + |c1-c2| ...

Therefore for M=2 choices we take the minimum result of 3 which leads us to choosing Item1 and Item3 as solution.

This number will give us the minimum number of slots used. Hence, this leads us to the solution.

MrBulut
  • 131
  • 6
  • 1
    I don't understand your algorithm. Could you please explain how it would work with input `[{2,2,2}, {2,2,2}, {3,1,1}, {1,3,1}], M = 2`? The way I understand how your sums work, we would see items 3 and 4 get 5 + 5 = 10, and choose those over items 1 and 2, which get 6 + 6 = 12. But the solution, as I understand, should be items 1 and 2 that yield 6 overall, where items 3 and 4 would yield 7 overall. – גלעד ברקן Aug 30 '20 at 23:32
  • Yes you are right, Instead of "Addition" I needed to take a different approach on greedy method since the example requires us to spend "minimum" space. Thank you so, so much for finding the mistake and it really helped me to fully grasp the answer. I edited the answer in the right direction thanks to you. This both provides the solution for your example and our friend's question. – MrBulut Aug 31 '20 at 01:27
  • 1
    Thanks!. This was asked in Google - SWE Intern Online Assessment btw. – nishant_boro Aug 31 '20 at 11:13
  • Thank you for explaining. But I still don't understand the new subtraction method. Could you please explain how it would work with input `[{2,2,2}, {2,2,2}, {3,3,3}, {3,3,3}], M = 2`? The way I understand it, `Item1-Item2 = 0`, but also `Item3-Item4 = 0` so how would we choose the better one, which is `(Item1, Item2)`? – גלעד ברקן Sep 06 '20 at 22:14
0

Solution using priority queue or sorting

let's say we choose some M elements and their maximum values of A, B, C are Amax, Bmax and Cmax respectively, then we are sure that there must be some element in array with A = Amax similarly for B and C, so we can say that there are at maximum N different values for A, B, C. Hence possible combination for (Amax, Bmax, Cmax) are at most N^3. Then we can check if there are at least M elements in the array satisfying this constraint for each combination and update the answer value using ans = max(ans, Amax + Bmax + Cmax). This approach takes O(N^4) time.

But we can reduce the time complexity to O(N^3logN) using sorting or priority queue for Cmax level. Basically, find all elements satisfying Amax and Bmax constraint and store C values for all such elements then find Mth smallest value in array for Cmax.

arnold
  • 1