New World

[Zero-base #02] 큐 본문

Self-Study/Study

[Zero-base #02] 큐

hyeovi 2023. 3. 15. 00:07
728x90
반응형

- 선입선출(FIFO) 자료구조 : 먼저 들어온 데이터가 먼저 나가는 구조

- 입력 순서대로 데이터 처리가 필요할 때 사용 ex. 프린터 출력 대기열, BFS 등

 

큐 기본 연산

- 데이터 추가 (Enqueue)

- 데이터 꺼내기 (Dequeue)

 

값 추가 값 확인 값 제거
큐의 맨 뒤에 값 삽입 큐의 맨 앞에 있는 값 반환 remove()
큐 맨 앞에 있는 값 반환 후 삭제
큐가 비어 있는 경우 NoSuchElementException 에러

add()
꽉 찬 경우 IllegalStateException 에러
element()
빈 경우 NoSuchElementException 에러
poll()
큐 맨 앞에 있는 값 반환 후 삭제
큐가 비어있을 경우 null 반환
offer()
꽉 찬 경우 false 반환
peek()
빈 경우 false 반환
clear()
큐 비우기

 

 

기능개발(https://school.programmers.co.kr/learn/courses/30/lessons/42586)

import java.util.*;

class Solution {
    public int[] solution(int[] progresses, int[] speeds) {
        Queue<Integer> pspeeds = new LinkedList<Integer>();
        
        //수행하는데 걸리는 스피드
        for(int i = 0; i < progresses.length; i++){
            pspeeds.add((int) Math.ceil((100.0 - progresses[i]) / speeds[i]));
        }
        
        
        //수행하는데 걸리는 날짜
        ArrayList<Integer> days = new ArrayList<Integer>();
        
        while(!pspeeds.isEmpty()){
            int n = pspeeds.poll();
            int day = 1;
            
            while(!pspeeds.isEmpty() && n >= pspeeds.peek()){
                day++;
                pspeeds.poll();
            }
            days.add(day);
        }
        
        return days.stream().mapToInt(Integer::intValue).toArray();
    }
}
반응형
Comments