ygreenb
yellowgreenblue
ygreenb
전체 방문자
오늘
어제
  • TIL (130)
    • Algorithm & Data Structure (70)
      • 이론 (4)
      • 프로그래머스 (54)
      • 백준 (12)
    • JAVA (4)
    • Android Studio (9)
    • Database (1)
    • WEB (25)
      • HTML+CSS (7)
      • Javascript (5)
      • React (11)
      • Django (1)
      • Node.js (1)
    • Computer Vision (13)
    • Git (8)

블로그 메뉴

  • HOME
  • TAG
  • GITHUB

공지사항

인기 글

태그

  • 코틀린
  • 백준
  • greedy
  • compareTo()
  • BFS
  • 스택/큐
  • entrySet
  • Arrays.sort()
  • Android
  • reactjs
  • getOrDefault
  • sort
  • 안드로이드
  • git bash
  • git
  • 프로그래머스 Lv.2
  • stack
  • java
  • Queue
  • kotiln
  • 해시
  • HashMap
  • 깃
  • DP
  • React
  • dfs
  • PriorityQueue
  • 깃허브
  • Comparator
  • 프로그래머스

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
ygreenb

yellowgreenblue

Algorithm & Data Structure/프로그래머스

[Java] 프로그래머스 Lv2 : 프린터

2022. 3. 11. 00:36

https://programmers.co.kr/learn/courses/30/lessons/42587?language=java 

 

코딩테스트 연습 - 프린터

일반적인 프린터는 인쇄 요청이 들어온 순서대로 인쇄합니다. 그렇기 때문에 중요한 문서가 나중에 인쇄될 수 있습니다. 이런 문제를 보완하기 위해 중요도가 높은 문서를 먼저 인쇄하는 프린

programmers.co.kr

 

풀이

인쇄물의 우선순위와 위치를 저장한 Print 클래스를 만들어 큐에 넣어준다.

우선순위가 들어있는 배열 priorities을 정렬하고, 현재 큐에 들어있는 인쇄물의 우선순위와 가장 큰 우선순위(priorities[j])와 비교한다.

  1. 현재 인쇄대기 문서의 우선순위가 가장 크다면 인쇄한다. ( j 와 answer 증가)
  2. 이때, 대기문서의 위치가 내가 인쇄를 요청한 문서(location)와 같다면 현재 인쇄순서(answer)를 retrun 해준다.
  3. 현재 인쇄대기 문서의 우선순위보다 더 큰 우선순위를 가진 문서가 있다면 맨 뒤로 보내준다.
import java.util.*;

class Solution {
    static class Print{
        int prior;
        int location;
        
        public Print(int prior, int l){
            this.prior = prior;
            this.location = l;
        }
    }
    public int solution(int[] priorities, int location) {
        int answer = 0; // 인쇄순서
        Queue<Print> q = new LinkedList<>();
        for(int i=0;i<priorities.length;i++){
            q.offer(new Print(priorities[i],i));
        }
    
        Arrays.sort(priorities);
        int j=priorities.length-1;
    
        while(!q.isEmpty()){
            // 현재 우선순위가 가장 크다면 인쇄
            if(q.peek().prior >= priorities[j]) {
                j--; answer++;
                // 인쇄문서의 location이 location이라면 현재 인쇄순서 return
                if(q.peek().location == location) return answer;
                q.poll();
            }
            // 더 큰 우선순위가 있다면 맨 뒤로 보냄
            else{
                q.offer(q.poll());
            }        
        }
        return answer;
    }
}

 

+ 다른사람풀이

location을 줄여나가 굳이 따로 위치저장을 하지 않고 풀어냈다.

내가 요청한 인쇄물의 위치가 될때까지 l--을 해주고 l이 0이된다면 break를 해준다.

import java.util.*;

class Solution {
    public int solution(int[] priorities, int location) {
        int answer = 0;
        int l = location;

        Queue<Integer> que = new LinkedList<Integer>();
        for(int i : priorities){
            que.add(i);
        }

        Arrays.sort(priorities);
        int size = priorities.length-1;



        while(!que.isEmpty()){
            Integer i = que.poll();
            if(i == priorities[size - answer]){
                answer++;
                l--;
                if(l <0)
                    break;
            }else{
                que.add(i);
                l--;
                if(l<0)
                    l=que.size()-1;
            }
        }

        return answer;
    }
}
저작자표시 (새창열림)

'Algorithm & Data Structure > 프로그래머스' 카테고리의 다른 글

[Java] 프로그래머스 Lv.2 : 더 맵게  (0) 2022.03.13
[Java] 프로그래머스 Lv.2 : 주식가격  (0) 2022.03.11
[Java] 프로그래머스 Lv.2 : 위장  (0) 2022.03.09
[Java] 프로그래머스 Lv.2 > 조이스틱  (0) 2022.03.06
[Java] 프로그래머스 Lv2 > 삼각 달팽이  (0) 2022.03.06
    'Algorithm & Data Structure/프로그래머스' 카테고리의 다른 글
    • [Java] 프로그래머스 Lv.2 : 더 맵게
    • [Java] 프로그래머스 Lv.2 : 주식가격
    • [Java] 프로그래머스 Lv.2 : 위장
    • [Java] 프로그래머스 Lv.2 > 조이스틱
    ygreenb
    ygreenb
    개발공부기록장

    티스토리툴바