λ¬Έμ
μ νλ²μ€ λ¬Έμ λ°λ‘κ°κΈ°
νμ΄
- μ νμ΄ μΆλ°νλ μκ°μ λ€ κ΅¬νλ€.
- μ¬λλ€μ timetableμ μ€λ¦μ°¨μμΌλ‘ μ λ ¬νλ€.
- κ° μ
νμ ν μ μλ μ¬λ(μ
ν μκ°λ³΄λ€ μκ±°λ κ°μ μ¬λ)μ ꡬνλ€.
- μ΄ λ, μ½μ΄ ν΄λΉ μ νμ ν μ μκΈ° μν μ΅νμ μκ°μ ꡬν΄λλλ€.
- μ΅νμ μκ°μ μ νμ μ¬λμ΄ κ½ μ°° κ²½μ° 'κ°μ₯ ν° μκ° - 1'
- μ νμ μ¬λμ΄ κ½ μ°¨μ§ μλλ€λ©΄ 'μ ν μΆλ° μκ°'μ΄λ€.
- κ° μ νμμμ μ΅ν μκ° μ€ κ°μ₯ μ΅νμΈ μκ°μ κ³ λ₯Έλ€.
μ½λ
#include <string>
#include <vector>
#include <set>
#include <iostream>
#include <algorithm>
using namespace std;
int getTime(string timeString){
auto idx = timeString.find(":");
int hour = stoi(timeString.substr(0, idx));
int minute = stoi(timeString.substr(idx+1, timeString.size()));
return hour*60 + minute;
}
string getTimeString(int time){
string hour = to_string(time / 60);
string minute = to_string(time % 60);
if(hour.size() == 1){
hour = '0' + hour;
}
if(minute.size() == 1){
minute = '0' + minute;
}
return hour + ":" + minute;
}
string solution(int n, int t, int m, vector<string> timetable) {
string answer = "";
vector<int> shuttleTime;
// 1. μ
νμ΄ μΆλ°νλ μκ°μ λ€ κ΅¬νλ€.
int nowTime = 9*60; // 첫 μκ° μΈν
shuttleTime.push_back(nowTime);
for(int i=0; i<n-1; i++){
nowTime += t;
shuttleTime.push_back(nowTime);
}
// 2. μ¬λλ€μ timetableμ μ€λ¦μ°¨μμΌλ‘ μ λ ¬νλ€.
vector<int> numberTimeTable;
for(int i=0; i<timetable.size(); i++){
numberTimeTable.push_back(getTime(timetable[i]));
}
sort(numberTimeTable.begin(), numberTimeTable.end());
// 3. κ° μ
νμ ν μ μλ μ¬λ(μ
ν μκ°λ³΄λ€ μκ±°λ κ°μ μ¬λ)μ ꡬνλ€.
// (μ΄ λ, μ½μ΄ ν΄λΉ μ
νμ ν μ μκΈ° μν μ΅νμ μκ°μ ꡬν΄λλλ€.)
// => μ΄κ±΄ 'κ°μ₯ ν° μκ° - 1λΆ'μ΄κ±°λ, μ
νμ μ¬λμ΄ κ½ μ°¨μ§ μλλ€λ©΄ 'μ
ν μΆλ° μκ°'μ΄λ€.
vector<int> cornTime;
int idx = 0;
for(int i=0; i<shuttleTime.size(); i++){ // 10
int shuttle = shuttleTime[i];
int shuttleWhole = 0;
int maxTime = 0;
// *μ΄λ―Έ λ²μ€μ ν μΈμμ μ μΈν΄μ£Όμ΄μΌ ν¨
// *μ
νμ mλͺ
μ΄μ λͺ» νκ² ν΄μΌ ν¨
for(; idx<numberTimeTable.size() && shuttleWhole != m; idx++){ // 2000
if(numberTimeTable[idx] <= shuttle){
shuttleWhole++;
if(maxTime < numberTimeTable[idx]) maxTime = numberTimeTable[idx];
}else{
break;
}
}
if(shuttleWhole < m){ // μ
ν μ리 λ¨μ
cornTime.push_back(shuttle);
}else{ // μ리 μμΌλ©΄ κ°μ₯ ν° μκ° - 1
cornTime.push_back(maxTime-1);
}
}
// 4. κ° μ
νμμμ μ΅ν μκ° μ€ κ°μ₯ μ΅ν μκ°μ κ³ λ₯Έλ€.
sort(cornTime.begin(), cornTime.end());
answer = getTimeString(cornTime.back());
return answer;
}
κ²°κ³Ό
ν μ€νΈ 1 γ | ν΅κ³Ό (0.02ms, 4.32MB) |
ν μ€νΈ 2 γ | ν΅κ³Ό (0.02ms, 4.25MB) |
ν μ€νΈ 3 γ | ν΅κ³Ό (0.02ms, 4.26MB) |
ν μ€νΈ 4 γ | ν΅κ³Ό (0.02ms, 3.78MB) |
ν μ€νΈ 5 γ | ν΅κ³Ό (0.02ms, 3.8MB) |
ν μ€νΈ 6 γ | ν΅κ³Ό (0.02ms, 4.21MB) |
ν μ€νΈ 7 γ | ν΅κ³Ό (0.08ms, 4.32MB) |
ν μ€νΈ 8 γ | ν΅κ³Ό (0.03ms, 4.26MB) |
ν μ€νΈ 9 γ | ν΅κ³Ό (0.02ms, 4.25MB) |
ν μ€νΈ 10 γ | ν΅κ³Ό (0.02ms, 4.27MB) |
ν μ€νΈ 11 γ | ν΅κ³Ό (0.02ms, 4.32MB) |
ν μ€νΈ 12 γ | ν΅κ³Ό (0.06ms, 4.32MB) |
ν μ€νΈ 13 γ | ν΅κ³Ό (0.06ms, 4.33MB) |
ν μ€νΈ 14 γ | ν΅κ³Ό (0.02ms, 4.26MB) |
ν μ€νΈ 15 γ | ν΅κ³Ό (0.03ms, 3.72MB) |
ν μ€νΈ 16 γ | ν΅κ³Ό (0.04ms, 4.32MB) |
ν μ€νΈ 17 γ | ν΅κ³Ό (0.06ms, 4.25MB) |
ν μ€νΈ 18 γ | ν΅κ³Ό (0.06ms, 4.27MB) |
ν μ€νΈ 19 γ | ν΅κ³Ό (0.06ms, 4.27MB) |
ν μ€νΈ 20 γ | ν΅κ³Ό (0.06ms, 4.33MB) |
ν μ€νΈ 21 γ | ν΅κ³Ό (0.20ms, 4.25MB) |
ν μ€νΈ 22 γ | ν΅κ³Ό (0.08ms, 3.92MB) |
ν μ€νΈ 23 γ | ν΅κ³Ό (0.06ms, 4.34MB) |
ν μ€νΈ 24 γ | ν΅κ³Ό (0.23ms, 3.9MB) |
λΆμ νν μ λ³΄κ° λ΄κ²¨μμ μ μμ΅λλ€!
λκΈλ‘ μλ €μ£ΌμΈμ :)
λμμ΄ λμ ¨λ€λ©΄ κ΄κ³ ν λ²μ© λλ¬μ£ΌμΈμ β£οΈ
λ°μν
Comment