2075N번째 큰 수

LJM·2023년 1월 7일
0

백준풀기

목록 보기
20/259

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

ArrayList 를 스택처럼 사용해서 풀었다

시간복잡도는 최악의 경우 N^2 로 생각된다

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


public class Main {
    public static void main(String[] args) {

        FastReader fr = new FastReader();

        int N = Integer.parseInt(fr.nextLine());

        ArrayList<ArrayList<Integer>> arrList = new ArrayList<>();

        for(int i = 0; i < N;++i)
        {
            arrList.add(new ArrayList<>());
        }

        for(int i = 0; i < N; ++i)
        {
            for(int j = 0; j < N; ++j)
            {
                arrList.get(j).add(fr.nextInt());
            }
        }

        int cnt = 0;
        int[] indexes = new int[N];
        int max = 0;
        int maxIdx = -1;
        int answer = 0;
        while(cnt < N)
        {
            max = Integer.MIN_VALUE;
            maxIdx = -1;
            for(int i = 0; i < N; ++i)
            {
                ArrayList<Integer> tmp = arrList.get(i);
                if(!tmp.isEmpty() && max < tmp.get(tmp.size()-1)) {
                    max = tmp.get(tmp.size()-1);
                    maxIdx = i;
                }
            }

            answer = arrList.get(maxIdx).remove(arrList.get(maxIdx).size()-1);
            cnt++;
        }

        System.out.println(answer);
    }

}

class FastReader
{
    BufferedReader br;
    StringTokenizer st;

    FastReader()
    {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next()
    {
        while(st == null || !st.hasMoreElements())
        {
            try
            {
                st = new StringTokenizer(br.readLine());
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    String nextLine()
    {
        String str = "";

        try
        {
            str = br.readLine();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        return str;
    }

    int nextInt()
    {
        return Integer.parseInt(next());
    }
}
profile
게임개발자 백엔드개발자

0개의 댓글