活动选择问题中缺少测试用例

时间:2019-02-19 21:25:28

标签: java algorithm greedy

我正在从一个网站上解决活动选择问题,在该网站上我必须找到可以完成的最大活动。首先,我读取测试用例的数量(t),然后针对每个测试用例的活动数量(n)。输入采用以下格式:

2
4
Maths 16:00 18:00
ComputerScience 12:00 13:00
Physics 12:30 14:00
Chemistry 14:00 16:30
5
Geography 14:00 16:00
History 12:00 14:30
Arts 14:00 16:30
Literature 12:30 13:30
German 13:30 15:00

我写了下面给出的代码。它通过了除一个以外的所有测试用例。我想念什么?

import java.time.LocalTime;
import java.util.*;

class solution {

    static class Subject {

        String name;
        LocalTime start;
        LocalTime end;

        public Subject(String name, String startString, String endString) {
            this.name = name;
            this.start = LocalTime.parse(startString);
            this.end = LocalTime.parse(endString);
        }
    }

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt(), i;
        while (t > 0) {
            int count = 1,n;
            LocalTime end;
            n = sc.nextInt();
            sc.nextLine();
            if(n>0)
            {
            List<Subject> list = new ArrayList<Subject>();
            for (i = 0; i < n; i++) {
                String[] input = sc.nextLine().split(" ");
                Subject subject = new Subject(input[0].trim(), input[1].trim(), input[2].trim());
                list.add(subject);
            }
            Collections.sort(list, (s1, s2) -> s1.end.compareTo(s2.end));
            end = list.get(0).end;
            i = 1;
            while (i < n) {
                if(!list.get(i).start.isBefore(end)) {
                    count++;
                    end = list.get(i).end;
                }
                i++;
            }
            System.out.println(count);
        }
        else
        {System.out.println("0");}
            t--;
        }

    }
}

1 个答案:

答案 0 :(得分:0)

是否有关于活动在另一活动结束时准确开始的规定?

相关问题