특정 과목들의 성적의 합으로 서류전형 합격여부를 결정한다.
요구하는 과목수와 반영된다고 공개된 과목이 주어질 경우
최소 점수와 최대 점수 구하기.
과목은 중복되지 않는다.
수강 과목수N 요구 과목수 M 대학에서 공개한 과목수 K
6 3 2
calculus 100
probability 70
physics 50
chemistry 80
python 90
algorithm 100
physics
python
요컨데 대학에서 공개한 과목수는 정해진 상태에서
M-K 과목수를 N-K 수강과목수 중에서 고르는 경우를 생각하면 될 것 같다.
Map을 사용해보면 좋을 것 같다.
K로 받은 값들을 Map에서 꺼내서 더해 놓고
값들 중 최대 최소를 구하기
점수들을 정렬해놓고 M-K개 만큼 앞뒤에서 더해주면 될 것 같다.
풀이 방식
1. split으로 떼내서 값들을 세팅한다.
2. Map을 사용한 다음 먼저 선택된 과목들은 미리 더해놓고 Map 리스트에서 제외한다.
4. Map의 값들을 오름차순으로 정렬하고 최소,최대를 각각 맨앞과 맨뒤에서 추가로 요구되어야 할 과목수 ( M-K개)만큼 더해서 구하는 방식으로 풀었다.
package 백준.브실이의입시전략29723;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Map<String, Integer> map = new HashMap<>();
int result = 0;
int minResult = 0;
int maxResult = 0;
String count = br.readLine();
int[] list = Arrays.stream(count.split(" ")).mapToInt(Integer::parseInt).toArray();
int totalIndex = list[0];
int needIndex = list[1];
int providedIndex = list[2];
for (int i = 0; i < totalIndex; i++) {
String[] str = br.readLine().split(" ");
map.put(str[0], Integer.parseInt(str[1]));
}
for (int i = 0; i < providedIndex; i++) {
String choice = br.readLine();
result += map.get(choice);
map.remove(choice);
}
// map의 값들을 오름차순 정렬
int [] lists = map.values().stream().mapToInt(i -> i).toArray();
Arrays.sort(lists);
for (int i = 0; i < needIndex-providedIndex; i++) {
minResult += lists[i];
maxResult += lists[(totalIndex-1)-i-providedIndex];
}
minResult += result;
maxResult += result;
System.out.printf("%d %d\n", minResult, maxResult);
br.close();
}
}
'자바 코딩테스트' 카테고리의 다른 글
리트코드 349 두 배열의 교집합 (0) | 2025.04.22 |
---|---|
백준 25325 학생 인기도 측정 (1) | 2025.04.18 |
백준 1181 단어정렬 (1) | 2025.04.16 |
백준 25757 임스와 함께하는 미니게임 (1) | 2025.04.15 |
리트코드187. Repeated DNA Sequences (0) | 2025.04.14 |