[프로그래머스 스택/큐] (level2) 2번 - 프린터

2021. 6. 29. 18:57·PS/알고리즘

https://programmers.co.kr/learn/courses/30/lessons/42587

 

코딩테스트 연습 - 프린터

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

programmers.co.kr

내풀이

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <string>
#include <vector>
#include <queue>
using namespace std;
 
int solution(vector<int> priorities, int location) {
    int answer = 0;
 
    ///초기
    queue<int> q; int locNum = priorities[location];
    int maxList[10] = {0,};
    for(int K : priorities){
        maxList[K]++;
        q.push(K);
    }
    /////// 1 2 1
    //pop됬을때 location == 0이면 return 1이다
    //반복마다 팝이 일어난다 우선순위가 높은게 맨 앞에 있는 구조를 만든다
    int retSeq = 1;
    while(!q.empty())
    {
        int mxIdx = 9;
        while(maxList[mxIdx] == 0){mxIdx--;}
        while(q.front() != mxIdx)
        {
            int qFT = q.front();q.pop();q.push(qFT);//queue first temp
            location--;
            if(location < 0)
                location += q.size();
        }
        if(q.front() == locNum && location == 0){
            answer = retSeq;
            break;
        }
        maxList[mxIdx]--; q.pop(); location--;retSeq++;
    }
 
    ///////
 
    return answer;
}
Colored by Color Scripter
 

####너의 잘못을 알아라

  1. queue에서 pop()될떄마다
    입력으로 받은 location은 한칸 한칸 앞으로 오게 되는것
    (location이 0으로 되가는것)을 착안했지만

문제는 location이 음수가 될때 의 처리를 잘못해서 애먹었다.
queue.size()가 아니라 vector.size()를 사용함 ㅠㅠ


참고해볼 풀이

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
 
using namespace std;
 
int solution(vector<int> priorities, int location) {
    queue<int> printer;                         //queue에 index 삽입.
    vector<int> sorted;                         //정렬된 결과 저장용
    for(int i=0; i<priorities.size(); i++) {
        printer.push(i);
    }
    while(!printer.empty()) {
        int now_index = printer.front();
        printer.pop();
        if(priorities[now_index] != *max_element(priorities.begin(),priorities.end())) {
            //아닌경우 push
            printer.push(now_index);
        } else {
            //맞는경우
            sorted.push_back(now_index);
            priorities[now_index] = 0;
        }
    }
    for(int i=0; i<sorted.size(); i++) {
        if(sorted[i] == location) return i+1;
    }
}
Colored by Color Scripter
cs

 

 

*Max_element 이거 유용해보이니 잘 써먹자

저작자표시 (새창열림)

'PS > 알고리즘' 카테고리의 다른 글

[프로그래머스 정렬] (level1) 1번 - K번째 수  (0) 2021.06.30
[백준 10814번] 정렬 - 나이순 정렬 (구조체/Pair 자료구조의 compare 함수!)  (0) 2021.06.30
[프로그래머스 스택/큐] (level2) 1번 - 기능개발  (0) 2021.06.28
[프로그래머스] string 입력 참고  (0) 2021.06.28
[백준 10828번] 스택 - 1차 스터디 A  (0) 2021.06.25
'PS/알고리즘' 카테고리의 다른 글
  • [프로그래머스 정렬] (level1) 1번 - K번째 수
  • [백준 10814번] 정렬 - 나이순 정렬 (구조체/Pair 자료구조의 compare 함수!)
  • [프로그래머스 스택/큐] (level2) 1번 - 기능개발
  • [프로그래머스] string 입력 참고
니앙팽이
니앙팽이
  • 니앙팽이
    니앙팽이 블로그
    니앙팽이
  • 전체
    오늘
    어제
    • 분류 전체보기 (126)
      • 그림그리기 (7)
      • 음악 (4)
        • FL Studio & MIDI (2)
        • 자작곡 (2)
      • 게임 (7)
        • 모바일 (0)
        • 스팀 (0)
        • 닌텐도 (0)
        • 개발 (7)
      • CS (44)
        • SW 공학 (27)
        • DB (7)
        • OS (9)
        • 네트워크 (1)
      • 팁 (9)
      • Language (21)
        • C# (8)
        • C&C++ (3)
        • 파이썬 메모 (3)
        • Javascript (7)
      • PS (0)
        • 알고리즘 (24)
        • 자료구조 (8)
        • 수학 (1)
        • 선형대수 (0)
        • 오토마타 (1)
        • 이산수학 (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    파이썬
    연결리스트
    유니티
    프로그래머스
    디자인패턴
    Stack
    객체지향개발
    Javascript
    알고리즘
    KAKAO
    클립 스튜디오
    unity
    노마드 코더
    자료구조
    그림 연습
    따라그리기
    프로세스
    clip studio paint
    가비지 콜렉터
    c#
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
니앙팽이
[프로그래머스 스택/큐] (level2) 2번 - 프린터
상단으로

티스토리툴바