华为4.21笔试
求问第一题——我只能通过27.8%
我的思路就是先排序,然后一个一个的进去累加某一个超限了就拒绝掉
请各位看到的大佬解释一下:我为什么错了
package HuaWei; import java.util.Comparator; import java.util.PriorityQueue; import java.util.Scanner; public class Reserve { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt();// 预定电话数 int m = sc.nextInt();// 总共能够容纳的人数 PriorityQueuequeue = new PriorityQueue(new Comparator() { @Override public int compare(Order o1, Order o2) { return o1.startTime - o2.startTime; } }); for(int i = 0; i < n; i++){ int startTime = sc.nextInt(); int endTime = sc.nextInt(); int personNum = sc.nextInt(); Order order = new Order(startTime, endTime, personNum); queue.add(order); } int[] nums = new int[24]; while(!queue.isEmpty()){ Order order = queue.poll(); reserve(nums, order, m); } for(int i = 0; i < nums.length; i++){ if(i == nums.length - 1){ System.out.print(nums[i]); } else { System.out.print(nums[i] + " "); } } } private static void reserve(int[] nums, Order orderA, int limit){ int start = orderA.startTime; int end = orderA.endTime; for(int i = start; i < end; i++){ if(orderA.personNum + nums[i] > limit){ return; } } // 没有return的话表明可以接受该订单 for(int i = start; i < end; i++){ nums[i] += orderA.personNum; } } } class Order{ public int startTime; public int endTime; public int personNum; public Order(int startTime, int endTime, int personNum) { this.startTime = startTime; this.endTime = endTime; this.personNum = personNum; } } /* 4 40 9 12 5 8 11 10 14 16 30 15 17 20 */#华为##笔经#