코딩테스트연습/이것이 취업을 위한 코딩테스트다

[이코테 - JAVA] 숫자 카드 게임

:)jun 2021. 11. 24. 12:24
//프로덕션 코드

public class NumberCardGame {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();

        int[][] cardArray = new int[n][m];
        for (int i = 0; i < cardArray.length; i++) {
            for (int j = 0; j < cardArray[0].length; j++) {
                cardArray[i][j] = sc.nextInt();
            }
        }

        int theHighestNumber = 0;
        for(int i=0; i<n; i++) {
            theHighestNumber = Math.max(theHighestNumber, getTheLowestNumberInARow(i, cardArray));
        }

        System.out.println(theHighestNumber);
    }
    private static int getTheLowestNumberInARow(int i, int[][] cardArray){
        int theLowestNumberInARow = 100;
        for(int j=0; j<cardArray[i].length; j++){
            theLowestNumberInARow = Math.min(theLowestNumberInARow, cardArray[i][j]);
        }
        return theLowestNumberInARow;
    }

}

// 성공! 리팩토링 해보자!
// getTheLowestNumberInARow에서 한 행만 가져갈 수 있을 것같은데?

//프로덕션 코드
import java.util.Scanner;

public class NumberCardGame {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();

        int[][] cardArray = new int[n][m];
        for (int i = 0; i < cardArray.length; i++) {
            for (int j = 0; j < cardArray[0].length; j++) {
                cardArray[i][j] = sc.nextInt();
            }
        }

        int theHighestNumber = 0;
        for(int i=0; i<n; i++) {
            theHighestNumber = Math.max(theHighestNumber, getTheLowestNumberInARow(cardArray[i]));
        }

        System.out.println(theHighestNumber);
    }
    
    private static int getTheLowestNumberInARow(int[] oneRowOfCardArray){
        int theLowestNumberInARow = 100;
        for(int i=0; i<oneRowOfCardArray.length; i++){
            theLowestNumberInARow = Math.min(theLowestNumberInARow, oneRowOfCardArray[i]);
        }
        return theLowestNumberInARow;
    }
}

// 굿!