华为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;
}
}
全部评论
相关推荐
点赞 评论 收藏
分享
点赞 评论 收藏
分享
02-07 10:52
复旦大学 Java
混子不想混:非常能理解,感觉他们就靠着入行早,打压新人一样。我这个公司也是,天天干的累死累活,然后绩效打C,合着让新人被绩效,像是年底攒棺材本一样。总是打击之后,还会让人开始自我怀疑,是不是我努力的还不够,实际上并不是,就是他们不做人,故意打压新人。 点赞 评论 收藏
分享
点赞 评论 收藏
分享
字节跳动工作强度 1104人发布