Time limit: 1 second Memory limit: 256 MB Alish, who is tired of this life, is going to…
- User
Time limit: 1 second Memory limit: 256 MB Alish, who is tired of this life, is going to Onur and goodbye tonight. Pasha, who is very interested in being a waiter, is a waiter at Dam Dareh and helps Ali. The party starts at 00:00 and lasts until 23:59.
Pasha's duty is that anyone who comes to a party or goes out, writes on a piece of paper first that person's name, then the time at that time, and then + or - (where + means he has come and - means he has left) and puts the paper in the bag. . After the party is over, Alish thinks about when the party was the busiest possible time, and since he was not distracted in the middle of the party, he asks this question to Pasha. Pasha, who is a simple waiter, has forgotten everything and only has the information on the papers. On the other hand, he doesn't want to upset Ali and asks you to tell him when your party was the busiest by taking information on the papers. (The number of people in the party at one time is counted after all the ins and outs at that time.)
input In the first line n n the number of pieces of paper, then in n The next line contains the name and time and one of the + or - characters.
1 ≤ n ≤ 100
000 1≤n≤100 000
The format of all hours is HH:MM. The name is a string consisting of English lowercase letters. The maximum length of the names is guaranteed 500
000 be 500,000. It is guaranteed that if someone enters the party, he was not in the party before that, and if he leaves, he was in the party before that. output Print the busiest party hour in HH:MM format on the only output line. If there are several answers, print one as desired.
Example Sample entry 1 2 Ali 23:32 - Ali 20:12 + Plain text Sample output 1 21:15 Plain text every hour between 20 : 12 20:12 to 23 : 31 23:31 can also be the answer.
Sample input 2 4 alish 16:15 + Pasha 22:34 - Alish 23:56 - Pasha 21:21 + Plain text Sample output 2 21:33 write code in java17
- Assistant
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { // Use BufferedReader for efficient reading BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = br.readLine(); if(line == null || line.isEmpty()){ System.out.println("00:00"); return; } int n = Integer.parseInt(line.trim()); // Initialize delta array for each minute from 0 to 1439 int[] delta = new int[1440]; for(int i=0;i<n;i++){ String eventLine = br.readLine(); if(eventLine == null || eventLine.isEmpty()) continue; String[] parts = eventLine.trim().split("\\s+"); if(parts.length !=3) continue; String timeStr = parts[1]; char sign = parts[2].charAt(0); int t = parseTime(timeStr); if(sign == '+'){ delta[t] +=1; } else if(sign == '-'){ delta[t] -=1; } // else ignore invalid sign } // Iterate through all minutes to find the best_minute int count =0; int max_count = 0; int best_minute =0; for(int t=0;t<1440;t++){ count += delta[t]; if(count > max_count){ max_count = count; best_minute = t; } } // Format the best_minute to HH:MM String result = formatTime(best_minute); System.out.println(result); } // Helper method to parse time string "HH:MM" to minutes since midnight private static int parseTime(String timeStr){ String[] parts = timeStr.split(":"); int hour = Integer.parseInt(parts[0]); int minute = Integer.parseInt(parts[1]); return hour * 60 + minute; } // Helper method to format minutes since midnight to "HH:MM" private static String formatTime(int t){ int hour = t / 60; int minute = t % 60; return String.format("%02d:%02d", hour, minute); } }