Skip to content

Commit

Permalink
Merge pull request #186 from AlgoLeadMe/4-mjj111
Browse files Browse the repository at this point in the history
4-mjj111
  • Loading branch information
9kyo-hwang authored Aug 11, 2024
2 parents 52e1780 + 5886852 commit 1fc381a
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
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();
}
}

0 comments on commit 1fc381a

Please sign in to comment.