本帖最后由 匿名 于 2024-4-9 11:45 编辑
重要的事情说三遍:
大家分享面经 尽量给出靠谱的答案! 不要贴Leetcode的链接,亚麻会删掉相关的LC链接!
第一题:
https://www.1point3acres.com/bbs/thread-857874-1-1.html
Given an array of integers (unsorted) and a value n indicating number of channels. You have to send all these numbers through n channels (At least one number should go through each channel)
第二题:
Maxium waiting time of each request:
给你一个 job array比如
[3, 1 , 2, 1] 第0秒一共有 4个 job, 第一秒处理 arr[0] 任务, 然后expire时间小于等于1秒的任务就expire掉 那么就剩下
[2] 1个任务 (3处理掉了, 1, 和 最后的1 expire) 然后第三秒 处理掉【2】 还剩0个任务
整理一下输出就是 4 (起始job) 1 (第一秒) 0 (最后) ==》 [4, 1, 0]
一个思路是bfs但是会超时,所以要o(n) 的方法,
补充内容 (2024-06-04 02:07 +08:00):// 重写了第二题答案, 可以pass all cases 速度贼快
public static List<Integer> findRequestsInQueue(List<Integer> wait) { int requests = wait.size(); // # of pending requests Map<Integer, Integer> counts = new HashMap<>(); counts.compute(time, (k, v) -> v == null ? 1 : v + 1); [/quote][quote] int n = wait.size(); List<Integer> result = new ArrayList<>(); int time = 0; // real / running time for (int waitingTime : wait) { if (waitingTime > time) { counts.compute(waitingTime, (k, v) -> v == 1 ? null : v - 1); Integer noOfReqExpired = counts.remove(time); if (noOfReqExpired != null) { requests -= noOfReqExpired; [/quote][quote] // Add remaining requests if time hasn't reached all waiting times [/quote][quote] return result; |