sggnology
하늘속에서IT
sggnology
전체 방문자
오늘
어제
  • 분류 전체보기 (83)
    • Algorithm (31)
      • Programmers (27)
      • Baekjoon (4)
    • WIKI (4)
      • VirtualBox (1)
      • Power Toys (1)
    • NodeJS (4)
      • nvm (1)
      • React (1)
      • Vue (1)
    • Dev Language (3)
      • Java (2)
      • Kotlin (1)
    • Spring Boot (17)
      • Gradle (1)
      • JPA (3)
    • DB (4)
      • MariaDB (3)
      • Redis (0)
    • Android (6)
      • Debug (3)
    • Nginx (3)
      • Debug (1)
    • Intellij (0)
    • Network (1)
    • Git (2)
      • GitHub (2)
    • Chrome Extension (0)
    • ETC (5)
      • Monitoring (2)
    • Linux (1)
      • WSL (1)
    • Visual Studio (1)
    • Side Project (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • java
  • 레벨3
  • 레벨2
  • JPA
  • Android Studio
  • 고득점KIT
  • 연습문제
  • nginx
  • 오블완
  • 프로그래머스
  • 백준
  • DB
  • 티스토리챌린지
  • 안드로이드 스튜디오
  • 알고리즘
  • mariadb
  • spring boot
  • docker
  • 고득점 Kit
  • kotlin

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
sggnology

하늘속에서IT

Algorithm/Programmers

Programmers 스택/큐::레벨2::프린터

2022. 4. 8. 14:30
728x90

풀이

import java.util.*

class Solution {
    fun solution(priorities: IntArray, location: Int): Int {
        val operationQ: Queue<PrintTarget> = LinkedList<PrintTarget>()
        val standByQ: Queue<PrintTarget> = LinkedList<PrintTarget>(priorities.mapIndexed { index, priorityValue -> PrintTarget(index, priorityValue) })

				/**
					설명
					- operationQ(실제 실행 대기열) 와 standByQ(실행 하기전 대기열) 두개의 큐를 선언한다.
					- 문제의 조건에 맞추어 standByQ 에서 operationQ 로 값을 이동시킨다.
				*/
        while(standByQ.isNotEmpty()){
            if(standByQ.isHigherThan()){
                standByQ.offer(standByQ.poll())
            }
            else{
                operationQ.offer(standByQ.poll())
            }
        }
        
        return operationQ.getIndexByLocation(location)
    }
}

/**
	설명
	- 프린트 대상을 클래스화 함
	
	특징
	- 해당 문제의 답으로 몇번째 위치에 있던 값을 궁금해함으로, `location` 변수를 추가
*/
data class PrintTarget(
    val location: Int,
    val priority: Int,
)

/**
	설명
	- 큐에서 priority 크기를 상대적 비교하는 코드이다.
	- 해당 큐에서 head 의 priority 의 크기가 큰지 작은지에 대한 평가

	특징
	- 예를들어 2,1,3,2,3 이라는 우선순위를 가지는 array 가 있다고 가정할때
	=> 해당 array 에서 제일 큰 우선순위는 3이다. 3이라는 우선순위를 가지는 경우가 2가지가 있다.
	=> 그렇다면 해당 경우에서 2번째 index의 값이 먼저 출력되어야 하는 대상이다.
	- Queue의 값을 순회하며 더 높은 priority 가 있다면 true, 그렇지 않다면 false 를 반환한다.
*/
fun Queue<PrintTarget>.isHigherThan(): Boolean{

    val thisPriority = this.peek().priority

    this.forEach { printTargetEle ->
        if(thisPriority < printTargetEle.priority){
            return true
        }
    }

    return false
}

/**
	설명
	- 문제의 정답을 구하기 위한 확장함수이다.
*/
fun Queue<PrintTarget>.getIndexByLocation(location: Int): Int{
    this.forEachIndexed { index, printTarget ->
        if(location == printTarget.location){
            return index+1
        }
    }
    return 0
}

출처

 

코딩테스트 연습 - 프린터

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

programmers.co.kr

 

728x90

'Algorithm > Programmers' 카테고리의 다른 글

Programmers 완전탐색 :: 레벨2 :: 카펫  (0) 2022.07.19
Programmers 이분탐색 :: 레벨3 :: 입국심사  (0) 2022.07.14
Programmers :: 해시 전화 번호 목록 레벨2  (0) 2022.03.26
Programmers :: 정렬 가장 큰 수 레벨2  (0) 2022.03.23
Programmers :: Heap(힙) 더 맵게 레벨2  (0) 2022.03.23
    'Algorithm/Programmers' 카테고리의 다른 글
    • Programmers 완전탐색 :: 레벨2 :: 카펫
    • Programmers 이분탐색 :: 레벨3 :: 입국심사
    • Programmers :: 해시 전화 번호 목록 레벨2
    • Programmers :: 정렬 가장 큰 수 레벨2
    sggnology
    sggnology
    하늘은 파란색이니까 내 삶도 파란색이길 ㅎㅎ

    티스토리툴바