μ½”λ”©ν…ŒμŠ€νŠΈ/μ½”λ”©ν…ŒμŠ€νŠΈ μ—°μŠ΅

[C++] ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ 2018 KAKAO BLIND RECRUITMENT [1μ°¨] μ…”ν‹€λ²„μŠ€

Bradbury 2020. 10. 28. 09:27
728x90

(1μ‹œκ°„ 40)

HH:MMν˜•νƒœμ˜ μ‹œκ°„μ„ λΆ„μœΌλ‘œ μΉ˜ν™˜(int둜 λ³€ν™˜) ν›„ μš°μ„ μˆœμœ„ 큐에 μ§‘μ–΄λ„£μ—ˆλ‹€.

제일 λŠ¦μ€ μ‹œκ°„μ„ κ΅¬ν•˜λŠ” κ±°λ‹ˆκΉŒ λ§ˆμ§€λ§‰ μš΄ν–‰ μ°¨λŸ‰ μ „κΉŒμ§€λŠ” νƒ‘μŠΉ κ°€λŠ₯ν•œ 인원에 맞좰 μ œκ±°ν•΄μ£Όκ³ 

λ§ˆμ§€λ§‰ μš΄ν–‰μ—μ„œλŠ” νƒ‘μŠΉ 인원이 빌 경우 λ§ˆμ§€λ§‰ μš΄ν–‰μ‹œκ°„μ΄ 도착 μ‹œκ°„ 쀑 제일 λŠ¦μ€ μ‹œκ°μ΄κ³ 

νƒ‘μŠΉ 인원이 가득 찼을 경우 제일 λ§ˆμ§€λ§‰μ— 탄 μ‚¬λžŒμ—μ„œ -1 ν•œκ²Œ 도착 μ‹œκ°„ 쀑 제일 λŠ¦μ€ μ‹œκ°„μ΄λ‹€.

 

#include <string>
#include <vector>
#include <queue>

using namespace std;

string solution(int n, int t, int m, vector<string> timetable) {
	priority_queue<int, vector<int>, greater<int>> pq;
	vector<int> bus;
	int answer = 0;

	// 크루 λŒ€κΈ°μ—΄ μ‹œκ°„ μ •λ ¬ν•˜κΈ°
	for (int i = 0; i < timetable.size(); i++) {
		int hour = atoi(timetable[i].substr(0, 2).c_str());
		int minute = atoi(timetable[i].substr(3, 2).c_str());
		int time = hour * 60 + minute;
		pq.push(time);
	}

	// λ²„μŠ€ μš΄μ˜μ‹œκ°„ μ €μž₯
	int startTime = 9 * 60;
	for (int i = 0; i < n; i++) {
		bus.push_back(startTime + t * i);
	}

	// λ§ˆμ§€λ§‰ 운영 μ‹œκ°„ μ „κΉŒμ§€ λŒ€κΈ°μ—΄ μ§€μš°κΈ°
	int idx;
	for (idx = 0; idx < bus.size() - 1; idx++) {
		for (int j = 0; j < m; j++) {
			if (!pq.empty()) {
				if (bus[idx] >= pq.top()) {
					pq.pop();
				}
			}
		}
	}
    
    // λ§ˆμ§€λ§‰ λ²„μŠ€κ°€ 가득 μ°ΌλŠ”μ§€
    bool isEmpty = true;
    
	// λ§ˆμ§€λ§‰ 운영 μ‹œκ°„
	for (int j = 0; j < m; j++) {
		if (!pq.empty()) {
			if (bus[idx] >= pq.top()) {
                answer = pq.top();
				pq.pop();
            }else {
                answer = bus[idx];
                isEmpty = false;
                break;
		    }
		}else {
			answer = bus[idx];
            isEmpty = false;
			break;
		}
	}
    
    // λ²„μŠ€κ°€ 가득 찼을 경우 제일 λ§ˆμ§€λ§‰μ— 탄 μ‚¬λžŒλ³΄λ‹€ 1 μž‘μ€ κ²½μš°κ°€ μ •λ‹΅
    if(isEmpty) answer = answer - 1;

	// μ‹œκ°„ λ³€ν™˜
	string ansHour = answer / 60 > 9 ? to_string(answer / 60) : '0' + to_string(answer / 60);
	string ansMinute = answer % 60 > 9 ? to_string(answer % 60) : '0' + to_string(answer % 60);
	string ansStr = ansHour + ':' + ansMinute;

	return ansStr;
}
728x90