코딩테스트 및 알고리즘
멘토와 멘티
영카이브
2023. 12. 20. 11:15
문제. 멘토와 멘티가 짝을 이룬다. M번의 시험으로 등수를 가지고 멘토와 멘티를 정하게 된다.
M번의 모든 시험에서 등수가 앞서야 멘토와 멘티를 이룰 수 있다. 시험 결과는 해당 학생의 학생번호로 주어진다.
입력 예시
4 3
3 4 1 2
4 3 2 1
3 1 4 2
답 : 3
나의 답변
import java.util.Scanner;
public class Main {
public int solution(int n, int m, int[][] arr) {
int answer = 0;
for( int i=1; i<=n; i++ ) { // 멘토 학생 번호
for( int j=1; j<=n; j++) { // 멘티 학생 번호
if( i==j ) continue;
boolean isMentor = true;
for( int k=0; k<m; k++) { // 테스트
int mentorRank=0;
int menteeRank=0;
for( int s=0; s<n; s++) { // 등수
if( arr[k][s]==i ) mentorRank=s;
if( arr[k][s]==j ) menteeRank=s;
}
if( mentorRank >= menteeRank ) {
isMentor=false;
break;
}
}
if(isMentor) answer++;
}
}
return answer;
}
public static void main(String[] args) {
Main T = new Main();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[][] arr = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = sc.nextInt();
}
}
System.out.println(T.solution(n, m, arr));
}
}
또 다른 방법 : mentorRank < menteeRank 일 때 횟수를 세서 총 테스트 횟수와 일치하면 answer++되는 방법도 있다.