작성
·
230
0
만약 강의에서처럼 규칙을 찾지 못해서
큐로 구현했는데, 이렇게 구현하면 안되는건가요???
import java.util.*;
class Node{
int number; //해당 작업의 작업시간
int idx; //작업 번호
Node(int number, int idx){
this.number = number;
this.idx = idx;
}
}
class Main {
public static Queue<Node> q= new LinkedList<>();
public int solution(int[] tasks, long k) {
int answer = 0;
for(int i=0; i<tasks.length; i++) {
q.offer(new Node(tasks[i],i+1)); //1 2 3, 각 작업 번호들 대입
}
long time=0;
while(!q.isEmpty()) {
Node tmp = q.poll();
int nownumber = tmp.number;
int nowwork = tmp.idx;
nownumber--;
time++;
if(time==k) {
answer = nowwork;
break;
}
if(nownumber!=0) {
q.offer(new Node(nownumber, nowwork));
}
}
return answer;
}
public static void main(String[] args){
Main T = new Main();
System.out.println(T.solution(new int[]{1, 2, 3}, 5));
System.out.println(T.solution(new int[]{8, 5, 2, 9, 10, 7}, 30));
System.out.println(T.solution(new int[]{8, 9, 12, 23, 45, 16, 25, 50}, 100));
}
}
답변 2