Last active
March 25, 2020 04:50
-
-
Save varvir/88a359620ffac6b7e9cabc8592d4f81a to your computer and use it in GitHub Desktop.
[2018 kakao blind 셔틀버스] 기본 spec을 정공법으로 풀었다. 복잡해지는 경우 부분부분으로 나누는 것에 주목한다. #kakao #2018
This file contains hidden or 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
/* | |
bustable, timetable 변환하기는 쉬우니 넘어가자. | |
- 버스 시간보다 빨리 온 사람을 확인해서 넣을 수 있는 인원 m명만큼 넣는다. | |
bussimulation한다. 마지막버스 빼고. | |
- 이러다가 마지막 버스인 걸 확인할 경우 또 뭔가를 해야한다. | |
이걸 하나에 다 담으려고 하지마라. 생각의 한계에 부딫을 경우 함수로 나누자. | |
마지막 버스에서는 버스에 담길 사람이 중요하므로 **버스에 안담길 사람을 모두 제거한다.** | |
m명 확인해서 콘이 어디에 올지 확인해주면 된다. | |
- 저 2가지를 같이 생각하면 머리 아프다... | |
*/ | |
int lastbussimulation(Integer last, LinkedList<Integer> mans, int m){ | |
while (!mans.isEmpty()){ | |
if(last < mans.getFirst()){ | |
mans.removeLast(); | |
} else break; | |
} | |
if(mans.size() >= m){ | |
return mans.get(m-1)-1; | |
} else { | |
return last; | |
} | |
} | |
void bussimulation(List<Integer> buses, LinkedList<Integer> mans, int m) { | |
for (int i=0; i<buses.size(); i++){ | |
int thisbus = buses.get(i); | |
int avail = m; | |
while (!mans.isEmpty()){ | |
if(avail > 0 && thisbus >= mans.getFirst()){ | |
mans.removeFirst(); | |
avail--; | |
} else break; | |
} | |
} | |
} |
This file contains hidden or 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
import java.util.*; | |
import java.util.function.Function; | |
import java.util.stream.Collectors; | |
class Solution { | |
List<Integer> bustable(int n, int t){ | |
List<Integer> tmp = new ArrayList<>(); | |
for(int i=0; i<n; i++){ | |
tmp.add(540+i*t); | |
} | |
return tmp; | |
} | |
public String solution(int n, int t, int m, String[] timetable) { | |
Function<String, Integer> tomin = e->{ | |
String[] hm= e.split(":"); | |
int hour = Integer.parseInt(hm[0]); | |
int minute = Integer.parseInt(hm[1]); | |
int minutes = hour*60+minute; | |
return minutes; | |
}; | |
List<Integer> mins = Arrays.stream(timetable) | |
.map(tomin) | |
.collect(Collectors.toList()); | |
mins.sort(Comparator.naturalOrder()); | |
LinkedList<Integer> mans = new LinkedList<>(mins); | |
List<Integer> buses = bustable(n, t); | |
bussimulation(buses.subList(0, buses.size()-1), mans, m); | |
int intanswer = lastbussimulation(buses.get(buses.size()-1), mans, m); | |
int am = intanswer % 60; | |
int hm = intanswer / 60; | |
String fa = String.format("%02d:%02d", hm, am); | |
return fa; | |
} | |
int lastbussimulation(Integer last, LinkedList<Integer> mans, int m){ | |
while (!mans.isEmpty()){ | |
if(last < mans.getFirst()){ | |
mans.removeLast(); | |
} else break; | |
} | |
if(mans.size() >= m){ | |
return mans.get(m-1)-1; | |
} else { | |
return last; | |
} | |
} | |
void bussimulation(List<Integer> buses, LinkedList<Integer> mans, int m) { | |
for (int i=0; i<buses.size(); i++){ | |
int thisbus = buses.get(i); | |
int avail = m; | |
while (!mans.isEmpty()){ | |
if(avail > 0 && thisbus >= mans.getFirst()){ | |
mans.removeFirst(); | |
avail--; | |
} else break; | |
} | |
} | |
} | |
} |
This file contains hidden or 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
// string을 저렇게 format하면 쉽게 표현이 가능하니, 자주 애용하자. Model, View, Control을 생각하자. | |
int am = intanswer % 60; | |
int hm = intanswer / 60; | |
String fa = String.format("%02d:%02d", hm, am); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment