어떻게 수행 할 수있는 참가자의 최대 수를 찾는 방법은?

팬더 :

나는 다음과 같은 설명이 질문을 해결하기 위해 노력하고 있습니다 :

한 번에 한 사람 만이 스테이지의 도착 시간에 수행 할 수 있고, 각 참가자의 시간은 어레이의 형태로 주어진다. 수행 할 수있는 참가자의 최대 수를 찾을 수 있습니다.

예:

도달 시간 [1,3,3,5,7]

시간 : [2,2,1,2,1]

대답은 1-3로 4 것; 3-5 또는 3-4; 5-7; 7-8

이에서 그래서, 나는 각 참가자의 종료 시간을 찾을 수 있습니다. 타이밍에이 겹치는 경우 어떻게 가능한 이벤트의 최대 수를 찾을 수 없습니다.

내가 시도하는 코드는 다음과 같습니다

int count = 1;

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

return count;

내가 도착 + 기간을 사용하여 출구 목록을 찾았지만, 많은 테스트가 실패하고 있습니다. 위의 예는 통과하지만, 중복 시간 이상의 참가자가 가능 할 수 있습니다 경우가있을 수 있습니다.

내가 진행하는 방법을 알아낼 수 없습니다입니다.

Conffusion :

새로운 테스트 케이스로 업데이트 대답은 그들이 같은 도착 시간이있는 경우 공연 순서가 변경 될 수있다 명확하게하는 추가되었습니다. 둘째 업데이트 각 출연자 (+ 도착 시간)의 종료 시각에 정렬 입력. 현재 연기자의 endTime- 사용자를 유지합니다. 만 공연은 현재 연기자의 끝 이후에 도착하는 수행 할 수 있습니다.

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();
    }

}

추천

출처http://43.154.161.224:23101/article/api/json?id=202644&siteId=1