Guest User

Untitled

a guest
Feb 4th, 2021
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. public class Solution {
  2. public static void main(String[] args) {
  3. BufferedReader reader = null;
  4. String temp;
  5. ArrayList<Visitor> visitors = new ArrayList<>();
  6. String [] tempForTime;
  7.  
  8. try {
  9. reader = new BufferedReader(new FileReader("C:\\task2_inputData.txt"));
  10.  
  11. while (reader.ready()) { //читаем файл
  12. temp = reader.readLine();
  13. temp = temp.substring(0, temp.length() - 2);
  14. tempForTime = temp.split(" ");
  15. DateFormat timeFormat = new SimpleDateFormat("HH:mm");
  16. Date tDuration = timeFormat.parse(tempForTime[0]);
  17. long in = tDuration.getTime();
  18. tDuration = timeFormat.parse(tempForTime[1]);
  19. long out = tDuration.getTime();
  20. visitors.add(new Visitor(in,out));
  21.  
  22. }
  23. } catch (FileNotFoundException e) {
  24. e.printStackTrace();
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. } catch (ParseException e) {
  28. e.printStackTrace();
  29. }
  30.  
  31. System.out.println(new Max().count(visitors).getOut());
  32. }
  33. public static class Max {
  34.  
  35. private int max;//максимум посетителей
  36. private long in;//начало максимального посещения
  37. private long out;//конец максимального посещения
  38.  
  39. public int getMax() {
  40. return max;
  41. }
  42.  
  43. public long getIn() {
  44. return in;
  45. }
  46.  
  47. public long getOut() {
  48. return out;
  49. }
  50.  
  51. public Max count(List<Visitor> visitors) {
  52. Max maxVisitor = new Max();
  53.  
  54. Set<Long> times = fillTime(visitors);
  55. int count = 0;
  56. int max = 0;
  57. long timeIn = 0;
  58. long timeOut = 0;
  59. for (Long time : times) {
  60. for (Visitor visitor : visitors) {
  61. if (time >= visitor.getIn() && time <= visitor.getOut()) {
  62. count++;
  63. }
  64. }
  65. if (count > max) {
  66. max = count;
  67. timeIn = time;
  68. timeOut = time;
  69. }
  70. if (count == max) {
  71. timeOut = time;
  72. }
  73. count = 0;
  74. }
  75.  
  76. this.in = timeIn;
  77. this.out = timeOut;
  78. this.max = max;
  79.  
  80. return maxVisitor;
  81. }
  82.  
  83.  
  84. private Set<Long> fillTime(List<Visitor> list) {
  85. Set<Long> timeSet = new TreeSet<>();
  86. for (Visitor element : list) {
  87. timeSet.add(element.getIn());
  88. timeSet.add(element.getOut());
  89. }
  90. return timeSet;
  91. }
  92.  
  93. }
  94. public static class Visitor {
  95.  
  96. private long in;
  97.  
  98. private long out;
  99.  
  100. public Visitor(long in, long out) {
  101. this.in = in;
  102. this.out = out;
  103. }
  104.  
  105. public long getIn() {
  106. return in;
  107. }
  108.  
  109. public long getOut() {
  110. return out;
  111. }
  112. }
  113. }
  114.  
  115. //значения во входном файле
  116. 8:00 8:30\n
  117. 8:45 9:00\n
  118. 8:30 9:00\n
  119. 9:00 9:30\n
  120. 9:10 9:20\n
  121.  
Advertisement
Add Comment
Please, Sign In to add comment