Repository navigation
Expand file tree
/
Copy pathline03.java
More file actions
82 lines (61 loc) · 1.41 KB
/
Copy pathline03.java
File metadata and controls
82 lines (61 loc) · 1.41 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
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
package Test;
import java.util.*;
public class line03 {
static ArrayList<Integer> list;
static LinkedList<Integer> num;
static StringBuilder sb;
static int max = Integer.MIN_VALUE;
private static void perm(int count, int m, String s) {
if (count == m) {// 3
sb = new StringBuilder(s);
for (int i = 0; i < m; i++) {
sb.setCharAt(num.get(i), '1');
}
int cnt = count(sb.toString());
if (cnt > max) {
max = cnt;
}
}
for (int i = 0; i < list.size(); i++) {
if (num.contains(list.get(i)) || !num.isEmpty() && list.get(i) < num.getLast()) {
continue;
}
num.add(list.get(i));
perm(count + 1, m, s);
num.removeLast();
}
}
private static int count(String s) {
int max = 0;
String arr[] = s.split("0");
for (int i = 0; i < arr.length; i++) {
int count = arr[i].length();
if (count > max) {
max = count;
}
}
return max;
}
// 가장 긴 1의길이를 찾아
public static int solution(String road, int n) {
list = new ArrayList<Integer>();
for (int i = 0; i < road.length(); i++) {
if (road.charAt(i) == '0') {
list.add(i);
}
}
num = new LinkedList<Integer>();
if (n > list.size()) {
return road.length();
} else {
perm(0, n, road);
}
System.out.println(max);
return max;
}
public static void main(String[] args) {
String s = "111011110011111011111100011111";
String s1 = "001100";
solution(s, 3);
}
}