How to find max number of participant who can perform?

Panda :

I am trying to solve a question which has the following description:

At a time only one person can perform on the stage and arrival time and duration of each participant is given in the form of an array. Find the max number of participants who can perform.

Example:

arrival time: [1,3,3,5,7]

duration: [2,2,1,2,1]

answer would be 4, as 1-3; 3-5 or 3-4; 5-7; 7-8

So from this, I can find the exit time of each participant. How do I find max number of events possible, when there is overlap in timings.

The code I have tried is:

int count = 1;

for(int i=0;i<entry.size()-1;i++) {
    if(entry.get(i) < exit.get(i+1)) {
        count++;
    }
}

return count;

I found the exit list using arrival + duration, but many tests are failing. The above example passes, but there might be instances where the overlapping time may have more participants possible.

I am not able to figure out how to proceed.

Conffusion :

Updated answer as a new test case was added which made it clear the performers can be reordered if they have the same arrival time. 2nd update: Sort the input on the end time of each performer (arrival+duration). Keep the endTime of the current performer. Only performers can perform which arrive after the end of the current performer.

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class LifeGig {

    public static class Performer {
        int arrival, duration;

        Performer(int arrival, int duration) {
            this.arrival = arrival;
            this.duration = duration;
        }

        @Override
        public String toString() {
            return "Performer [arrival=" + arrival + ", duration=" + duration + "]";
        }
    }

    public List<Performer> program(int[] entry,int[] duration)
    {
        List<Performer> performers=new ArrayList<>();
        for(int i=0;i<entry.length;i++)
        {
            performers.add(new Performer(entry[i],duration[i]));
        }
        Collections.sort(performers, new Comparator<Performer>() {
            @Override
            public int compare(Performer p1, Performer p2) {
                return Integer.compare(p1.arrival+p1.duration, p2.arrival+p2.duration);
            }
        });
        List<Performer> festival=new ArrayList<>();
        System.out.println(performers);
        int currentTime = 1;
        for (Performer p:performers) {
            if (p.arrival >= currentTime) {
                currentTime = p.arrival+p.duration;
                festival.add(p);
            }
        }
        return festival;
    }
    public static void test1()
    {
        int[] entry = new int[] {1,3,3,5,7};
        int[] duration = new int[] {2,2,1,2,1};
        List<Performer> festival=new LifeGig().program(entry,duration);
        System.out.println("Count (Expected=4): " + festival.size());
        System.out.println("lineup:"+festival);
    }

    public static void test2()
    {
        int[] entry = new int[] {1, 1, 1, 1, 4};
        int[] duration = new int[] {10, 3, 6, 4, 2};
        List<Performer> festival=new LifeGig().program(entry,duration);
        System.out.println("Count (Expected=2): " + festival.size());
        System.out.println("lineup:"+festival);
    }
    public static void test3()
    {
        int[] entry = new int[] {1,2,3,4};
        int[] duration = new int[] {10, 1,1,1,};
        List<Performer> festival=new LifeGig().program(entry,duration);
        System.out.println("Count (Expected=3): " + festival.size());
        System.out.println("lineup:"+festival);

    }

    public static void main(String[] args) {
        test1();
        test2();
        test3();
    }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=90863&siteId=1