[ํ๋ก๊ทธ๋๋จธ์ค Lv3] : [1์ฐจ] ์ ํ๋ฒ์ค
๋ฌธ์ ๋งํฌ
https://school.programmers.co.kr/learn/courses/30/lessons/17678
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
์ ํ๋ฒ์ค ์ดํ์ ๋ณด๊ฐ ์ฃผ์ด์ง ๋, ์ฝ์ด ์ ํ์ ํ๊ณ ์ฌ๋ฌด์ค๋ก ๊ฐ ์ ์๋ ๊ฐ์ฅ ๋ฆ์ ์ ๋ฅ์ฅ ๋์ฐฉ์๊ฐ์ ๊ตฌํ์ฌ๋ผ.
ํ์ด
์ฝ์ ์ด๋ฏธ ๋ค๋ฅธ ํฌ๋ฃจ๋ค์ด ๋ช์์ ์ ๋ฅ์ฅ์ ๋์ฐฉํ๋ ์ง ์๊ณ ์๋ค.
๊ทธ๋ฆฌ๊ณ ์ ํ๋ฒ์ค์๋ ์ ๋ฅ์ฅ์ ์ผ์ฐ ๋์ฐฉํ ์ฌ๋๋ถํฐ ํ์นํ๋ค.
์ฝ์ ์ ์ผ ๋ฆ๊ฒ ์ ๋ฅ์ฅ์ ๋์ฐฉํ๊ณ ์ถ์ดํ๊ธฐ ๋๋ฌธ์, ๋ฌด์กฐ๊ฑด ๋ง์ฐจ๋ฅผ ํ์ผํ๋ค.
๋ง์ฝ์ ๋์ฐฉํ ์๊ฐ์ ๊ธฐ์ค์ผ๋ก ํฌ๋ฃจ๋ค์ด ์ด๋ค ์ ํ๋ฒ์ค์ ํ์ง ๋ถ๋ฐฐํ์ ๋,
1. ๋ง์ฐจ๊ฐ ๋ง์์ด ์๋๋ผ๋ฉด, ์ฝ์ ๋ง์ฐจ ์ถ๋ฐ์๊ฐ์ ๋ฑ ๋ง์ถฐ ๋์ฐฉํ๋ฉด ๋๋ค.
2. ๋ง์ฐจ๊ฐ ๋ง์์ด๋ผ๋ฉด, ์ฝ์ m๋ฒ์งธ ์ฌ๋๋ณด๋ค ๋นจ๋ฆฌ ๋์ฐฉํด์ผํ๋ค.
=> m๋ฒ์งธ ์ฌ๋์ ์ ๋ฅ์ฅ ๋์ฐฉ์๊ฐ๋ณด๋ค 1๋ถ ๋น ๋ฅด๊ฒ ๋์ฐฉํ๋ค.
์ฝ๋ ์์ฑ
\\
|
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
string solution(int n, int t, int m, vector<string> timetable) {
string answer = "";
vector<int> crew; //ํฌ๋ฃจ๋ค์ ์ค์๋ ์๊ฐ
for(auto Time : timetable){
int clock = stoi(Time.substr(0,2));
int minute = stoi(Time.substr(3,2));
crew.push_back(clock * 100 + minute);
}
sort(crew.begin(), crew.end());
vector<int> Bus; // ๋ฒ์ค ์๊ฐํ
int clock = 9;
int minute = 0;
for(int i = 0; i<n; i++){
Bus.push_back(clock * 100 + minute);
minute += t;
if(minute >= 60){
minute %= 60;
clock ++;
}
}
vector<int> Board[11]; //i๋ฒ ๋ฒ์ค์ ํ์นํ ํฌ๋ฃจ ์ ๋ณด
for(auto Crew : crew){
int idx = lower_bound(Bus.begin(), Bus.end(), Crew) - Bus.begin();
if(Board[idx].size() >= m){
while(Board[idx].size() >= m && idx < Bus.size() - 1)
idx++;
}
Board[idx].push_back(Crew);
}
int N_Bus = Bus.size();
if(Board[N_Bus - 1].size() < m){
if(Bus[N_Bus - 1]/ 100 < 10)
answer += "0";
answer += to_string(Bus[N_Bus - 1] / 100);
answer += ":";
if(Bus[N_Bus - 1] % 100 < 10)
answer += "0";
answer += to_string(Bus[N_Bus - 1] % 100);
}
else{
int T = Board[N_Bus - 1][m-1];
int C = T / 100;
int M = T % 100;
M--;
if(M < 0){
M = 59;
C--;
}
if(C < 10)
answer += "0";
answer += to_string(C);
answer += ":";
if(M < 10)
answer += "0";
answer += to_string(M);
}
return answer;
}
|
cs |