2020 KAKAO BLIND RECRUITMENT 기둥과 보 설치
유형
코드
import java.util.*;
class Solution {
class Result implements Comparable<Result> {
int x;
int y;
int structure;
public Result(int x, int y, int structure) {
this.x = x;
this.y = y;
this.structure = structure;
}
@Override
public int compareTo(Result o) {
if(this.x > o.x) {
return 1;
} else if(this.x < o.x) {
return -1;
} else if(this.x == o.x) {
if(this.y > o.y) {
return 1;
} else if(this.y < o.y) {
return -1;
} else if(this.y == o.y) {
if(this.structure > o.structure) {
return 1;
} else if(this.structure < o.structure) {
return -1;
}
}
}
return 0;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Result result = (Result) o;
return x == result.x && y == result.y && structure == result.structure;
}
}
public int[][] solution(int n, int[][] build_frame) {
List<Result> resultList = new ArrayList<>();
for (int i = 0; i < build_frame.length; i++) {
int x = build_frame[i][0];
int y = build_frame[i][1];
int structure = build_frame[i][2];
int action = build_frame[i][3];
execute(resultList, x, y, structure, action);
}
Collections.sort(resultList);
int[][] answer = new int[resultList.size()][3];
for (int i = 0; i < answer.length; i++) {
answer[i][0] = resultList.get(i).x;
answer[i][1] = resultList.get(i).y;
answer[i][2] = resultList.get(i).structure;
}
return answer;
}
private void execute(List<Result> resultList, int x, int y, int structure, int action) {
boolean success = false;
if(action == 0) {
success = canUninstall(resultList, x, y, structure);
if(success == false) {
resultList.add(new Result(x, y, structure));
}
}
else if(action == 1) {
success = canInstall(resultList, x, y, structure);
if(success) {
resultList.add(new Result(x, y, structure));
}
}
}
private boolean canUninstall(List<Result> resultList, int x, int y, int structure) {
boolean success= true;
resultList.remove(new Result(x, y, structure));
for (Result result: resultList) {
success = canInstall(resultList, result.x, result.y, result.structure);
if(success == false) {
return false;
}
}
return success;
}
private boolean canInstall(List<Result> resultList, int x, int y, int structure) {
boolean success= false;
if(structure == 0) {
if(y == 0) {
success = true;
} else {
if(resultList.contains(new Result(x, y - 1, 0))
|| resultList.contains(new Result(x - 1, y, 1))
|| resultList.contains(new Result(x, y, 1))) {
success = true;
}
}
}
else if(structure == 1) {
if(resultList.contains(new Result(x, y - 1, 0))
|| resultList.contains(new Result(x + 1, y - 1, 0))
|| (resultList.contains(new Result(x - 1, y, 1)) && resultList.contains(new Result(x + 1, y, 1)))) {
success = true;
}
}
return success;
}
}