Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5 mjj111 #192

Merged
merged 3 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions mjj111/graph/์–‘๊ณผ๋Š‘๋Œ€.java
Original file line number Diff line number Diff line change
@@ -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<Integer>());
return maxSheepCnt;
}

private void dfs(int now, int sheepCnt, int wolfCnt, Set<Integer> nexts) {
if (info[now] == 0) sheepCnt++;
else wolfCnt++;

if (wolfCnt >= sheepCnt) return;
maxSheepCnt = Math.max(sheepCnt, maxSheepCnt);

// ๋‹ค์Œ ํƒ์ƒ‰ ์œ„์น˜ ๋ณต์‚ฌ
Set<Integer> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import java.util.*;

class ์ฃผ์‚ฌ์œ„๊ณ ๋ฅด๊ธฐ {

static int N;
static int[][] dices;

static List<Integer> choice = new ArrayList<>();

static List<Integer> arrA;
static List<Integer> 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<Integer> 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);
}
}

}
44 changes: 44 additions & 0 deletions mjj111/implement/ํ‘œํŽธ์ง‘.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.util.*;

class ํ‘œํŽธ์ง‘ {
public String solution(int n, int k, String[] cmd) {
Stack<Integer> 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();
}
}
Loading