-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2529.java
More file actions
40 lines (37 loc) · 1.18 KB
/
2529.java
File metadata and controls
40 lines (37 loc) · 1.18 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
import java.io.*;
import java.util.*;
public class Main {
private static int k;
private static boolean[] isVisited = new boolean[10];
private static char[] signs;
private static List<String> result = new ArrayList<>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
k = sc.nextInt();
signs = new char[k];
for (int i = 0; i < k; i++) {
signs[i] = sc.next().charAt(0);
}
dfs("", 0);
Collections.sort(result);
System.out.println(result.get(result.size() - 1));
System.out.println(result.get(0));
}
public static boolean compare(int a, int b, char c) {
if (c == '<') return a < b;
else return a > b;
}
public static void dfs(String num, int depth) {
if (depth == k + 1) {
result.add(num);
return;
}
for (int i = 0; i <= 9; i++) {
if (depth == 0 || !isVisited[i] && compare(num.charAt(num.length() - 1) - '0', i, signs[depth - 1])) {
isVisited[i] = true;
dfs(num + i, depth + 1);
isVisited[i] = false;
}
}
}
}