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

4 mjj111 #186

Merged
merged 2 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);
}
}
}
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