Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- public static void main(String[] args) {
- BufferedReader reader = null;
- String temp;
- ArrayList<Visitor> visitors = new ArrayList<>();
- String [] tempForTime;
- try {
- reader = new BufferedReader(new FileReader("C:\\task2_inputData.txt"));
- while (reader.ready()) { //читаем файл
- temp = reader.readLine();
- temp = temp.substring(0, temp.length() - 2);
- tempForTime = temp.split(" ");
- DateFormat timeFormat = new SimpleDateFormat("HH:mm");
- Date tDuration = timeFormat.parse(tempForTime[0]);
- long in = tDuration.getTime();
- tDuration = timeFormat.parse(tempForTime[1]);
- long out = tDuration.getTime();
- visitors.add(new Visitor(in,out));
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ParseException e) {
- e.printStackTrace();
- }
- System.out.println(new Max().count(visitors).getOut());
- }
- public static class Max {
- private int max;//максимум посетителей
- private long in;//начало максимального посещения
- private long out;//конец максимального посещения
- public int getMax() {
- return max;
- }
- public long getIn() {
- return in;
- }
- public long getOut() {
- return out;
- }
- public Max count(List<Visitor> visitors) {
- Max maxVisitor = new Max();
- Set<Long> times = fillTime(visitors);
- int count = 0;
- int max = 0;
- long timeIn = 0;
- long timeOut = 0;
- for (Long time : times) {
- for (Visitor visitor : visitors) {
- if (time >= visitor.getIn() && time <= visitor.getOut()) {
- count++;
- }
- }
- if (count > max) {
- max = count;
- timeIn = time;
- timeOut = time;
- }
- if (count == max) {
- timeOut = time;
- }
- count = 0;
- }
- this.in = timeIn;
- this.out = timeOut;
- this.max = max;
- return maxVisitor;
- }
- private Set<Long> fillTime(List<Visitor> list) {
- Set<Long> timeSet = new TreeSet<>();
- for (Visitor element : list) {
- timeSet.add(element.getIn());
- timeSet.add(element.getOut());
- }
- return timeSet;
- }
- }
- public static class Visitor {
- private long in;
- private long out;
- public Visitor(long in, long out) {
- this.in = in;
- this.out = out;
- }
- public long getIn() {
- return in;
- }
- public long getOut() {
- return out;
- }
- }
- }
- //значения во входном файле
- 8:00 8:30\n
- 8:45 9:00\n
- 8:30 9:00\n
- 9:00 9:30\n
- 9:10 9:20\n
Advertisement
Add Comment
Please, Sign In to add comment