diff --git "a/mjj111/graph/\354\226\221\352\263\274\353\212\221\353\214\200.java" "b/mjj111/graph/\354\226\221\352\263\274\353\212\221\353\214\200.java" new file mode 100644 index 0000000..036ab0c --- /dev/null +++ "b/mjj111/graph/\354\226\221\352\263\274\353\212\221\353\214\200.java" @@ -0,0 +1,39 @@ +import java.util.*; + +class 양과늑대 { + + int[][] edges; + int[] info; + int maxSheepCnt = 0; + + public int solution(int[] Info, int[][] Edges) { + info = Info; + edges = Edges; + dfs(0, 0, 0, new HashSet()); + return maxSheepCnt; + } + + private void dfs(int now, int sheepCnt, int wolfCnt, Set nexts) { + if (info[now] == 0) sheepCnt++; + else wolfCnt++; + + if (wolfCnt >= sheepCnt) return; + maxSheepCnt = Math.max(sheepCnt, maxSheepCnt); + + // 다음 탐색 위치 복사 + Set copySet = new HashSet<>(nexts); + + // 다음 탐색 목록중 현재 위치제외 + copySet.remove(now); + + for(int[] edge : edges) { + int start = edge[0]; + int end = edge[1]; + if(start == now) copySet.add(end); + } + + for (int next : copySet) { + dfs(next, sheepCnt, wolfCnt, copySet); + } + } +} diff --git "a/mjj111/implement/\354\243\274\354\202\254\354\234\204\352\263\240\353\245\264\352\270\260.java" "b/mjj111/implement/\354\243\274\354\202\254\354\234\204\352\263\240\353\245\264\352\270\260.java" new file mode 100644 index 0000000..6d6a979 --- /dev/null +++ "b/mjj111/implement/\354\243\274\354\202\254\354\234\204\352\263\240\353\245\264\352\270\260.java" @@ -0,0 +1,104 @@ +import java.util.*; + +class 주사위고르기 { + + static int N; + static int[][] dices; + + static List choice = new ArrayList<>(); + + static List arrA; + static List arrB; + + static int[] answer; + static int max = Integer.MIN_VALUE; + + public static int[] solution(int[][] dice) { + N = dice.length; + dices = dice; + answer = new int[N / 2]; + choiceDice(0, 0); + return answer; + } + + public static void choiceDice(int depth, int s) { + if (depth == N / 2) { + int winning = calculateWinningPercent(); + if (max < winning) { + max = winning; + for (int i = 0; i < choice.size(); i++) { + answer[i] = choice.get(i) + 1; + } + } + return; + } + + for (int i = s; i < N; i++) { + choice.add(i); + choiceDice(depth + 1, i + 1); + choice.remove(choice.size() - 1); + } + } + + private static int calculateWinningPercent() { + int count = 0; + + makeArrAB(); + + Collections.sort(arrB); + for (int i = 0; i < arrA.size(); i++) { + int number = arrA.get(i); + + int left = 0, right = arrB.size() - 1; + + int index = Integer.MIN_VALUE; + while (left <= right) { + int middle = (left + right) / 2; + + if (arrB.get(middle) < number) { + left = middle + 1; + index = Math.max(index, middle); + } else { + right = middle - 1; + } + } + if (index != Integer.MIN_VALUE) { + count += index + 1; + } + } + return count; + } + + public static void makeArrAB() { + arrA = new ArrayList<>(); + arrB = new ArrayList<>(); + + int[][] diceA = new int[N / 2][6]; + int[][] diceB = new int[N / 2][6]; + int a = 0, b = 0; + for (int i = 0; i < N; i++) { + if (choice.contains(i)) { + diceA[a] = dices[i]; + a++; + } else { + diceB[b] = dices[i]; + b++; + } + } + + makeArr(0, diceA, 0, arrA); + makeArr(0, diceB, 0, arrB); + } + + public static void makeArr(int depth, int[][] dice, int sum, List arr) { + if (depth == N / 2) { + arr.add(sum); + return; + } + for (int i = 0; i < 6; i++) { + int newSum = sum + dice[depth][i]; + makeArr(depth + 1, dice, newSum, arr); + } + } + +} \ No newline at end of file diff --git "a/mjj111/implement/\355\221\234\355\216\270\354\247\221.java" "b/mjj111/implement/\355\221\234\355\216\270\354\247\221.java" new file mode 100644 index 0000000..3f30a24 --- /dev/null +++ "b/mjj111/implement/\355\221\234\355\216\270\354\247\221.java" @@ -0,0 +1,44 @@ +import java.util.*; + +class 표편집 { + public String solution(int n, int k, String[] cmd) { + Stack cancels = new Stack<>(); + int now = k; + int totalRows = n; + + for (String c : cmd) { + if (c.charAt(0) == 'U') { + int value = Integer.parseInt(c.split(" ")[1]); + now -= value; + continue; + } + if (c.charAt(0) == 'D') { + int value = Integer.parseInt(c.split(" ")[1]); + now += value; + continue; + } + if (c.charAt(0) == 'C') { + cancels.push(now); + totalRows--; + if (now == totalRows) now--; + continue; + } + if (c.charAt(0) == 'Z') { + int cancelledRow = cancels.pop(); + if (cancelledRow <= now) now++; + totalRows++; + } + } + + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < totalRows; i++) { + sb.append("O"); + } + + while (!cancels.isEmpty()) { + sb.insert(cancels.pop().intValue(), "X"); + } + + return sb.toString(); + } +}