본문 바로가기
알고리즘

[프로그래머스] 과제 진행하기 Java 풀이

by 내기록 2023. 5. 27.
반응형

한줄 요약 : HH:mm  포맷이 나오면 '시간*60+분'을 사용한다. stack사용

 

 

첫 번째 풀이)

이 풀이로 통과 했으나, 다른 사람들은 시간 다루는 부분을 어떻게 했는지 확인해봤다.

나는 12:20 과 같이 시간으로 들어오는 값을 HH:mm 타입의 dateformat으로 계산했는데, 다른사람들은 '시간*60+분' 으로 minutes로 변경해서 start를 저장해서 사용하는 것을 확인했다.

나중에 문제 풀 때 도움이 많이 될 것 같아서 이 방법으로 변경하기로 결정

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

public class Main {
    public static void main(String[] args) {
        String[][] array = {{"science", "12:40", "50"},
                {"music", "12:20", "40"},
                {"history", "14:00", "30"},
                {"computer", "12:30", "100"}};
        String[] res = solution(array);

        for (String r : res) {
            System.out.print(r);
        }
    }

    public static String[] solution(String[][] plans) {

        ArrayList<String> answer = new ArrayList<>();

        try {
            SimpleDateFormat format = new SimpleDateFormat("HH:mm");
            List<Node> plan_list = new ArrayList<>();

            for (String[] p : plans) {
                plan_list.add(new Node(p[0], format.parse(p[1]), Integer.parseInt(p[2])));
            }

            Comparator<Node> comparator = (n1, n2) -> Long.valueOf(n1.start.getTime()).compareTo(n2.start.getTime());
            Collections.sort(plan_list, comparator);

            Stack<Node> stack = new Stack<>();


            for (int i = 0; i < plan_list.size() - 1; i++) {
                Node node = plan_list.get(i);
                Date t = node.start;
                Date t2 = plan_list.get(i + 1).start;

                long diff = TimeUnit.MILLISECONDS.toMinutes(t2.getTime() - t.getTime());

                if (diff < node.playtime) {
                    stack.push(new Node(node.name, node.playtime - diff));
                } else if (diff > node.playtime) {
                    answer.add(node.name);
                    diff = diff - node.playtime;

                    while (diff > 0 && !stack.empty()) {
                        Node tmp = stack.pop();
                        if (diff < tmp.playtime) {
                            stack.push(new Node(tmp.name, tmp.playtime - diff));
                            diff = 0;
                        } else if (diff >= tmp.playtime) {
                            answer.add(tmp.name);
                            diff = diff - tmp.playtime;
                        }
                    }
                } else {
                    answer.add(node.name);
                }
            }
            answer.add(plan_list.get(plan_list.size() - 1).name);

            while(!stack.empty()){
                Node n = stack.pop();
                answer.add(n.name);
            }


        } catch (Exception e) {
            System.out.print(e);
        }
        return answer.toArray(new String[answer.size()]);
    }
}

class Node {
    String name;
    Date start;
    long playtime;

    Node(String name, Date start, long playtime) {
        this.name = name;
        this.start = start;
        this.playtime = playtime;
    }

    Node(String name, long playtime) {
        this.name = name;
        this.playtime = playtime;
    }
}

 

두 번째 풀이) 

시간*60+분 적용

import java.util.*;

public class Main {
    public static void main(String[] args) {
        String[][] array = {{"science", "12:40", "50"},
                {"music", "12:20", "40"},
                {"history", "14:00", "30"},
                {"computer", "12:30", "100"}};
        String[] res = solution(array);

        for (String r : res) {
            System.out.print(r);
        }
    }

    public static String[] solution(String[][] plans) {

        ArrayList<String> answer = new ArrayList<>();

        try {
            List<Node> plan_list = new ArrayList<>();

            for (String[] p : plans) {
                String[] time = p[1].split(":");

                plan_list.add(new Node(p[0], (Integer.parseInt(time[0])*60)+Integer.parseInt(time[1]),
                        Integer.parseInt(p[2])));
            }

            plan_list.sort((h1, h2) -> h1.start - h2.start);

            Stack<Node> stack = new Stack<>();

            for (int i = 0; i < plan_list.size() - 1; i++) {
                Node node = plan_list.get(i);
                long t = node.start;
                long t2 = plan_list.get(i + 1).start;

                long diff = t2-t;

                if (diff < node.playtime) {
                    stack.push(new Node(node.name, node.playtime - diff));
                } else if (diff > node.playtime) {
                    answer.add(node.name);
                    diff = diff - node.playtime;

                    while (diff > 0 && !stack.empty()) {
                        Node tmp = stack.pop();
                        if (diff < tmp.playtime) {
                            stack.push(new Node(tmp.name, tmp.playtime - diff));
                            diff = 0;
                        } else if (diff >= tmp.playtime) {
                            answer.add(tmp.name);
                            diff = diff - tmp.playtime;
                        }
                    }
                } else {
                    answer.add(node.name);
                }
            }
            answer.add(plan_list.get(plan_list.size() - 1).name);

            while(!stack.empty()){
                Node n = stack.pop();
                answer.add(n.name);
            }


        } catch (Exception e) {
            System.out.print(e);
        }
        return answer.toArray(new String[answer.size()]);
    }
}

class Node {
    String name;
    int start;
    long playtime;

    Node(String name, int start, long playtime) {
        this.name = name;
        this.start = start;
        this.playtime = playtime;
    }

    Node(String name, long playtime) {
        this.name = name;
        this.playtime = playtime;
    }
}
반응형

댓글