프로그래머스 [PCCP 기출문제] 1번 / 동영상 재생기

2025. 2. 16. 19:56·코딩 알고리즘 스터디

https://school.programmers.co.kr/learn/courses/30/lessons/340213?language=java

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

1. 문제 설명

- 동영상 기능

prev : 10초 전으로 이동

next : 10초 후로 이동

오프닝 건너뛰기 : 오프닝이 끝나는 위치로 이동 (현재 위치가 오프닝 구간일 경우)

 

- 변수

video_len : 동영상 길이

pos : 기능이 수행되기 직전의 재생위치

op_start : 오프닝 시작 시간

op_end : 오프닝 끝나는 시간

commands : 사용자의 입력 (1차원 문자열배열)

 

- 결과

사용자의 입력이 모두 끝난 후 동영상의 위치를 "mm:ss" 형태로 return

 

2. 문제 풀이

- 초를 계산하는 함수를 만들어 바꾸어 주었다. 

    // 문자열로 되어있는 시간을 계산하기 위해 초로 변경
    public int second(String time) {
        String[] a = time.split(":");
        int m = Integer.parseInt(a[0]);
        int s = Integer.parseInt(a[1]);
        return m*60+s;
    }
    public String solution(String video_len, String pos, String op_start, String op_end, String[] commands) {
    // 문자열시간을 초로 변경
    int currentPos = second(pos);
    int videoLen = second(video_len);
    int opStart = second(op_start);
    int opEnd = second(op_end);

 

- 실행 전과 실행 후에 오프닝 시간에 있는지 확인, prev, next 기능이 실행될 경우 확인

        // commands가 진행되었을 경우
        for (String command : commands) {
            // 기능 실행 전, 시간이 오프닝시간에 있는지 확인
            if (currentPos >= opStart && currentPos <= opEnd) {
                currentPos = opEnd;
            }
            // prev일 경우
            if (command.equals("prev")) {           
                currentPos -= 10;
                if (currentPos < 0){
                    currentPos = 0;
                }
            // next일 경우
            } else {
                currentPos += 10;
                if (currentPos > videoLen){
                    currentPos = videoLen;
                }
            }
            // 기능 실행 후, 시간이 오프닝시간에 있는지 확인
            if (currentPos >= opStart && currentPos <= opEnd) {
                currentPos = opEnd;
            }
        }

 

- 다시 mm:ss 형태로 변경

        // mm:ss 형태로 변환
        int m = currentPos / 60;
        int s = currentPos % 60;
        return String.format("%02d:%02d", m,s);

 

- 전체 코드

class Solution {
	// 문자열로 되어있는 시간을 계산하기 위해 초로 변경
    public int second(String time) {
        String[] a = time.split(":");
        int m = Integer.parseInt(a[0]);
        int s = Integer.parseInt(a[1]);
        return m*60+s;
    }
    
    public String solution(String video_len, String pos, String op_start, String op_end, String[] commands) {
        // 문자열시간을 초로 변경
        int currentPos = second(pos);
        int videoLen = second(video_len);
        int opStart = second(op_start);
        int opEnd = second(op_end);
        
        // commands가 진행되었을 경우
        for (String command : commands) {
            // 기능 실행 전, 시간이 오프닝시간에 있는지 확인
            if (currentPos >= opStart && currentPos <= opEnd) {
                currentPos = opEnd;
            }
            // prev일 경우
            if (command.equals("prev")) {           
                currentPos -= 10;
                if (currentPos < 0){
                    currentPos = 0;
                }
            // next일 경우
            } else {
                currentPos += 10;
                if (currentPos > videoLen){
                    currentPos = videoLen;
                }
            }
            // 기능 실행 후, 시간이 오프닝시간에 있는지 확인
            if (currentPos >= opStart && currentPos <= opEnd) {
                currentPos = opEnd;
            }
        }
        // mm:ss 형태로 변환
        int m = currentPos / 60;
        int s = currentPos % 60;
        return String.format("%02d:%02d", m,s);
    }
}

'코딩 알고리즘 스터디' 카테고리의 다른 글

프로그래머스 DP / N으로 표현 (Python)  (0) 2025.03.17
프로그래머스 [2025 프로그래머스 코드챌린지 2차 예선] / 택배상자 꺼내기 - java  (1) 2025.02.24
프로그래머스 [PCCE 기출문제] 10번 / 데이터 분석  (0) 2025.02.16
프로그래머스 Lv.1 대충 만든 자판도움말 - Java  (1) 2025.01.12
프로그래머스 Lv.1 [2024 KAKAO WINTER INTERNSHIP] 가장 많이 받은 선물 - JAVA  (0) 2025.01.05
'코딩 알고리즘 스터디' 카테고리의 다른 글
  • 프로그래머스 DP / N으로 표현 (Python)
  • 프로그래머스 [2025 프로그래머스 코드챌린지 2차 예선] / 택배상자 꺼내기 - java
  • 프로그래머스 [PCCE 기출문제] 10번 / 데이터 분석
  • 프로그래머스 Lv.1 대충 만든 자판도움말 - Java
Rabet
Rabet
  • 블로그 메뉴

    • 관리자
    • 글쓰기
  • Rabet
    卯
    Rabet
  • 전체
    오늘
    어제
    • Root (139)
      • KT AIVLE School (85)
        • Start (4)
        • Python프로그래밍 & 라이브러리 (6)
        • 데이터 처리 및 분석 (7)
        • 데이터 분석 및 의미 찾기 (7)
        • 웹크롤링 (10)
        • 머신러닝 (10)
        • 딥러닝 (6)
        • 시각지능 딥러닝 (10)
        • 언어지능 딥러닝 (6)
        • JAVA (4)
        • SQL (2)
        • 가상화 클라우드 (5)
        • 프로젝트 (8)
      • QA (2)
        • 오류사항 (1)
      • 웹공부 (14)
        • SPRING (11)
        • React (1)
      • 코딩 알고리즘 스터디 (23)
      • 코딩테스트 (9)
        • JAVA (8)
        • HTML (1)
      • CS공부 (3)
      • 자격증공부 (3)
        • 정보처리기사 (1)
        • 컴퓨터활용능력 1급 (1)
        • AICE Associate (1)
        • CSTS (0)
  • 인기 글

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.2
Rabet
프로그래머스 [PCCP 기출문제] 1번 / 동영상 재생기
상단으로

티스토리툴바