fromtypingimportListdefto_second(time:str)->int:second=0time_split=time.split(':')h,m,s=time_split[0],time_split[1],time_split[2]ifh<'10':second+=int(h[1])*60*60else:second+=int(h)*60*60ifm<'10':second+=int(m[1])*60else:second+=int(m)*60ifs<'10':second+=int(s[1])else:second+=int(s)returnseconddefto_time(second:int)->str:h=second//3600m=(second%3600)//60s=second%60returnto_time_str(h)+':'+to_time_str(m)+':'+to_time_str(s)defto_time_str(num:int)->str:ifnum<10:return'0'+str(num)returnstr(num)defsolution(play_time:str,adv_time:str,logs:List[str])->str:answer=0play_time_second=to_second(play_time)adv_time_second=to_second(adv_time)# 초당 시청자 수 (누적합 배열)
total_viewer_for_second=[0for_inrange(play_time_second+1)]# 각 구간의 시청자 수 체크
# total_viewer_for_second[time] = (time 시각에 시작한 시청자 수) - (time 시각에 종료한 시청자 수)
forloginlogs:start,end=log.split('-')start_sec=to_second(start)end_sec=to_second(end)total_viewer_for_second[start_sec]+=1total_viewer_for_second[end_sec]-=1# 누적합 계산 1회 (1초 간격으로 누적합 계산)
# total_viewer_for_second[time] = (time 시각부터 time + 1 시각까지의 1초간의 구간을 포함한 시청자 수)
foriinrange(1,len(total_viewer_for_second)):total_viewer_for_second[i]+=total_viewer_for_second[i-1]# 누적합 계산 2회 (해당 초까지의 누적 시청자 계산)
# total_viewer_for_second[time] = (0초부터 time + 1 시각까지의 구간을 포함한 누적 시청자 수)
foriinrange(1,len(total_viewer_for_second)):total_viewer_for_second[i]+=total_viewer_for_second[i-1]max_viewer_from_zero_time=total_viewer_for_second[adv_time_second-1]foriinrange(0,len(total_viewer_for_second)-adv_time_second):target=total_viewer_for_second[i+adv_time_second]-total_viewer_for_second[i]iftarget>max_viewer_from_zero_time:max_viewer_from_zero_time=targetanswer=i+1print(answer)returnto_time(answer)print(solution("02:03:55","00:14:15",["01:20:15-01:45:14","00:40:31-01:00:00","00:25:50-00:48:29","01:30:59-01:53:29","01:37:44-02:02:30"]))
packageprogrammers.sully.week39;publicclassInsertAd{publicstaticlongtoSecond(Stringtime){longsecond=0;String[]timeSplit=time.split(":");inth=Integer.parseInt(timeSplit[0]);intm=Integer.parseInt(timeSplit[1]);ints=Integer.parseInt(timeSplit[2]);second+=h<10?Integer.parseInt(timeSplit[0].substring(1))*60*60:h*60*60;second+=m<10?Integer.parseInt(timeSplit[1].substring(1))*60:m*60;second+=s<10?Integer.parseInt(timeSplit[2].substring(1)):s;returnsecond;}publicstaticStringtoTime(longsecond){longh=second/3600;longm=(second%3600)/60;longs=second%60;returntoTimeStr(h)+":"+toTimeStr(m)+":"+toTimeStr(s);}publicstaticStringtoTimeStr(longnum){returnnum<10?"0"+num:String.valueOf(num);}publicstaticStringsolution(StringplayTime,StringadvTime,String[]logs){longanswer=0;longplayTimeSecond=toSecond(playTime);longadvTimeSecond=toSecond(advTime);// 초당 시청자 수 (누적합 배열)long[]totalViewerForSecond=newlong[(int)(playTimeSecond+1)];// 각 구간의 시청자 수 체크// totalViewerForSecond[time] = (time 시각에 시작한 시청자 수) - (time 시각에 종료한 시청자 수)for(Stringlog:logs){String[]logSplit=log.split("-");longstartSec=toSecond(logSplit[0]);longendSec=toSecond(logSplit[1]);totalViewerForSecond[(int)startSec]++;totalViewerForSecond[(int)endSec]--;}// 누적합 계산 1회 (1초 간격으로 누적합 계산)// totalViewerForSecond[time] = (time 시각부터 time + 1 시각까지의 1초간의 구간을 포함한 시청자 수)for(inti=1;i<totalViewerForSecond.length;i++){totalViewerForSecond[i]+=totalViewerForSecond[i-1];}// 누적합 계산 2회 (해당 초까지의 누적 시청자 계산)// totalViewerForSecond[time] = (0초부터 time + 1 시각까지의 구간을 포함한 누적 시청자 수)for(inti=1;i<totalViewerForSecond.length;i++){totalViewerForSecond[i]+=totalViewerForSecond[i-1];}longmaxViewerFromZeroTime=totalViewerForSecond[(int)(advTimeSecond-1)];for(inti=0;i<totalViewerForSecond.length-advTimeSecond;i++){longtarget=totalViewerForSecond[i+(int)advTimeSecond]-totalViewerForSecond[i];if(target>maxViewerFromZeroTime){maxViewerFromZeroTime=target;answer=i+1;}}System.out.println(answer);returntoTime(answer);}}
Leave a comment