-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion5.java
More file actions
33 lines (30 loc) · 1.26 KB
/
Copy pathQuestion5.java
File metadata and controls
33 lines (30 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package practice4_sort;
import java.util.ArrayList;
public class Question5 {
public int solution(int[][] board){
int answer=0;
int n = board.length;
ArrayList<Integer> row = new ArrayList<>();
ArrayList<Integer> col = new ArrayList<>();
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(board[i][j] == 1){
row.add(i);
col.add(j);
}
}
}
col.sort((a, b) -> a - b);
int x = row.get(row.size() / 2);
int y = col.get(col.size() / 2);
for(int p : row) answer += Math.abs(x - p);
for(int p : col) answer += Math.abs(y - p);
return answer;
}
public static void main(String[] args){
Question5 T = new Question5();
System.out.println(T.solution(new int[][]{{1, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 1}, {0, 0, 0, 0, 0}, {0, 0, 1, 0, 0}}));
System.out.println(T.solution(new int[][]{{1, 0, 0, 0, 1}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 1, 0}}));
System.out.println(T.solution(new int[][]{{1, 0, 0, 0, 1, 1}, {0, 1, 0, 0, 1, 0}, {0, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 1, 1}}));
}
}