-
99클럽 코테 스터디 7일차 TIL + 과제 진행하기(프로그래머스)Study(진행중)/항해99 2024. 7. 29. 02:01
오늘의 학습 키워드
- 알고리즘
- 구현
- 정렬
- 스택
공부한 내용 본인의 언어로 정리하기
https://school.programmers.co.kr/learn/courses/30/lessons/176962
정렬과 스택을 이용한 구현 알고리즘 문제를 풀었다.구현하는게 많이 까다로웠다.
풀이는 다음과 같다.
import java.util.*; class Solution { class Task { String name; int leftTime; Task(String name, int leftTime) { this.name = name; this.leftTime = leftTime; } } public String[] solution(String[][] plans) { StringBuilder sb = new StringBuilder(); Arrays.sort(plans, (o1, o2) -> o1[1].compareTo(o2[1])); ArrayList<String> results = new ArrayList<>(); ArrayDeque<Task> taskStack = new ArrayDeque<>(); int curTime = timeToInt(plans[0][1]); taskStack.addLast(new Task(plans[0][0], Integer.parseInt(plans[0][2]))); int size = plans.length; for (int i = 1; i < size; i++) { int nextTime = timeToInt(plans[i][1]); Task nextTask = new Task(plans[i][0], Integer.parseInt(plans[i][2])); while (curTime < nextTime) { if (taskStack.isEmpty()) { curTime = nextTime; break; } Task leftTask = taskStack.pollLast(); if (curTime + leftTask.leftTime > nextTime) { int timeDiff = nextTime - curTime; leftTask.leftTime -= timeDiff; taskStack.addLast(leftTask); curTime = nextTime; } else { results.add(leftTask.name); curTime += leftTask.leftTime; } } taskStack.addLast(nextTask); } while (taskStack.size() != 0) { results.add(taskStack.pollLast().name); } String[] answer = results.toArray(String[]::new); return answer; } public int timeToInt(String timeStr) { String[] parse = timeStr.split(":"); return Integer.parseInt(parse[0]) * 60 + Integer.parseInt(parse[1]); } }
내일 학습할 것은 무엇인지
내일은 스터디 문제를 보고 어떤 알고리즘을 할 지 고민해봐야겠다.
반응형'Study(진행중) > 항해99' 카테고리의 다른 글
99클럽 코테 스터디 9일차 TIL + 프로세스와 스레드, 코루틴 (0) 2024.07.31 99클럽 코테 스터디 8일차 TIL + 베스트앨범(프로그래머스) (0) 2024.07.30 99클럽 코테 스터디 6일차 TIL + 테이블 해시 함수(프로그래머스) (0) 2024.07.27 99클럽 코테 스터디 5일차 TIL + 베스트앨범(프로그래머스) (0) 2024.07.27 99클럽 코테 스터디 4일차 TIL + 문자열 압축(프로그래머스) (0) 2024.07.26