본문 바로가기
Algorithm/Problem_프로그래머스

프로그래머스(프린터, 124 나라의 숫자, 스킬트리) - Java

by uyoo 2020. 4. 2.

[프린터]

 

* 로직

  • 해당 프린터의 index와 우선순위를 관리하는 클래스를 생성한다
  • 큐에 차례대로 넣어준다
  • 큐를 하나씩 꺼내어
    • 남은 요소 중 우선순위가 더 큰 부분이 존재한다면 해당 큐를 뒤로 다시 넣는다
    • 존재하지 않는다면 해당 정보를 리스트에 넣는다
  • 리스트 중에서 찾고자 하는 index(location)을 찾고 답을 출력한다

 

//프린터
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

class PrinterInfo {
    public int idx;
    public int priority;

    public PrinterInfo(int idx, int priority) {
        this.idx = idx;
        this.priority = priority;
    }
}

public class Problem_Printer {

    public static void main(String[] args) {
        int[] priorities = {2, 1, 3, 2};
        int location = 2;
        int answer = solution(priorities, location);

        System.out.println(answer);
    }

    public static int solution(int[] priorities, int location) {
        int answer = 0;
        Queue<PrinterInfo> list = new LinkedList<>();

        for(int i=0; i<priorities.length; ++i){
            list.offer(new PrinterInfo(i, priorities[i]));
        }

        ArrayList<PrinterInfo> results = new ArrayList<>();
        while (!list.isEmpty()){
            PrinterInfo q = list.poll();

            boolean check = false;
            for(PrinterInfo tmp : list){
                if(q.priority < tmp.priority) {
                    check = true;
                    break;
                }
            }

            if(check) list.offer(q);
            else results.add(q);
        }

        for(int i=0; i<results.size(); ++i){
            if(results.get(i).idx == location){
                answer = i+1;
                break;
            }
        }

        return answer;
    }
}

 

[124 나라의 숫자]

 

* 로직

  • 3으로 나누었을 때 나머지의 인덱스로 124 숫자가 표현되는 것을 볼 수 있다
  • 대신 나머지가 0인 순간엔 4로 표현되기 때문에 배열 형태를 ["4", "1", "2"]로 설정한다
  • n이 0이 될때까지 n/=3으로 계속 갱신을 진행해주고, 만약 나누어 떨어지는 순간엔 n-- 를 진행한다
    (반복문이 한번 더 진행돼서 오류 발생)
  • 사이클을 돌 때마다 124 숫자를 넣어주고 답을 출력한다

 

//124 나라
import java.util.LinkedList;

public class Problem_124Country {

    public static void main(String[] args) {
        int n = 20;
        String answer = solution(n);
        System.out.println(answer);
    }

    public static String solution(int n) {
        String answer = "";
        String[] arr = {"4", "1", "2"};

        StringBuilder sb = new StringBuilder();
        LinkedList<String> results = new LinkedList<>();
        while (n > 0){
            int rest = n % 3;
            n /= 3;
            if(rest == 0) n -= 1;

            results.addFirst(arr[rest]);
        }

        for(String res : results){
            sb.append(res);
        }

        return answer = sb.toString();
    }
}

 

 

[스킬트리]

 

* 로직

  • skill_trees를 하나씩 꺼내고 skill의 단어를 거꾸로 한글자씩 꺼낸다
  • 뽑은 글자가 skill_trees에 존재한다면 base index를 갱신하고 글자를 찾았다는 마킹을 진행한다
  • 그 다음 뽑은 글자 역시 존재한다면
    • 만약 해당 글자의 index > 이전 글자의 index(base index) => 불가능
    • 만약 해당 글자의 index < 이전 글자의 index(base index) => 가능
  • 그 다음 뽑은 글자가 존재하지 않는다면
    • 만약 이전 글자도 마찬가지로 존재하지 않았다면 => 상관없음
    • 만약 이전 글자가 존재했었다면 => 불가능
//스킬트리
public class Problem_SkillTree {

    public static void main(String[] args) {
        String skill = "CBD";
        String[] skill_trees = {"BACDE", "CBADF", "AECB", "BDA"};
        int answer = solution(skill, skill_trees);

        System.out.println(answer);
    }

    public static int solution(String skill, String[] skill_trees) {
        int answer = 0;
        for(String skill_tree : skill_trees){
            int baseIdx = skill_tree.length();
            boolean check = false;
            boolean possible = true;

            for(int s= skill.length()-1; s>=0; --s){
                String tmp = skill.substring(s, s + 1);

                //발견했다면
                if(skill_tree.indexOf(tmp) != -1){

                    //뒤에 문자에서 발견한 index보다 앞에 있다면 가능
                    if(skill_tree.indexOf(tmp) < baseIdx){
                        baseIdx = skill_tree.indexOf(tmp);
                        check = true;
                    }

                    else if(skill_tree.indexOf(tmp) > baseIdx) possible = false;
                }

                //이전엔 발견했는데 현재 문자가 없다면 => 불가능
                else if(skill_tree.indexOf(tmp) == -1 && check) {
                    possible = false;
                }

                if(!possible) break;
            }

            if(possible) answer++;
        }

        return answer;
    }
}