Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 서블릿
- 양꼬치
- 2839
- 2638
- 다이나믹 프로그래밍
- 문자열 압축
- 투어
- 맛집
- 스프링 MVC
- 설탕 배달
- 1로 만들기
- Servlet
- 백준
- 쓰레드 풀
- 호유동
- 동적 프로그래밍
- 프로그래머스
- 알고리즘
- HTTP API
- 맛집 투어
- mvc
- 2020 KAKAO BLIND
- 2589
- 완도산회
- Spring
- 스프링
- 포두부 보쌈
- BFS
- 고모네 콩탕
- dp
Archives
- Today
- Total
프로그래밍 공방
[프로그래머스] 광고 삽입 본문
문제
programmers.co.kr/learn/courses/30/lessons/72414
문제해결방법
이 문제는 전체 시간을 배열로 잡고 각 시간마다 몇 명이 듣고 있는지 다 구해준 다음에 광고 길이를 구간으로 잡고
그 구간의 합이 최대가 될 때의 시작 시간을 반환하는 문제였다.
* 계속 테스트 케이스 하나가 틀렸는데 알고보니까 자료형 문제였다. 정신차리고 주의하자.
코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | package Programmers; public class Solution_광고삽입 { public static int[] timeLine, sumOfTimeLine; public static int timeToInt(String time) { String[] str = time.split(":"); return Integer.parseInt(str[0])*3600+Integer.parseInt(str[1])*60+Integer.parseInt(str[2]); } public static String intToTime(long time) { StringBuilder sb = new StringBuilder(); if(time/3600<10) sb.append('0'); sb.append(time/3600); sb.append(':'); time%=3600; if(time/60<10) sb.append('0'); sb.append(time/60); sb.append(':'); time%=60; if(time<10) sb.append('0'); sb.append(time); return sb.toString(); } public static String solution(String play_time, String adv_time, String[] logs) { String answer = ""; timeLine = new int[timeToInt(play_time)+1]; sumOfTimeLine = new int[timeToInt(play_time)+1]; int adv = timeToInt(adv_time); for(int i=0; i<logs.length; i++) { String[] str = logs[i].split("-"); timeLine[timeToInt(str[0])]++; timeLine[timeToInt(str[1])]--; } int curViewCount = 0; long sum = 0; for(int i=0; i<=adv; i++) { sum += curViewCount; curViewCount+=timeLine[i]; sumOfTimeLine[i] = curViewCount; } long max = sum; int startTime = 0; for(int i = adv+1; i<timeLine.length; i++) { sum += curViewCount; sum -= sumOfTimeLine[i-adv-1]; curViewCount+=timeLine[i]; sumOfTimeLine[i] = curViewCount; if(max<sum) { max = sum; startTime = i-adv; } } answer = intToTime(startTime); System.out.println(intToTime(max)); return answer; } public static void main(String[] args) { String play_time = "02:03:55"; String adv_time = "00:14:15"; String[] logs = {"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"}; System.out.println(solution(play_time, adv_time, logs)); } } | cs |
코드에 대한 피드백이나 더 좋은 아이디어는 언제나 환영입니다.
'개발 > 문제해결' 카테고리의 다른 글
[백준] 14425번 : 문자열 집합 (0) | 2021.02.02 |
---|---|
[프로그래머스] 외벽 점검 (0) | 2021.01.29 |
[백준] 1238번 : 파티 (0) | 2021.01.27 |
[프로그래머스] 신규 아이디 추천 (0) | 2021.01.27 |
[프로그래머스] 합승 택시 요금 (0) | 2021.01.26 |