From 62243d104a938c0a57ab1d01fbcb4dd1c79b1246 Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Fri, 3 Oct 2025 21:35:41 +0900 Subject: [PATCH] =?UTF-8?q?[20251003]=20PGM=20/=20Lv3=20/=20=EC=A3=BC?= =?UTF-8?q?=EC=82=AC=EC=9C=84=20=EA=B3=A0=EB=A5=B4=EA=B8=B0=20/=20?= =?UTF-8?q?=EC=9D=B4=EA=B0=95=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4 \352\263\240\353\245\264\352\270\260.md" | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 "lkhyun/202510/03 PGM Lv3 \354\243\274\354\202\254\354\234\204 \352\263\240\353\245\264\352\270\260.md" diff --git "a/lkhyun/202510/03 PGM Lv3 \354\243\274\354\202\254\354\234\204 \352\263\240\353\245\264\352\270\260.md" "b/lkhyun/202510/03 PGM Lv3 \354\243\274\354\202\254\354\234\204 \352\263\240\353\245\264\352\270\260.md" new file mode 100644 index 00000000..1a0c3642 --- /dev/null +++ "b/lkhyun/202510/03 PGM Lv3 \354\243\274\354\202\254\354\234\204 \352\263\240\353\245\264\352\270\260.md" @@ -0,0 +1,97 @@ +```java +import java.util.*; + +class Solution { + static int n; + static int maxWin; + static int[] answer; + + public int[] solution(int[][] dice) { + n = dice.length; + maxWin = 0; + + combination(new boolean[n], 0, 0, dice); + + return answer; + } + + void combination(boolean[] selected, int idx, int count, int[][] dice) { + if (count == n / 2) { + int winCount = getWinCount(selected, dice); + + if (winCount > maxWin) { + maxWin = winCount; + answer = getSelectedDice(selected); + } + return; + } + + if (idx == n) return; + + selected[idx] = true; + combination(selected, idx + 1, count + 1, dice); + + selected[idx] = false; + combination(selected, idx + 1, count, dice); + } + + int getWinCount(boolean[] selected, int[][] dice) { + List aList = new ArrayList<>(); + List bList = new ArrayList<>(); + + makeSum(selected, dice, true, 0, 0, aList); + makeSum(selected, dice, false, 0, 0, bList); + + Collections.sort(bList); + + int win = 0; + for (int a : aList) { + win += lowerBound(bList, a); + } + + return win; + } + + void makeSum(boolean[] selected, int[][] dice, boolean isA, int idx, int sum, List list) { + if (idx == n) { + list.add(sum); + return; + } + + if (selected[idx] == isA) { + for (int num : dice[idx]) { + makeSum(selected, dice, isA, idx + 1, sum + num, list); + } + } else { + makeSum(selected, dice, isA, idx + 1, sum, list); + } + } + + int lowerBound(List list, int target) { + int left = 0; + int right = list.size(); + + while (left < right) { + int mid = (left + right) / 2; + if (list.get(mid) < target) { + left = mid + 1; + } else { + right = mid; + } + } + + return left; + } + + int[] getSelectedDice(boolean[] selected) { + int[] result = new int[n / 2]; + int idx = 0; + for (int i = 0; i < n; i++) { + if (selected[i]) { + result[idx++] = i + 1; + } + } + return result; + } +} +```