Wedding Ceremony

Problem

Hyun-soo is getting married next month.
Hyun-soo rents a place for the wedding reception and plans to hold it for three days without a break.
Hyeon-soo requested information about the time of N friends attending the reception in advance.
Each friend told Hyeon-soo what time he would arrive and what time he would leave.
Based on this information, Hyun-soo tries to find the maximum number of people present at the reception area at the same time and rent a place that can accommodate that number of people. Please help Hyun-soo.
If a friend arrives at 13 and leaves at 15, it is assumed that this friend is present at the reception at 13:00 and not at 15:00.

input

  • The first line gives the number of people N (5<=N<=100,000) to attend the reception
  • From line 2 to line N, each person’s arrival and departure times are given
  • Time is expressed as a non-negative integer with the arrival and departure times of the timeline with 0 o’clock on the first day as 0 and 12 o’clock on the last night as 72
5

14 18
12 15
15 20
20 30
5 14

output

In the first line, print the maximum number of people present in the extension at the same time.

2

Solution

function solution(times) {
	let answer = Number.MIN_SAFE_INTEGER,
		timeLine = [],
		cnt = 0;

	for (let [st, et] of times) {
		timeLine.push([st, 1]);
		timeLine.push([et, 0]);
	}

	timeLine.sort(([t1, type1], [t2, type2]) => t1 - t2 || type1 - type2);

	for (let [time, type] of timeLine) {
		if (type) cnt++;
		else cnt--;

		answer = Math.max(answer, cnt);
	}

	return answer;
}
function solution(times) {
	let answer = Number.MIN_SAFE_INTEGER,
		T_line = [];

	times.sort(([st1, et1], [st2, et2]) => st1 - st2 || et1 - et2);

	for (let x of times) {
		T_line.push([x[0], 's']);
		T_line.push([x[1], 'e']);
	}

	T_line.sort((a, b) => {
		if (a[0] === b[0]) return a[1].charCodeAt() - b[1].charCodeAt();
		else return a[0] - b[0];
	});

	let cnt = 0;

	for (let x of T_line) {
		if (x[1] === 's') cnt++;
		else cnt--;

		answer = Math.max(answer, cnt);
	}

	return answer;
}