본문 바로가기
항해

[DAY49] 99클럽 코딩테스트 JAVA 백준 부등호 2529번

by neVerThe1ess 2025. 2. 5.

https://www.acmicpc.net/problem/2529

 

🌕 최종코드

import java.io.*;
import java.util.*;

 
public class Main {

	static int n;
	static boolean[] visited;
	static String[] arr;
	static List<String> list = new ArrayList<>();
	
	static void dfs(String num, int idx) {	
		if(idx == n+1) {		
			list.add(num);
			return;
		}

	
		for(int j = 0 ; j < 10; j++) {
			// 사용한 숫자인지 체크
			if(visited[j]) {
				continue;
						
			}		
			if(idx == 0 || ckeck(Character.getNumericValue(num.charAt(idx - 1)), j , arr[idx-1])) {
				visited[j] = true;			
				dfs(num+j, idx+1);
				visited[j] = false;				
			}
		}
		
	}
	
	static boolean ckeck(int a, int b, String c) {
	
		if (c.equals(">")) {
			if(a < b) return false;
		} else if (c.equals("<")){
			if(a > b) return false;
		}
		return true;
	}

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		n = Integer.parseInt(br.readLine());
		arr = br.readLine().split(" ");

		visited = new boolean[10];
		
		
		dfs("",0);

		//최대값
		System.out.print(list.get(list.size() - 1) + "\n");
		//최소값
		System.out.print(list.get(0));
		
	}

}