프로그래머스 위클리 챌린지 6주차
2021. 9. 8. 10:31ㆍCodingTest
문제
https://programmers.co.kr/learn/courses/30/lessons/85002
문제요약
1. 복서들의 승률, 자기보다 무거운 선수를 이긴 횟수, 몸무게, 번호 정보를 활용하여 복서들을 정렬합니다.
2. 정렬의 기준은 아래와 같습니다.
- 승률이 높은 사람
- 자기보다 무거운 선수를 이긴 횟수가 많은 사람
- 몸무게가 더 무거운 사람
- 선수 번호가 작은 사람
3. 아직 다른 복서랑 붙어본적이 없는 복서의 승률은 0%로 취급합니다.
문제풀이
1. 선수들간의 승률 계산
한 선수의 승률 = (승리횟수*1000.0) / 싸운 횟수
(단, 아직 선수들간에 붙어본적(N)이 없는 경우에는 싸운 횟수에 들어가지 않습니다.)
2. 자기보다 무거운 선수를 이긴 횟수 계산
한 선수의 전적을 참조하여 승리한 경기에서 상대 선수와의 몸무게를 비교하여 상대선수의 몸무게가 더 큰 경우 카운트합니다.
3. 자바언어의 경우 선수 클래스에 대한 정렬 기준을 생성하기 위하여 Comparator 인터페이스를 사용하였습니다. 위의 정렬기준을 참조하여 복서들을 정렬합니다.
소스코드
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Solution{
static class Player{
int num;
String record;
double win_rate;
int win_cnt_heaviy; // 자기보다 무거운 복서를 이긴 횟수
int weight;
public Player(int num, String record, double win_rate, int win_cnt_heaviy, int weight) {
this.num = num;
this.record = record;
this.win_rate = win_rate;
this.win_cnt_heaviy = win_cnt_heaviy;
this.weight = weight;
}
@Override
public String toString() {
return "Player [num=" + num + ", record=" + record + ", win_rate=" + win_rate + ", win_cnt_heaviy="
+ win_cnt_heaviy + ", weight=" + weight + "]";
}
}
public static double get_winRate(String record)
{
int win = 0;
String record_ = record.replace("N", "");
for(int i=0;i<record_.length();i++)
{
if(record_.charAt(i)=='W')
{
win++;
}
}
// 설명 역량 부족
return (record_.length()==0 ? 0.0 : win*1000.0/record_.length());
}
public static int get_winCntHeaviy(int num, int[] weights, String record)
{
int win_count = 0;
for(int i=0;i<record.length();i++)
{
if(record.charAt(i)=='W')
{
if(weights[num]<weights[i])
{
win_count++;
}
}
}
return win_count;
}
public static int[] solution(int[] weights, String[] head2head) {
int[] answer = {};
List<Integer> answer_list = new ArrayList<Integer>();
List<Player> players = new ArrayList<>();
for(int i=0;i<weights.length;i++)
{
players.add(new Player(i+1, head2head[i], get_winRate(head2head[i]), get_winCntHeaviy(i, weights, head2head[i]), weights[i]));
}
Comparator<Player> compara_winRate_desc = new Comparator<Player>() {
@Override
public int compare(Player p1, Player p2) {
return Double.compare(p2.win_rate,p1.win_rate);
}
};
Comparator<Player> compara_winCntHeaviy_desc = new Comparator<Player>() {
@Override
public int compare(Player p1, Player p2) {
return Integer.compare(p2.win_cnt_heaviy,p1.win_cnt_heaviy);
}
};
Comparator<Player> compara_weight_desc = new Comparator<Player>() {
@Override
public int compare(Player p1, Player p2) {
return Integer.compare(p2.weight,p1.weight);
}
};
Comparator<Player> compara_num_asc = new Comparator<Player>() {
@Override
public int compare(Player p1, Player p2) {
return Integer.compare(p1.num, p2.num);
}
};
Collections.sort(players,
compara_winRate_desc
.thenComparing(compara_winCntHeaviy_desc)
.thenComparing(compara_weight_desc)
.thenComparing(compara_num_asc));
players.forEach(p -> answer_list.add(p.num));
answer = answer_list.stream().mapToInt(i->i).toArray();
return answer;
}
public static void main(String args[])
{
int[] weights = {50,82,75,120};
String[] head2head = {"NLWL","WNLL","LWNW","WWLN"};
System.out.println(Arrays.toString(solution(weights, head2head)));
}
}
'CodingTest' 카테고리의 다른 글
백준 2193, 이천수 (0) | 2021.09.13 |
---|---|
백준 11653, 소인수분해 (0) | 2021.09.08 |
백준 11726, 2 x n 타일링 (0) | 2021.09.06 |
프로그래머스 위클리 챌린지 4주차 (0) | 2021.09.02 |
프로그래머스 위클리 챌린지 2주차 (0) | 2021.09.01 |