华为OD机试真题 - 执行时长
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int GPU = scanner.nextInt();
int runableCount = scanner.nextInt();
PriorityQueue priorityQueue = new PriorityQueue<Integer>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
for (int i = 0; i < runableCount; i++) {
priorityQueue.add(scanner.nextInt());
}
System.out.println(getMinTime(GPU, priorityQueue));
}
public static int getMinTime(int GPUCount, PriorityQueue<Integer> runables) {
GPUPoll poll = new GPUPoll(GPUCount);
int i = 0;
while (!runables.isEmpty()) {
while (poll.hasBanancePoll() != -1) {
if (runables.peek() != null)
poll.poll[poll.hasBanancePoll()] = runables.poll();
else break;
}
poll.execut();
i++;
}
i += poll.getMaxTime();
return i;
}
static class GPUPoll {
int[] poll;
public GPUPoll(int count) {
this.poll = new int[count];
}
public void execut() {
for (int i = 0; i < poll.length; i++) {
poll[i] -= 1;
}
}
public int hasBanancePoll() {
for (int i = 0; i < poll.length; i++) {
if (poll[i] == 0)
return i;
}
return -1;
}
public int getMaxTime() {
int max = Integer.MIN_VALUE;
for (int i = 0; i < poll.length; i++) {
if (poll[i] > 0) {
max = Math.max(max,poll[i]);
}
}
return max;
}
}
Scanner scanner = new Scanner(System.in);
int GPU = scanner.nextInt();
int runableCount = scanner.nextInt();
PriorityQueue priorityQueue = new PriorityQueue<Integer>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
for (int i = 0; i < runableCount; i++) {
priorityQueue.add(scanner.nextInt());
}
System.out.println(getMinTime(GPU, priorityQueue));
}
public static int getMinTime(int GPUCount, PriorityQueue<Integer> runables) {
GPUPoll poll = new GPUPoll(GPUCount);
int i = 0;
while (!runables.isEmpty()) {
while (poll.hasBanancePoll() != -1) {
if (runables.peek() != null)
poll.poll[poll.hasBanancePoll()] = runables.poll();
else break;
}
poll.execut();
i++;
}
i += poll.getMaxTime();
return i;
}
static class GPUPoll {
int[] poll;
public GPUPoll(int count) {
this.poll = new int[count];
}
public void execut() {
for (int i = 0; i < poll.length; i++) {
poll[i] -= 1;
}
}
public int hasBanancePoll() {
for (int i = 0; i < poll.length; i++) {
if (poll[i] == 0)
return i;
}
return -1;
}
public int getMaxTime() {
int max = Integer.MIN_VALUE;
for (int i = 0; i < poll.length; i++) {
if (poll[i] > 0) {
max = Math.max(max,poll[i]);
}
}
return max;
}
}
全部评论
相关推荐
葛明珠:被动打杂真的是实习的坑,主动找问题 + 带方案沟通,才是实习的正确打开方式
点赞 评论 收藏
分享
点赞 评论 收藏
分享
2025-11-13 14:37
门头沟学院 Java 程序员牛肉:是的,我觉得你最先需要的是多接触计算机圈子。我感觉你这个写的太幼稚了,根本没换位思考面试官。
你对实习的描述还是我写了前后端,我写了Restful接口,我用了EChatrs。你这让面试官怎么问你?问你什么是前后端?问你什么是Restful?讲真的兄弟,你这个简历在面试官眼里就是啥也不懂的好学生。所以一定要尽快加入一个圈子跟大家多聊聊,看看正儿八经的简历是怎么写的。
可以看一下我首页的简历怎么写那篇文章来学一下,你这里面的坑点我那篇文章里面都有讲过。
点赞 评论 收藏
分享
牛客49896997...:电话都漏出来,要保护个人隐私,我先来,你说我见过最美的牛客女孩 点赞 评论 收藏
分享
点赞 评论 收藏
分享