🤍 전 강의 25% 할인 중 🤍

2024년 상반기를 돌아보고 하반기에도 함께 성장해요!
인프런이 준비한 25% 할인 받으러 가기 >>

  • 카테고리

    질문 & 답변
  • 세부 분야

    알고리즘 · 자료구조

  • 해결 여부

    미해결

테스트 케이스 추가 질문

21.06.07 23:56 작성 조회수 149

0

안녕하세요! 아래와 같이 구현하였는데, 주어진 케이스만으로는 검증이 되지 않아서요! 혹시 다른 케이스 제공 부탁드려도 될까요?

- 구현 -

1. ArrayList의 time 부분을 해시맵으로 구현 -> time 종류의 수를 통해 그 수만큼만 반복하도록 while문을 구현하였습니다.(테스트 케이스의 경우 1, 2, 3 으로 3종류이므로 3번 반복)

2. 0번째, 즉 ArrayList의 맨 앞의 부분을 반복 이전에 미리 빼두었습니다. 

3. while 내에 for문을 통해 i를 갱신하며 이전에 탐색했던 부분은 지나가도록 구현하였습니다.

import java.io.IOException;
import java.util.*;


class Pay {
int money;
int time;

public Pay(int money, int time) {
this.money = money;
this.time = time;
}
}
public class Main {

public static void main(String[] args) throws IOException {
Main T = new Main();
Scanner sc = new Scanner(System.in);

int n = sc.nextInt();
HashMap<Integer, Integer> hm = new HashMap<>();
ArrayList<Pay> arr = new ArrayList<>();
for (int i = 0; i < n; i++) {
int m = sc.nextInt();
int t = sc.nextInt();
hm.put(t, hm.getOrDefault(hm.get(t), 0)+1);

arr.add(new Pay(m, t));
}

Collections.sort(arr, new Comparator<Pay>() {
@Override
public int compare(Pay o1, Pay o2) {
if(o2.time == o1.time) {
return o2.money - o1.money;
} else
return o2.time - o1.time;
}
});

PriorityQueue<Integer> pQ = new PriorityQueue<>(Collections.reverseOrder());

int day = arr.get(0).time;
int key = hm.size();
int j = 1;
int result = 0;
pQ.offer(arr.get(0).money);
while(key > 0) {
for(int i=j; i<arr.size(); i++) {
if(day == arr.get(i).time) {
pQ.offer(arr.get(i).money);
} else {
day=arr.get(i).time;
j = i;
break;
}
}
key --;
result += pQ.poll();
}
System.out.println(result);
}
}

답변 2

·

답변을 작성해보세요.

1

안녕하세요^^

반례입니다.

10

13 2

18 1

68 10

72 8

11 7

41 2

48 7

15 7

34 1

13 8

답은 302입니다.

0

leejohy님의 프로필

leejohy

질문자

2021.06.08

바쁘실텐데 답변 감사드립니다! 원인을 찾았네요~~

채널톡 아이콘