https://programmers.co.kr/learn/courses/30/lessons/92341

 

코딩테스트 연습 - 주차 요금 계산

[180, 5000, 10, 600] ["05:34 5961 IN", "06:00 0000 IN", "06:34 0000 OUT", "07:59 5961 OUT", "07:59 0148 IN", "18:59 0000 IN", "19:09 0148 OUT", "22:59 5961 IN", "23:00 5961 OUT"] [14600, 34400, 5000]

programmers.co.kr

 

 

1. 차 번호를 key, 해당 차량이 드나든 시간 vector를 value로 갖는 car변수에 차 번호와 시간정보 저장

2. 차 번호와 해당 차량의 요금 정보를 pair로 갖는 carNum에 (차 번호, 0) 의 값을 pair로 저장

3. car에 담긴 시간 정보를 바탕으로 총 이용시간 산출

4. 산출된 시간정보를 바탕으로 feeCal함수를 사용하여 요금 정보 산출

5. 산출된 요금 정보를 carNum의 second value로 입력

6. 해당 값을 answer vector에 push_back

 

// 주차장에 머무른 총 시간을 계산하는 함수
int timeCal(string t, string tn) {
	int result=0;
	int h = stoi(t.substr(0, 2));
	int m = stoi(t.substr(3, 2));
	int hn = stoi(tn.substr(0, 2));
	int mn = stoi(tn.substr(3, 2));

	result = ((hn - h)*60) + (mn - m);
	return result;
}

// 시간 정보를 받아 요금을 계산하는 함수
int feeCal(int baseTime, int baseFee, int unitTime, int unitFee, int time) {
	int result=0;
	if (time > baseTime) {
		if((time - baseTime) % unitTime != 0)
			return (((time - baseTime) / unitTime)+1) * unitFee + baseFee;
		else
			return ((time - baseTime) / unitTime) * unitFee + baseFee;
	}
	else
		return baseFee;
}

vector<int> solution(vector<int> fees, vector<string> records) {
	vector<int> answer;
	map<string, vector<string>> car;
	set<pair<string, int>> carNum;	

	for (auto i : records) {
		car[i.substr(6, 4)].push_back(i.substr(0, 5));
		carNum.insert(make_pair(i.substr(6, 4), 0));
	}
	
	for (auto i : carNum) {
		string s = i.first;
		int x = car[s].size();
		if (x % 2 != 0)
			car[s].push_back("23:59");
	}

	for (auto i : carNum) {
		string s = i.first;
		int t = 0;
		for (int j = 0; j < car[s].size()-1; j+=2) {
			int time = timeCal(car[s][j], car[s][j + 1]);
			t += time;
		}
		answer.push_back(feeCal(fees[0], fees[1], fees[2], fees[3], t));
	}
	return answer;
}

+ Recent posts