Skip to content

Commit

Permalink
Merge pull request #165 from AlgoLeadMe/14-jung0115
Browse files Browse the repository at this point in the history
14-jung0115
  • Loading branch information
jung0115 authored Nov 21, 2024
2 parents 4f26b45 + b2f729f commit 03b5dd8
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 1 deletion.
3 changes: 2 additions & 1 deletion jung0115/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
| 10차시 | 2024.08.28.수 | 브루트포스 | [램프(1034)](https://www.acmicpc.net/problem/1034) | https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/151 |
| 11차시 | 2024.09.04.수 | 수학 | [축구(1344)](https://www.acmicpc.net/problem/1344) | https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/153 |
| 12차시 | 2024.09.07.토 | 다이나믹 프로그래밍 | [개근상(1563)](https://www.acmicpc.net/problem/1563) | https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/157 |
| 13차시 | 2024.09.23.월 | 다이나믹 프로그래밍 | [사전(1256)](https://www.acmicpc.net/problem/1256) | https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/162 |
| 13차시 | 2024.09.23.월 | 다이나믹 프로그래밍 | [사전(1256)](https://www.acmicpc.net/problem/1256) | https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/162 |
| 14차시 | 2024.09.25.수 | 그리디 알고리즘 | [컵라면(1781)](https://www.acmicpc.net/problem/1781) | https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/165 |
66 changes: 66 additions & 0 deletions jung0115/그리디알고리즘/Baekjoon_1781.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 14차시 2024.09.25.수 : 백준 - 컵라면(1781)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Arrays;
import java.util.PriorityQueue;

public class Baekjoon_1781 {
static class CupNoodle implements Comparable<CupNoodle> {
int deadLine;
int count;

public CupNoodle(int deadLine, int count) {
this.deadLine = deadLine;
this.count = count;
}

@Override
public int compareTo(CupNoodle o) {
// 데드라인을 기준으로 오름차순 정렬
return this.deadLine - o.deadLine;
}
}

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int N = Integer.parseInt(br.readLine());

CupNoodle[] cupNoodles = new CupNoodle[N];

for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());

int deadLine = Integer.parseInt(st.nextToken()); // 데드라인
int count = Integer.parseInt(st.nextToken()); // 맞힐 때 받는 컵라면 수

cupNoodles[i] = new CupNoodle(deadLine, count);
}

// 데드라인을 기준으로 정렬 (오름차순)
Arrays.sort(cupNoodles);

PriorityQueue<Integer> pq = new PriorityQueue<>();

for (CupNoodle cn : cupNoodles) {
// 만약 현재 데드라인 내에 풀 수 있는 문제라면 큐에 추가
pq.offer(cn.count);

// 큐의 크기가 데드라인을 초과하면 가장 적은 컵라면 수를 제거
if (pq.size() > cn.deadLine) {
pq.poll();
}
}

int answer = 0;

// 큐에 남아 있는 컵라면의 총합을 구함
while (!pq.isEmpty()) {
answer += pq.poll();
}

System.out.println(answer);
}
}
37 changes: 37 additions & 0 deletions jung0115/그리디알고리즘/Baekjoon_1781_fail.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 14차시 2024.09.25.수 : 백준 - 컵라면(1781)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.HashMap;
import java.util.Iterator;

public class Baekjoon_1781_fail {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int N = Integer.parseInt(br.readLine());

HashMap<Integer, Integer> cupNoodles = new HashMap<Integer, Integer>();

for(int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());

int deadLine = Integer.parseInt(st.nextToken()); // 데드라인
int count = Integer.parseInt(st.nextToken()); // 맞힐 때 받는 컵라면 수

int current = cupNoodles.getOrDefault(deadLine, 0);
if(current < count) cupNoodles.put(deadLine, count);
}

int answer = 0;

Iterator<Integer> keys = cupNoodles.keySet().iterator();
while(keys.hasNext()){
int key = keys.next();
answer += cupNoodles.get(key);
}

System.out.print(answer);
}
}

0 comments on commit 03b5dd8

Please sign in to comment.