File tree Expand file tree Collapse file tree 1 file changed +53
-0
lines changed
Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ class Solution {
3+ public int convertTime (String time ) {
4+ String [] args = time. split(" :" );
5+ return Integer . parseInt(args[0 ])* 3600 + Integer . parseInt(args[1 ])* 60 + Integer . parseInt(args[2 ]);
6+ }
7+
8+ public String solution (String play_time , String adv_time , String [] logs ) {
9+ // 1. imos
10+ int N = convertTime(play_time), M = convertTime(adv_time);
11+ long [] arr = new long [N + 2 ];
12+ for (String log : logs) {
13+ String [] args = log. split(" -" );
14+ int start = convertTime(args[0 ]), end = convertTime(args[1 ]);
15+ arr[start]++ ;
16+ arr[end]-- ;
17+ }
18+
19+ // 2. find
20+ long s = arr[0 ];
21+ long [] b = new long [N + 1 ];
22+ b[0 ] = s;
23+ for (int i= 1 ;i<= N ;i++ ) {
24+ s += arr[i];
25+ b[i] = s + b[i- 1 ];
26+ }
27+
28+ int ans = 0 ;
29+ long max = b[M ];
30+ for (int i= 1 ;i+ M - 1 <= N ;i++ ) if (b[i+ M - 1 ]- b[i- 1 ] > max) {
31+ max = b[i+ M - 1 ]- b[i- 1 ];
32+ ans = i;
33+ }
34+
35+ int hour = ans / 3600 ;
36+ ans %= 3600 ;
37+ int minute = ans / 60 ;
38+ ans %= 60 ;
39+ int second = ans;
40+
41+ String result = " " ;
42+ if (hour < 10 ) result += " 0" ;
43+ result += Integer . toString(hour) + " :" ;
44+ if (minute < 10 ) result += " 0" ;
45+ result += Integer . toString(minute) + " :" ;
46+ if (second < 10 ) result += " 0" ;
47+ result += Integer . toString(second);
48+
49+ return result;
50+
51+ }
52+ }
53+ ```
You can’t perform that action at this time.
0 commit comments