[Programmers] 실패율 - 2019 KAKAO BLIND RECRUITMENT

동민·2021년 3월 10일
0
import java.util.ArrayList;

// 실패율 - 2019 KAKAO BLIND RECRUITMENT
public class FailureRate {
	public int[] solution(int N, int[] stages) {

		ArrayList<Double> failureList = new ArrayList<>(); // 실패율을 저장할 리스트
		ArrayList<Integer> rankList = new ArrayList<>(); // 순위를 저장할 리스트

		int size = stages.length;
		int count;
		for (int i = 1; i <= N; i++) {
			count = 0;
			for (int j = 0; j < stages.length; j++) {

				if (stages[j] > 0 && stages[j] <= i) {
					count++;
					stages[j] = -1; // 조건에 만족한 element를 -1 로 치환 (다음 계산에서 중복처리되는 것을 막음)
				}
			}
			if (size == 0) { // (NaN 처리) 0 으로 나누는 예외를 처리해주어야 한다.
				failureList.add(0.0); // size가 0, 즉 스테이지에 도달한 유저가 없는 경우 실패율을 0으로 정의
			} else {
				failureList.add((double) count / size); // 실패율을 저장
			}
			size -= count;
		}

		for (int i = 0; i < failureList.size(); i++) {
			int max = 0;
			for (int j = 0; j < failureList.size(); j++) {
				if (failureList.get(max) < failureList.get(j)) {
					max = j;
				}

			}
			rankList.add(max + 1);
			failureList.set(max, -1.0); // 실패율이 같을 경우 중복처리를 방지하기 위해 -1 로 치환
		}

		return rankList.stream().mapToInt(i -> i.intValue()).toArray();

	}

	public static void main(String[] args) {
		FailureRate s = new FailureRate();

		int[] arr1 = { 2, 1, 2, 4, 2, 4, 3, 3 };
		int[] arr2 = { 4, 4, 4, 4, 4 };

		for (int i = 0; i < s.solution(5, arr1).length; i++) {
			System.out.print(s.solution(5, arr1)[i] + " ");
		}
		System.out.println();
		for (int i = 0; i < s.solution(4, arr2).length; i++) {
			System.out.print(s.solution(4, arr2)[i] + " ");
		}

	}
}
  • (NaN 처리) 0 으로 나누는 예외를 처리해주어야 한다.
profile
BE Developer

0개의 댓글

관련 채용 정보