-
Notifications
You must be signed in to change notification settings - Fork 0
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
[프로그래머스] 2차원 동전 뒤집기 #56
Comments
풀이 언어
코드class Solution {
private int[][] allFlipped;
private int[][] tempArr;
private int row;
private int col;
public int solution(int[][] beginning, int[][] target) {
int answer = Integer.MAX_VALUE;
row = beginning.length;
col = beginning[0].length;
allFlipped = new int[row][col];
for(int i = 0; i < row; i++) {
for(int j = 0; j < col; j++) {
allFlipped[i][j] = 1 - beginning[i][j];
}
}
int cnt = 0;
for(int i = 0; i < 1 << row; i++) {
tempArr = new int[row][col];
int count = 0;
for(int j = 0; j < row; j++) {
int temp = 1 << j;
int and = i & temp;
if(and == 0) {
tempArr[j] = beginning[j].clone();
} else {
tempArr[j] = allFlipped[j].clone();
count += 1;
}
}
for(int k = 0; k < col; k++) {
for(int t = 0; t < row; t++) {
if(target[t][k] != tempArr[t][k]) {
flipCol(k);
count++;
break;
}
}
}
if(check(tempArr, target)) {
answer = Math.min(count, answer);
}
}
if(answer == Integer.MAX_VALUE) {
return -1;
}
return answer;
}
private void flipCol(int targetCol) {
for(int i = 0; i < row; i++) {
tempArr[i][targetCol] = 1 - tempArr[i][targetCol];
}
}
private boolean check(int[][] arr, int[][] target) {
for(int i = 0; i < row; i++) {
for(int j = 0; j < col; j++) {
if(arr[i][j] != target[i][j]) {
return false;
};
}
}
return true;
}
} 핵심 로직 혹은 자료구조시간 복잡도
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TITLE
2차원 동전 뒤집기
LINK
📷 Screenshots
댓글 양식
The text was updated successfully, but these errors were encountered: