Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21.06.26 - [PG] 실패율 #96

Closed
suhyunsim opened this issue Jun 26, 2021 · 0 comments
Closed

21.06.26 - [PG] 실패율 #96

suhyunsim opened this issue Jun 26, 2021 · 0 comments
Assignees
Labels
lv.1 프로그래머스 - level 1 성공 맞은 문제 정렬 정렬하기

Comments

@suhyunsim
Copy link
Owner

문제

핵심 아이디어

어려운 점, 실수

  • 도전자가 0명인 경우를 놓치지 말기
  • Map에서 정렬을 활용하는 법 확인

풀이

package main.java.com.poogle.PG.Q42889;

import java.util.*;

public class Solution {
    public static int[] solution(int N, int[] stages) {
        int[] trials = new int[N + 1];
        for (int stage : stages) {
            for (int j = 0; j < stage; j++) {
                trials[j]++;
            }
        }
        Map<Integer, Double> rates = new HashMap<>();
        for (int i = 0; i < N; i++) {
            if (trials[i] != 0) {
                rates.put(i + 1, ((trials[i] - trials[i + 1]) / (double) trials[i]));
            } else {
                //나누는 수가 0일 때는 0.0으로 넣어줘야 함
                rates.put(i + 1, 0.0);
            }
//            System.out.println("실패한 사람: " + (trials[i] - trials[i + 1]));
//            System.out.println("총 도전자: " + (double) trials[i]);
//            System.out.println("value: " + (trials[i] - trials[i + 1]) / (double) trials[i]);
        }
        List<Integer> keySet = new ArrayList<>(rates.keySet());
        //value값으로 key 정렬
        keySet.sort((val1, val2) -> rates.get(val2).compareTo(rates.get(val1)));
        return keySet.stream().mapToInt(i -> i).toArray();
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(solution(5, new int[]{2, 1, 2, 4, 2, 4, 3, 3})));
    }
}
@suhyunsim suhyunsim added 성공 맞은 문제 정렬 정렬하기 lv.1 프로그래머스 - level 1 labels Jun 26, 2021
@suhyunsim suhyunsim added this to the 6월 4주 차 milestone Jun 26, 2021
@suhyunsim suhyunsim self-assigned this Jun 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lv.1 프로그래머스 - level 1 성공 맞은 문제 정렬 정렬하기
Projects
None yet
Development

No branches or pull requests

1 participant