콘텐츠로 바로가기

now0930 일지

이런저런 생각

  • 홈
  • 비공개
  • 강좌
  • 잔여 작업 조회
  • 위치

프로그래머스 이중우선큐

아래처럼하면 될듯하다.

위 방법대로 하려다 잘 안된다. max 큐에서 min큐로 변경될 때 연산이 하나씩 틀어진다. 결국 max queue를 poll하면 min queue를 클리어하고, max queue로 업데이트 했다.


import java.util.Collections;
import java.util.regex.*;
import java.util.PriorityQueue;

public class Main {

	public static void main(String[] args) {
		// 시작..
		//		String[] operations = { "I16", "I10", "I20", "D1" };
		String[] operations = { "I -45", "I 653", "D 1", "I -642", "I 45", "I 97", "D 1", "D -1", "I 333" };

		Solution sol = new Solution();
		int[] t = sol.solution(operations);
		System.out.println(t[0]+","+ t[1]);
	}// void main

}

class Solution {
	public int[] solution(String[] operations) {
		Pattern tmpNumber = Pattern.compile("(-[0-9]{1,}|[0-9]{1,})");
		PriorityQueue<Work> pqMax = new PriorityQueue<>();
		PriorityQueue<Work> pqMin = new PriorityQueue<>(Collections.reverseOrder());


		for (int in = 0; in < operations.length; in++) {
			//			System.out.println("인자: " + operations[in] + operations[in]);
			String op = operations[in].substring(0, 1);
			//정규표현식으로 숫자만 출력.
			Matcher tmpMatcher = tmpNumber.matcher(operations[in]);
			String tmpNum = "";
			Integer num = 0;
			//			int num = Integer.parseInt(tmpMatcher.group());
			while (tmpMatcher.find()) {
				tmpNum = tmpMatcher.group();
				//				System.out.println(tmpMatcher.group());
			}
			num = Integer.parseInt(tmpNum);
			//			System.out.println(num);
			//			System.out.println(tmpMatcher.find());

			//			int num = Integer.parseInt(operations[in + 1]);
			if (op.equals("I")) {

				pqMax.add(new Work(num));
				pqMin.add(new Work(num));

			} else if (op.equals("D"))
				if (num.equals(1)) {
					pqMax.poll();
					if (pqMax.size() == 0)
						//size가 0이면 마지막 1개를 취출했음.
						pqMin.poll();
					else {
						pqMin.clear();
						pqMin.addAll(pqMax);
					}
				} else if (num.equals(-1)) {
					pqMin.poll();
					if (pqMin.size() == 0)
						//size가 0이면 마지막 1개를 취출했음.
						pqMax.poll();
					else {
						pqMax.clear();
						pqMax.addAll(pqMin);
					}

				}

		}

		/*
		 * System.out.println("Max 큐"); while (!pqMax.isEmpty()) {
		 * System.out.println(pqMax.poll().number); }
		 * 
		 * System.out.println("Min 큐"); while (!pqMin.isEmpty()) {
		 * System.out.println(pqMin.poll().number); }
		 */

		int[] answer = new int[2];
		if (pqMax.size() != 0)
			answer[0] = pqMax.poll().number;
		else
			answer[0] = 0;
		if (pqMin.size() != 0)
			answer[1] = pqMin.poll().number;
		else
			answer[1] = 0;

		return answer;
	} //solution

}

class Work implements Comparable<Work> {
	int number;

	Work(Integer num) {
		this.number = num;
	}

	@Override
	public int compareTo(Work arg0) {
		// TODO Auto-generated method stub

		return this.number > arg0.number ? -1 : 1;
	}

}

이 글 공유하기:

  • Tweet
발행일 2019-06-07글쓴이 이대원
카테고리 생활코딩 태그 java, programmers, 코딩, 테스트

댓글 남기기응답 취소

이 사이트는 Akismet을 사용하여 스팸을 줄입니다. 댓글 데이터가 어떻게 처리되는지 알아보세요.

글 내비게이션

이전 글

프로그래머스 디스크컨트롤러

다음 글

프로그래머스 베스트앨범

2025 5월
일 월 화 수 목 금 토
 123
45678910
11121314151617
18192021222324
25262728293031
4월    

최신 글

  • common mode, differential mode 2025-05-11
  • signal conditioner, 신호 처리기 2025-05-10
  • strain gage 2025-05-09
  • 칼만 필터 2025-05-01
  • positioner(I/P) 2025-04-26

카테고리

  • 산업계측제어기술사
  • 삶 자국
    • 책과 영화
    • 투자
  • 생활코딩
    • LEGO
    • ROS
    • tensorflow
  • 전기기사
  • 피아노 악보

메타

  • 로그인
  • 엔트리 피드
  • 댓글 피드
  • WordPress.org

페이지

  • 소개
  • 잔여 작업 조회
    • 작업 추가
    • 작업의 사진 조회
    • 작업 수정 페이지
  • 사진
    • GPS 입력된 사진
    • 사진 조회
  • 위치
    • 하기 휴가 방문지
    • 해외 출장

태그

android bash c docker driver FSM gps java kernel LEGO linux mysql network program opcua open62541 plc programmers python raspberry reinforcementLearning ros state space system program tensorflow transfer function 경제 미국 민수 삼국지 세계사 실기 에너지 역사 유전자 일본 임베디드 리눅스 전기기사 조선 중국 채윤 코딩 테스트 통계 한국사 한국어

팔로우하세요

  • Facebook
now0930 일지
WordPress로 제작.
 

댓글 로드중...