プログラミングアルゴリズム問題スタックとキューを解く
機能開発
JAVA Queue声明ソリューション
1.モジュールを使用して指定された値を検索
2.資料構造はQueueを用いて一つずつ抽出し、以下の数字と比較する
解決策一番前のドキュメントを抽出(J)-> Queue確認に入ると、値は2つのパラメータなので、クラスを作成して分離しましょう 最初に失敗しました->キューは、次の値を検証するときにwhile(!isEmpty()として検証されるため、一度だけ検証するfor Eachに変更され、問題解決 が発生しました.
JAVA Queue声明
Queue<Integer>queue=new LinkedList<>();
1.モジュールを使用して指定された値を検索
2.資料構造はQueueを用いて一つずつ抽出し、以下の数字と比較する
public int[] solution(int[] progresses, int[] speeds) {
// 큐 생성
Queue<Integer> queue=new LinkedList<>();
for (int i=0;i<progresses.length;i++) {
int val=100-progresses[i];
int mod=val%speeds[i];
int res=val/speeds[i];
// System.out.println(mod);
// System.out.println(res);
if (mod>0) {
progresses[i]=res+1;
}
else {
progresses[i]=res;
}
queue.add(progresses[i]);
}
// System.out.println(Arrays.toString(progresses));
// System.out.println(queue.toString());
ArrayList<Integer>arrayList=new ArrayList<>();
while (!queue.isEmpty()) {
int res=queue.poll();
int time=1;
while (!queue.isEmpty()) {
int next=queue.peek();
if (res>=next) {
queue.poll();
time++;
}
else {
break;
}
}
arrayList.add(time);
}
// System.out.println(arrayList.toString());
int[] answer =new int[arrayList.size()];
for (int i=0;i<arrayList.size();i++) {
answer[i]=arrayList.get(i);
}
return answer;
}
プリンタ解決策
Queue<Printer> queue=new LinkedList<>();
static class Printer{
int idx;
int prioirty;
Printer(int idx, int priority) {
this.idx;
this.priority;
}
}
static class Printer{
int idx;
int priority;
Printer(int idx, int priority) {
this.idx=idx;
this.priority=priority;
}
}
public int solution(int[] priorities, int location) {
int answer = 0;
Queue<Printer> queue=new LinkedList<>();
for (int i=0;i<priorities.length;i++) {
Printer printer=new Printer(i,priorities[i]);
queue.add(printer);
}
// System.out.println(queue.toString());
int time=1;
while (!queue.isEmpty()) {
Printer printer=queue.poll();
int idx=printer.idx;
int first_priority=printer.priority;
boolean check=true;
// 1번 문제 -> Queue가 무한히 돌면 안됨
// while (!queue.isEmpty()) {
// Printer next_printer=queue.peek();
// int next_priority=next_printer.priority;
// int next_idx=next_printer.idx;
// // 큰게 존재
// if (first_priority<next_priority) {
// queue.add(new Printer(idx,first_priority));
// check=false;
// break;
// }
// // else {
// // if (idx==location) {
// // return time;
// // }
// // time++;
// // }
// }
// -> for Each로 수정 (1번만 돌게 만든다)
for (Printer nextPrinter:queue) {
int next_priority=nextPrinter.priority;
int next_idx=nextPrinter.idx;
if (first_priority<next_priority) {
queue.add(new Printer(idx,first_priority));
check=false;
break;
}
}
// 다 돌았는데 큰게 없으면 뽑느다.
if (check) {
if (idx==location) {
return time;
}
time++;
}
}
return answer;
}
ブリッジトラックReference
この問題について(プログラミングアルゴリズム問題スタックとキューを解く), 我々は、より多くの情報をここで見つけました https://velog.io/@sds1vrk/프로그래머스-알고리즘-문제-풀이-스택-큐テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol