PS/BaekJoon

2529 JAVA ๋ถ€๋“ฑํ˜ธ

chaerlo127 2022. 10. 27. 13:00
728x90

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Main {
	
	// attribute
	static String[] array;
	static boolean[] check;
	static ArrayList<String> ans;
	
	public static void main(String[] args) {
		try {
			BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
			int a = Integer.parseInt(bf.readLine()); // ๋ถ€๋“ฑํ˜ธ ๊ฐœ์ˆ˜
			
			array = new String[a]; // ๋ถ€๋“ฑํ˜ธ
			array = bf.readLine().split(" ");
			
			check = new boolean[10];
			ans = new ArrayList<String>();
			
			dfs("", 0, a); // index, a
			
			System.out.println(ans.get(ans.size()-1));
			System.out.println(ans.get(0));
			
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private static void dfs(String string, int index, int a) {
		if(index == a + 1) {  // ์ธ๋ฑ์Šค๊ฐ€ ๋ถ€๋“ฑํ˜ธ ๊ฐœ์ˆ˜ + 1์„ ๋งŒ์กฑ
			ans.add(string);
			return;
		}else {
			for (int i = 0; i < 10; i++) {
				if(check[i]) continue;
				if(index == 0 || compare(string.charAt(string.length()-1) - '0', i, index)) {
					check[i] = true;
					dfs(string+i, index+1, a);
					check[i] = false;
				}
			}
		}
	}
	
	private static boolean compare(int a, int i, int index) {
		if(array[index-1].equals(">")) return a>i;
		else return a<i;
	}
}
728x90