240522 N번째 큰 수

Jongleee·2024년 5월 22일
0

TIL

목록 보기
579/737
static class Info implements Comparable<Info> {
	int row;
	int col;
	int value;

	public Info(int row, int col, int value) {
		this.row = row;
		this.col = col;
		this.value = value;
	}

	@Override
	public int compareTo(Info other) {
		return Integer.compare(other.value, this.value);
	}
}

public static void main(String[] args) throws IOException {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	StringTokenizer st;

	int n = Integer.parseInt(br.readLine());
	int[][] matrix = new int[n][n];

	for (int i = 0; i < n; i++) {
		st = new StringTokenizer(br.readLine());
		for (int j = 0; j < n; j++) {
			matrix[i][j] = Integer.parseInt(st.nextToken());
		}
	}

	PriorityQueue<Info> pq = new PriorityQueue<>();
	for (int j = 0; j < n; j++) {
		pq.add(new Info(n - 1, j, matrix[n - 1][j]));
	}

	Info maxInfo = null;
	for (int i = 0; i < n; i++) {
		maxInfo = pq.poll();
		int row = maxInfo.row;
		int col = maxInfo.col;

		if (row > 0) {
			pq.add(new Info(row - 1, col, matrix[row - 1][col]));
		}
	}

	System.out.println(maxInfo.value);
}

출처:https://www.acmicpc.net/problem/2075

0개의 댓글