문제
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
풀이
DP로 풀기는 했는데 백트래킹이나 BFS, 조합으로도 풀 수 있는 것 같다. N이 최대 20이라서 조합으로 푸는 게 쉬웠을지도?
import java.util.*;
public class Solution {
private static final Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int T = sc.nextInt();
for (int tc = 1; tc <= T; tc++) {
System.out.println("#" + tc + " " + solution());
}
sc.close();
}
private static int solution() {
int N = sc.nextInt();
int B = sc.nextInt();
int[] H = new int[N];
int sum = 0;
for (int i = 0; i < N; i++) {
H[i] = sc.nextInt();
sum += H[i];
}
Arrays.sort(H);
boolean[] dp = new boolean[sum + 1];
dp[0] = true;
for (int h: H) {
for (int i = sum; i >= h; i--) {
if (dp[i - h]) dp[i] = true;
}
}
for (int i = B; i <= sum; i++) {
if (dp[i]) return i - B;
}
return -1;
}
}'PS > SWEA' 카테고리의 다른 글
| SWEA 1861 - 정사각형 방 [Java] (0) | 2026.07.21 |
|---|---|
| SWEA 1210 - Ladder1 [Java] (0) | 2026.07.20 |
| SWEA 1868 - 파핑파핑 지뢰찾기 [Java] (0) | 2026.07.18 |
| SWEA 4193 - 수영대회 결승전 [Java] (0) | 2026.07.18 |
| [Computational Thinking] 수와 표현 (0) | 2026.07.02 |