이번 문제는 구현 문제인데 머리에 있는 로직을 무작정 코드로 짜다 보니 개인적으로는 휴율적으로 풀었다는 생각이 들지 않는다.
더 좋은 풀이가 있다면 댓글로 달아주시면 감사하겠습니다,, : )
# Process
input
: 등수 N 과 N만큼의 팀이 주어진다. 이를 기반으로 배열을 생성해주고, 팀의 범위가 1-200 이니까 201의 team 배열도 추가로 생성해준다. (인원수 확인 및 4위까지 점수 측정을 위해 사용)
6명이 모두 있는 팀의 순위를 비교하는 것이기 때문에 순위를 기록할 majorRank 라는 HashMap 도 선언해준다.
static int N, max;
static int[] score, team;
static ArrayList<Integer>[] major;
static HashMap<Integer,Integer> majorRank;
static void input()
{
N = scan.nextInt();
score = new int[N+1]; // 랭크입력
team = new int[201]; // 1 - 200
major = new ArrayList[201];
majorRank = new HashMap<>();
for(int i=1; i<=N; i++)
{
score[i] = scan.nextInt();
team[score[i]]++; // 인원수 체크
// 6명이 모두 있다면 비교할 팀에 등록
if(team[score[i]] == 6)
{
// index 는 팀번호
major[score[i]] = new ArrayList<>();
majorRank.put(score[i], 0);
team[score[i]] = 0; // 초기화
}
}
}
구현
1. N 까지 순회하며 점수를 측정해준다.
2. 측정한 점수를 기반으로 MAX 값을 잡아준다.
- 점수가 같을 경우 5번째 순위의 점수가 낮은 팀을 선정한다.
static void pro()
{
int index = 1;
// 1. 각 팀에 등수별로 점수 반환
for(int i=1; i<=N; i++)
{
if(!majorRank.containsKey(score[i])) continue;
// 4위까지 점수 측정
if(team[score[i]] < 4)
{
majorRank.put(score[i], majorRank.get(score[i]) + index);
}
major[score[i]].add(index);
team[score[i]]++;
index++;
}
// 2. SET MAX
int min = Integer.MAX_VALUE;
int minTeam = 201;
for(int k : majorRank.keySet())
{
int value = majorRank.get(k);
if(min > value)
{
min = value;
minTeam = k;
}
else if(min == value)
{
if(major[minTeam].get(4) > major[k].get(4))
{
// 점수는 동일, team 만 스위칭
minTeam = k;
}
}
}
sb.append(minTeam+"\n");
}
전체 코드
: 위와 같은 로직을 통해 답을 도출할 수 있다.
static int N, max;
static int[] score, team;
static ArrayList<Integer>[] major;
static HashMap<Integer,Integer> majorRank;
static void input()
{
N = scan.nextInt();
score = new int[N+1]; // 랭크입력
team = new int[201]; // 1 - 200
major = new ArrayList[201];
majorRank = new HashMap<>();
for(int i=1; i<=N; i++)
{
score[i] = scan.nextInt();
team[score[i]]++; // 인원수 체크
// 6명이 모두 있다면 비교할 팀에 등록
if(team[score[i]] == 6)
{
// index 는 팀번호
major[score[i]] = new ArrayList<>();
majorRank.put(score[i], 0);
team[score[i]] = 0; // 초기화
}
}
}
static void pro()
{
int index = 1;
// 각 팀에 등수별로 점수 반환
for(int i=1; i<=N; i++)
{
if(!majorRank.containsKey(score[i])) continue;
// 4위까지 점수 측정
if(team[score[i]] < 4)
{
majorRank.put(score[i], majorRank.get(score[i]) + index);
}
major[score[i]].add(index);
team[score[i]]++;
index++;
}
int min = Integer.MAX_VALUE;
int minTeam = 201;
for(int k : majorRank.keySet())
{
int value = majorRank.get(k);
if(min > value)
{
min = value;
minTeam = k;
}
else if(min == value)
{
if(major[minTeam].get(4) > major[k].get(4))
{
// 점수는 동일, team 만 스위칭
minTeam = k;
}
}
}
sb.append(minTeam+"\n");
}
public static void main(String[] args)
{
int T = scan.nextInt();
while(T--> 0)
{
input();
pro();
}
System.out.println(sb);
}