Last active
April 13, 2018 09:18
-
-
Save jacknie84/54fb9e41c8e48a768227d877811d5e28 to your computer and use it in GitHub Desktop.
카카오신입공채 코딩테스트 1차(JAVA)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.jacknie.doodle.kakao; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.Queue; | |
import java.util.StringTokenizer; | |
import java.util.concurrent.LinkedBlockingQueue; | |
import java.util.stream.Collectors; | |
public class KakaoTest4 { | |
public static void main(String[] args) { | |
System.out.println(getBusTime(1, 1, 5, new String[] {"08:00", "08:01", "08:02", "08:03"})); | |
System.out.println(getBusTime(2, 10, 2, new String[] {"09:10", "09:09", "08:00"})); | |
System.out.println(getBusTime(2, 1, 2, new String[] {"09:00", "09:00", "09:00", "09:00"})); | |
System.out.println(getBusTime(1, 1, 5, new String[] {"00:01", "00:01", "00:01", "00:01", "00:01"})); | |
System.out.println(getBusTime(1, 1, 1, new String[] {"23:59"})); | |
System.out.println(getBusTime(10, 60, 45, new String[] {"23:59","23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59"})); | |
} | |
static Time getBusTime(int n, int t, int m, String[] timetable) { | |
List<Crew> todayCrewList = Arrays.stream(timetable) | |
.map(Time::parse) | |
.sorted() | |
.map(Crew::new).collect(Collectors.toList()); | |
ShuttleBus shuttleBus = new ShuttleBus(n, t, m); | |
Queue<Crew> waitingLine = new LinkedBlockingQueue<>(); | |
while (shuttleBus.isEnableService()) { | |
Time busTime = shuttleBus.getBusTime(); | |
todayCrewList.stream() | |
.filter(crew -> !crew.isLeft() && busTime.compareTo(crew.getWaitTime()) >= 0) | |
.forEach(waitingLine::add); | |
shuttleBus.pickUp(waitingLine); | |
} | |
if (shuttleBus.isFull()) { | |
Crew lastCrew = todayCrewList.get(todayCrewList.size() - 1); | |
Time lastCrewWaitTime = lastCrew.getWaitTime(); | |
lastCrewWaitTime.minusMinutes(1); | |
return lastCrewWaitTime; | |
} | |
else { | |
Time busTime = shuttleBus.getBusTime(); | |
busTime.minusMinutes(t); | |
return busTime; | |
} | |
} | |
static class ShuttleBus { | |
private final int n; | |
private final int t; | |
private final int m; | |
private final Time closeTime = Time.parse("23:59"); | |
private Time busTime = Time.parse("09:00"); | |
private int service; | |
private List<Crew> crewList = new ArrayList<>(); | |
public ShuttleBus(int n, int t, int m) { | |
this.n = n; | |
this.t = t; | |
this.m = m; | |
this.service = 0; | |
} | |
public void pickUp(Queue<Crew> waitingLine) { | |
crewList.clear(); | |
for (int i = 0; i < m; i++) { | |
Crew crew = waitingLine.poll(); | |
if (crew != null) { | |
crew.takeBus(); | |
crewList.add(crew); | |
} | |
} | |
busTime.plusMinutes(t); | |
service++; | |
} | |
public boolean isEnableService() { | |
return n > service && busTime.compareTo(closeTime) <= 0; | |
} | |
public boolean isFull() { | |
return crewList.size() >= m; | |
} | |
public Time getBusTime() { | |
return Time.of(busTime); | |
} | |
@Override | |
public String toString() { | |
return "ShuttleBus [n=" + n + ", t=" + t + ", m=" + m + ", closeTime=" + closeTime + ", busTime=" + busTime | |
+ ", service=" + service + ", crewList=" + crewList + "]"; | |
} | |
} | |
static class Crew { | |
private final Time waitTime; | |
private boolean left; | |
public Crew(Time waitTime) { | |
this.waitTime = waitTime; | |
} | |
public Time getWaitTime() { | |
return Time.of(waitTime); | |
} | |
public void takeBus() { | |
this.left = true; | |
} | |
public boolean isLeft() { | |
return left; | |
} | |
@Override | |
public String toString() { | |
return waitTime.toString(); | |
} | |
} | |
static class Time implements Comparable<Time> { | |
private int hours; | |
private int minutes; | |
public static final Time of(Time other) { | |
return new Time(other.hours, other.minutes); | |
} | |
public static final Time parse(String expression) { | |
StringTokenizer tokenizer = new StringTokenizer(expression, ":"); | |
String hours = tokenizer.nextToken(); | |
String minutes = tokenizer.nextToken(); | |
return new Time(Integer.parseInt(hours), Integer.parseInt(minutes)); | |
} | |
private Time(int hours, int minutes) { | |
this.hours = hours; | |
this.minutes = minutes; | |
} | |
public void plusMinutes(int t) { | |
int mins = t + this.minutes; | |
this.hours += mins / 60; | |
this.minutes = mins % 60; | |
if (this.hours >= 24) { | |
throw new IllegalStateException("하루가 지났습니다."); | |
} | |
} | |
public void minusMinutes(int t) { | |
this.minutes -= t; | |
if (this.minutes < 0) { | |
int mins = this.minutes * -1; | |
int remainder = mins % 60; | |
this.hours -= mins / 60 + (remainder > 0 ? 1 : 0); | |
this.minutes = remainder > 0 ? 60 - remainder : remainder; | |
} | |
if (this.hours < 0) { | |
throw new IllegalStateException("하루 전입니다."); | |
} | |
} | |
@Override | |
public String toString() { | |
return String.format("%02d:%02d", hours, minutes); | |
} | |
@Override | |
public int compareTo(Time other) { | |
if (other == null) { | |
throw new NullPointerException("other 객체가 null 입니다."); | |
} | |
if (this.hours < other.hours) { | |
return -1; | |
} | |
if (this.hours > other.hours) { | |
return 1; | |
} | |
if (this.minutes < other.minutes) { | |
return -1; | |
} | |
if (this.minutes > other.minutes) { | |
return 1; | |
} | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment