정렬 실패율

jaegeunsong97·2023년 3월 7일
0
post-thumbnail



어떻게 푸는 가를 생각했을 때는 근접했다.

지금 내가 못하는 것은 구현인 것 같다. 구현은 꾸준히 하면 향상되기 때문에 별 신경 안씀 ㅎㅅㅎ

문제 접근을 할 때 가장 먼저 실패율을 구하고 비교의 기준이 다르기 때문에 새로운 객체를 만들어서 정렬 기준을 만들어줘야 한다고 생각했다.

  1. 실패율을 계산 후
  2. 어떤 객체에 넣고
  3. 객체가 Comparable를 implements 해야한다.

여기까지 생각은 했지만,, 구현 ㅠㅠ

import java.util.*;

class Node implements Comparable<Node> {
	private int stage;
    private double fail;
    
    public Node(int stage, double fail) {
    	this.stage = stage;
        this.fail = fail;
    }
    
    public int getStage() {
    	return this.stage;
    }
    
    @Override
    public int compareTo(Node other) {
    	if (this.fail == other.fail) return Integer.compare(this.stage, other.stage);
        return Double.compare(other.fail, this.fail);
    }
}

class Solution {
	public int[] solution(int N, int[] stages) {
    	int[] answer = new int[N];
        ArrayList<Node> list = new ArrayList<>();
        int length = stages.length;
        
        for (int i = 1; i <= N; i++) {
        	int levelPass = 0;
            
            for (int j = 0; j < stages.length; j++) {
            	if (i == stages[j]) levelPass += 1;
            }
            
            double fail = 0;
            if (1 <= length) fail = (double) levelPass / length;
            
            list.add(new Node(i, fail));
            length -= levelPass;
        }
        
        Collectinos.sort(list);
        
        for (int i = 0; i < N; i++) answer[i] = list.get(i).getStage();
        return answer;
    }
}
profile
현재 블로그 : https://jasonsong97.tistory.com/

0개의 댓글