https://programmers.co.kr/learn/courses/30/lessons/42586
import java.util.*;
class Solution {
public int[] solution(int[] progresses, int[] speeds) {
int[] answer = {};
List<Integer> list = new LinkedList<>();
Queue<Integer> q = new LinkedList<>();
for(int i=0; i<progresses.length; i++){
int day=0;
if((100-progresses[i])%speeds[i]==0) day=(100-progresses[i])/speeds[i];
else day=(100-progresses[i])/speeds[i]+1;
q.add(day);
}
while(!q.isEmpty()){
int cur = q.poll();
int cnt=1;
while(!q.isEmpty()){
if(q.peek()<=cur){
q.remove();
cnt++;
}else break;
}
list.add(cnt);
}
answer=new int[list.size()];
for(int i=0; i<list.size();i++){
answer[i]=list.get(i);
}
return answer;
}
}
https://programmers.co.kr/learn/courses/30/lessons/42587
import java.util.*;
class Solution {
class Node{
int priority;
int num;
Node(int priority, int num){
this.priority=priority;
this.num = num;
}
}
public int solution(int[] priorities, int location) {
int answer = 0;
Queue<Node> q = new LinkedList<>();
int size = priorities.length;
for(int i=0; i<size; i++){
q.add(new Node(priorities[i],i));
}
Arrays.sort(priorities);
while(!q.isEmpty()){
Node cur = q.poll();
if(cur.priority==priorities[size-1]){
answer++;
size--;
if(cur.num==location) break;
}else q.add(cur);
}
return answer;
}
}
https://programmers.co.kr/learn/courses/30/lessons/42583
import java.util.*;
class Solution {
class Node{
int endTime;
int weight;
Node(int endTime, int weight){
this.endTime=endTime;
this.weight = weight;
}
}
public int solution(int bridge_length, int weight, int[] truck_weights) {
int answer = 0;
Queue<Node> q = new LinkedList<>();
int current=0;
int i=0;
while(!q.isEmpty() || i!=truck_weights.length){
answer++;
while(!q.isEmpty()){
if(q.peek().endTime==answer){
current-=q.peek().weight;
q.remove();
}
else break;
}
if(i<truck_weights.length && current+truck_weights[i]<=weight){
current+=truck_weights[i];
q.add(new Node(answer+bridge_length,truck_weights[i]));
i++;
}
}
return answer;
}
}
https://programmers.co.kr/learn/courses/30/lessons/42584
import java.util.*;
class Solution {
public int[] solution(int[] prices) {
int[] answer = new int[prices.length];
Stack<Integer> st = new Stack<>();
for(int i=0; i<prices.length; i++){
while(!st.isEmpty() && prices[st.peek()]>prices[i]){
int tmp = st.pop();
answer[tmp]=i-tmp;
}
st.push(i);
}
while(!st.isEmpty()){
int tmp = st.pop();
answer[tmp]=prices.length-1-tmp;
}
return answer;
}
}
'Algorithm > 자료구조' 카테고리의 다른 글
[프로그래머스] 코딩테스트 고득점 Kit - 힙(HEAP) (JAVA) (0) | 2021.10.02 |
---|---|
[프로그래머스] 코딩테스트 고득점 Kit - 해시 (JAVA) (0) | 2021.09.30 |
[백준] 2470번 : 두 용액 (C++) (0) | 2021.08.30 |
[백준] 5430번 : AC (C++) (0) | 2021.08.23 |
댓글