정답률 30퍼인데 의외로 잘 푼 것 같아서(시간도 다른 이들에 비해 짧고 메모리도 잘 써서)
공유해둔다.
재귀를 k 보다 더 깊이 들어가지 않음으로써 시간을 아낌.
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Solution {
private static int min;
private static int[] typeA;
private static int[] typeB;
public static void main(String[] args) throws Exception {
FastScanner scan = new FastScanner();
int testcases = scan.nextInt();
for (int testcase = 1; testcase <= testcases; testcase++) {
int height = scan.nextInt();
int width = scan.nextInt();
int k = scan.nextInt();
int[][] film = new int[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
film[i][j] = scan.nextInt();
}
}
typeA = new int[width];
typeB = new int[width];
for (int i = 0; i < width; i++)
typeB[i]++;
min = k;
func(film, height, width, k, 0, 0);
System.out.println("#" + testcase + " " + min);
}
}
private static void func(int[][] film, int height, int width, int k, int index, int count) {
if (check(film, height, width, k)) {
if (min > count)
min = count;
return;
}
if (count >= k - 1)
return;
for (int i = index; i < height; i++) {
int[] temp = film[i];
film[i] = typeA;
func(film, height, width, k, i + 1, count + 1);
film[i] = typeB;
func(film, height, width, k, i + 1, count + 1);
film[i] = temp;
}
}
private static boolean check(int[][] film, int height, int width, int k) {
for (int j = 0; j < width; j++) {
int count = 1;
for (int i = 0; i < height - 1 && count < k; i++) {
if (film[i][j] == film[i + 1][j])
count++;
else
count = 1;
}
if (count < k)
return false;
}
return true;
}
private static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
private int nextInt() throws Exception {
if (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return Integer.parseInt(st.nextToken());
}
private String nextString() throws Exception {
return br.readLine();
}
}
}
|
cs |
'Coding Interview' 카테고리의 다른 글
코딩 검사할 때 엣지케이스 찾기 리스트 (0) | 2020.06.14 |
---|---|
[2020 KAKAO BLIND RECRUITMENT] 자물쇠와 열쇠 (0) | 2020.06.10 |
[SW Expert Academy] 2477. [모의 SW 역량테스트] 차량 정비소 (0) | 2020.06.04 |
[JAVA] 전체 값을 나열하는 순열 코드 (0) | 2020.04.27 |
CS 인터뷰 준비 방법(코딩 테스트, 전산학) (0) | 2020.04.09 |