1 minute read

๐Ÿ“Œ ๋‚œ์ด๋„

๐Ÿฅˆ Silver 2


๐Ÿ“Œ ๋ฌธ์ œ

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


image


๐Ÿ“Œ ํ’€์ด

์ด ๋ฌธ์ œ๋ฅผ ์ฒ˜์Œ ๋ณด์ž๋งˆ์ž ๋“  ์ƒ๊ฐ์€ ๋ฐ”๋กœ Two Pointer ์˜ ์—ฐ์žฅ์„ ์ด๋ผ๋Š” ์ƒ๊ฐ์ด์—ˆ๋‹ค.
n x n ์˜ ๋ฐฐ์—ด์ค‘ ๋งˆ์ง€๋ง‰์— ์žˆ๋Š” ๊ฐ’๋“ค๋งŒ ์„œ๋กœ ๋น„๊ตํ•ด์„œ ํฐ ๊ฐ’์„ ๋นผ๋‹ค๊ฐ€ n๋ฒˆ์งธ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์—ฟ๋‹ค.

๋”ฐ๋ผ์„œ, int[][] table ๊ณผ int[] tableIndex ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ , tableIndex์˜ ๊ฐ’์€ n์œผ๋กœ ์ดˆ๊ธฐํ™” ํ•ด์ฃผ์—ˆ๋‹ค.
for๋ฌธ์ด ํ•œ๋ฒˆ ๋Œ๋•Œ๋งˆ๋‹ค tableIndex์— ์žˆ๋Š” ๊ฐ’์„ table์˜ ์ฃผ์†Œ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ, ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๊ฐ€์ง„ ์ฃผ์†Œ์— ํ•ด๋‹นํ•˜๋Š” tableIndex๊ฐ’์„ -1 ํ•ด์ฃผ์—ˆ๋‹ค.

์ด์ œ ์‹ค๋ฒ„ ๋ฌธ์ œ๋“ค์€ ๊ฑฐ๋œฌํ•˜๊ฒŒ ํ•ด๊ฒฐ์ด ๊ฐ€๋Šฅํ•  ๊ฒƒ ๊ฐ™๋‹ค.


๐Ÿ“Œ Code

package DS;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class N2075 {
    static int[][] table;
    static int[] tableIndex;
    static int n;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        n = Integer.parseInt(br.readLine());
        table = new int[n + 1][n];
        tableIndex = new int[n];
        Arrays.fill(tableIndex, n);
        for (int i = 0; i < n; i++) {
            table[0][i] = -1200000000;
        }

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

        int result = 0;
        for (int i = 0; i < n; i++) {
            result = FindMax();
        }
        System.out.println(result);
    }

    public static int FindMax() {
        int flag = 0;
        int max = -1200000000;
        for (int i = 0; i < n; i++) {
            if (max < table[tableIndex[i]][i]) {
                flag = i;
                max = table[tableIndex[i]][i];
            }
        }
        tableIndex[flag] -= 1;
        return max;
    }
}

package (์ด๋ฆ„); ๋ฅผ ๋•Œ๊ณ , class ์ด๋ฆ„์„ Main์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค.



๊ฐœ์ธ ๊ณต๋ถ€ ๊ธฐ๋ก์šฉ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
ํ‹€๋ฆฌ๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์ œ๋ณดํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜