일단 시작해보는 블로그

[알고리즘_풀이] 카카오 코딩테스트, 실패율 Java 본문

CS/알고리즘 풀이

[알고리즘_풀이] 카카오 코딩테스트, 실패율 Java

Selina Park 2019. 8. 30. 09:50

 

 

프로그래머스용 코드

import java.util.*;
class Solution {
    static class Node{
        int index;
        double value;
        Node(int index, double value){
            this.index = index;
            this.value = value;
        }

        int printIndex(){
            return this.index;
        }

        double printValue(){
            return this.value;
        }
    }

    public int[] solution(int N, int[] stages) {
        int[] answer = new int[N];
        int[] challenge = new int[N+1];
        int[] numOfFailure = new int[N+1];

        for(int i=0; i<stages.length; i++){
            for(int j=0; j<stages[i]; j++){
                challenge[j] += 1;
            }
            numOfFailure[stages[i]-1] += 1;
        }

        List<Node> tmp = new LinkedList<>();
        List<Integer> sortedArr = new LinkedList<>();

        for(int i=0; i<N; i++){
            tmp.add(new Node(i+1, (double)numOfFailure[i]/(double)challenge[i]));
        }

        for(int i=0; i<tmp.size()-1; i++){
            int maxIndex = i;
            for(int j=i+1; j<tmp.size(); j++){
                if(tmp.get(maxIndex).value < tmp.get(j).value){
                    maxIndex = j;
                }else if(tmp.get(maxIndex).value == tmp.get(j).value){
                    if(tmp.get(maxIndex).index > tmp.get(j).index) {
                        //인덱스비교해서 작은걸 maxIndex에 넣는다.
                        Node swapNode = tmp.get(j);
                        tmp.set(j, tmp.get(maxIndex));
                        tmp.set(maxIndex, swapNode);
                    }
                }
            }
            if(maxIndex != i){
                Node tmpNode = tmp.get(i);
                tmp.set(i, tmp.get(maxIndex));
                tmp.set(maxIndex, tmpNode);
            }
        }

        //tmp에 있는걸 옮겨담기
        for(int i=0; i<answer.length; i++){
            answer[i] = tmp.get(i).index;
        }

        return answer;
    }
}

 

IDE

package codingTest;

import java.util.*;

public class Kakao_failureRate {
    static class Node{
        int index;
        double value;
        Node(int index, double value){
            this.index = index;
            this.value = value;
        }

        int printIndex(){
            return this.index;
        }

        double printValue(){
            return this.value;
        }
    }

    public static int[] solution(int N, int[] stages) {
        int[] answer = new int[N];
        int[] challenge = new int[N+1];
        int[] numOfFailure = new int[N+1];

        for(int i=0; i<stages.length; i++){
            //System.out.println("stages[i] : " + stages[i]);
            for(int j=0; j<stages[i]; j++){
                challenge[j] += 1;
            }
            numOfFailure[stages[i]-1] += 1;
        }

//        for(int ci : challenge){
//            System.out.print(ci + " ");
//        }
//
//        System.out.println();
//
//        for(int ni : numOfFailure){
//            System.out.print(ni + " ");
//        }

        //double[] tmp = new double[N];
        //HashMap<Integer, Double> tmp = new HashMap<>();
        List<Node> tmp = new LinkedList<>();
        List<Integer> sortedArr = new LinkedList<>();

        for(int i=0; i<N; i++){
            tmp.add(new Node(i+1, (double)numOfFailure[i]/(double)challenge[i]));
        }

        for(int i=0; i<tmp.size()-1; i++){
            int maxIndex = i;
            for(int j=i+1; j<tmp.size(); j++){
                if(tmp.get(maxIndex).value < tmp.get(j).value){
                    maxIndex = j;
                }else if(tmp.get(maxIndex).value == tmp.get(j).value){
                    if(tmp.get(maxIndex).index > tmp.get(j).index) {
                        //인덱스비교해서 작은걸 maxIndex에 넣는다.
                        Node swapNode = tmp.get(j);
                        tmp.set(j, tmp.get(maxIndex));
                        tmp.set(maxIndex, swapNode);
                    }
                }
            }
            if(maxIndex != i){
                Node tmpNode = tmp.get(i);
                tmp.set(i, tmp.get(maxIndex));
                tmp.set(maxIndex, tmpNode);
            }
        }


        //tmp에 있는걸 옮겨담기
        for(int i=0; i<answer.length; i++){
            answer[i] = tmp.get(i).index;
        }

        return answer;
    }

    public static void main(String[] args) {
        int[] stages = {4,4,4,4,4};
        int[] answer = solution(4, stages);

        for(int ai : answer){
            System.out.print(ai + " ");
        }

    }
}
Comments