Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

15-kangrae-jo #57

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

15-kangrae-jo #57

wants to merge 2 commits into from

Conversation

kangrae-jo
Copy link
Collaborator

@kangrae-jo kangrae-jo commented Jan 11, 2025

πŸ”— 문제 링크

[λ™μ˜μƒ μž¬μƒκΈ°]

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

48m

✨ μˆ˜λ„ μ½”λ“œ

PCCP 기좜 1단계 문제λ₯Ό λ“€κ³ μ™”μŠ΅λ‹ˆλ‹€.
이 λ¬Έμ œλŠ” μ½μœΌλ©΄μ„œ λ™μ‹œμ— 문제λ₯Ό μ΄λ ‡κ²Œ ν’€μ–΄μ•Όκ² λ‹€λŠ” 생각이 λ“œλŠ” 일반적인 κ΅¬ν˜„λ¬Έμ œλΌκ³  μƒκ°ν–ˆμŠ΅λ‹ˆλ‹€.
κ·Έλž˜μ„œ λΉ λ₯΄κ²Œ 읽고 30λΆ„μ•ˆμ— 문제λ₯Ό ν’€κ³  μ‹Άμ–΄μ„œ λ‚˜μ΄λΈŒν•˜κ²Œ 생각이 λ‚˜λŠ” λŒ€λ‘œ μ½”λ“œλ₯Ό μ¨λ‚΄λ €κ°”μŠ΅λ‹ˆλ‹€.

문제 풀이 μˆœμ„œλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

1. string ν˜•νƒœμ˜ μ‹œκ°„ λ³€μˆ˜λ“€μ„ int ν˜•μœΌλ‘œ λŒ€μ†Œ 비ꡐ가 κ°€λŠ₯ν•˜λ„λ‘ λ³€ν˜•
2. commandλ₯Ό ν•˜λ‚˜μ”© κΊΌλ‚΄λ©° μ‹œκ°„μ„ μ€„μ΄κ±°λ‚˜ 늘리기
3. κ·Έ κ³Όμ • μ†μ—μ„œ κ³„μ†ν•΄μ„œ "μ˜€ν”„λ‹ κ±΄λ„ˆλ›°κΈ°" 검사 및 μ‹€ν–‰ 

κ°„λ‹¨ν•˜μ£ ? μ•„λ§ˆ c++을 μ•ˆμ“°λŠ” 뢄듀은 더 μ‰½κ²Œ λŠκ»΄μ‘Œμ„ κ²λ‹ˆλ‹€.

근데... 말은 쉽닀고 ν–ˆκ³  30λΆ„μ•ˆμ— 문제λ₯Ό 풀것이라 생각도 ν–ˆλŠ”λ° μ‹€μ œλ‘œ κ±Έλ¦°μ‹œκ°„μ€ 48뢄에 μ½”λ“œλ„ μ—‰λ§μž…λ‹ˆλ‹€...

제 μ½”λ“œκ°€ μ™œ 엉망이라고 λŠκΌˆλƒλ©΄, stringλ³€μˆ˜λ₯Ό intν˜• μ‹œκ°„ λ³€μˆ˜λ‘œ λ°›μ•„μ˜¬ λ•Œ
λΆ„μ—λŠ” 60을 κ³±ν•˜κ³ , μ΄ˆλŠ” κ·ΈλŒ€λ‘œ λ”ν•œλ‹€λ©΄ λ³΅μž‘ν•œ ν˜•νƒœλ‘œ 비ꡐλ₯Ό ν•  ν•„μš”κ°€ μ—†μ—ˆμŠ΅λ‹ˆλ‹€.

λΆ„κ³Ό 초λ₯Ό λ”°λ‘œ λ‹€λ£¨λŠ” 방식은 μ΄ν•΄ν•˜κΈ°λŠ” 쉽닀고 λŠλ‚„ 수 μžˆμ§€λ§Œ,
μ‹œκ°„μ˜ λŒ€μ†Œλ₯Ό, 그리고 증감을 μ œλŒ€λ‘œ λ‹€λ£¨κΈ°μ—λŠ” μ λ‹Ήν•˜μ§€ μ•Šμ•˜λ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€!

그리고 무엇보닀... μ‹œκ°„μ΄ μ΄λ ‡κ²Œ μ˜€λ²„λœ 이유 λ˜ν•œ λ‹€λ£¨λŠ” λ³€μˆ˜κ°€ λ„ˆλ¬΄ λ§Žμ•„μ„œ μ˜€νƒ€λ₯Ό μ°Ύκ³  고치기 μ–΄λ €μ› κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€.

μ—¬λŸ¬λΆ„μ€ λ¬Έμ œκ°€ 쉽닀고 μƒκ°λ˜μ–΄λ„ κΌ­ λ°©ν–₯을 ν•œλ²ˆμ”© μ κ²€ν•΄λ³΄μ‹œκ³  계속 λ‚˜μ•„κ°€μ‹œκΈΈ... λ°”λžλ‹ˆλ‹€...!

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

λ‹€λ₯Έ μ‚¬λžŒμ˜ 풀이λ₯Ό ν•œ 번 λ΄€μŠ΅λ‹ˆλ‹€.

int time(string str) { 
    return (stoi(str)*60)+((str[3]-'0')*10)+str[4]-'0';
}

μ œκ°€ μ•„κΉŒ λ§ν•œκ²Œ μ΄κ±°μž…λ‹ˆλ‹€.
λΆ„μ—λŠ” 미리 60을 κ³±ν•˜λŠ” 그런 λ‘œμ§μž…λ‹ˆλ‹€.
(μ΄λ ‡κ²Œ ν•΄λ†“μœΌλ©΄ μ‹œκ°„μ˜ λŒ€μ†Œ 비ꡐ와 증감이 쉽겠죠??)

그리고 stoiλ₯Ό μ‚¬μš©ν•˜λ©΄ "11:30"μ—μ„œ intκ°€ μ•„λ‹Œ 문자":"λ₯Ό λ§Œλ‚  λ•Œ κΉŒμ§€ stringν™” λ©λ‹ˆλ‹€.
(처음 μ•Œμ•˜λ„€μš©)

λ‹€λ₯Έ μ‚¬λžŒ 풀이 μ°Έκ³ 

Copy link
Collaborator

@g0rnn g0rnn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ œκ°€ 제일 μ•½ν•œ λ¬Έμžμ—΄ + κ΅¬ν˜„ λ¬Έμ œλ„€μš”. cpp을 μ‚¬μš©ν•˜λŠ” μ €λ‘œμ„œλŠ” ν’€κΈ°κ°€ μ‹«λ”κ΅°μš”.. 그만큼 λ“μ΄λ˜λŠ” λ¬Έμ œλΌλŠ” 거겟죠.?

저도 이 λ¬Έμ œλŠ” 30λΆ„ μ•ˆμ— 풀지 λͺ»ν–ˆμŠ΅λ‹ˆλ‹€. 근데 λ‚œμ΄λ„ μžμ²΄λŠ” 어렡지 μ•Šμ•„ λ³΄μ—¬μ„œ 계속 κ΅¬ν˜„ν•΄λ³΄μ•˜μ–΄μš”. κ°•λž˜λ‹˜μ΄ μ„€λͺ…ν•˜μ‹  μ½”λ“œλ₯Ό λ³΄λ‹ˆ μ‹œκ°„μ„ ν•˜λ‚˜μ˜ μ •μˆ˜λ‘œ ν‘œκΈ°ν•˜μ—¬ λŒ€μ†Œ 비ꡐλ₯Ό κ°„λ‹¨νžˆ ν•  수 μžˆλ„λ‘ ν•˜λŠ” 연산이 κ°€μž₯ μ •ν™•ν•˜κ²Œ 문제λ₯Ό ν’€ 수 μžˆμ„κ²ƒ κ°™μŠ΅λ‹ˆλ‹€.

ν•˜μ§€λ§Œ 제 μ½”λ“œλŠ” κ·Έλƒ₯ string을 지지고 λ³Άμ•˜μŠ΅λ‹ˆλ‹€. κ°μ‚¬ν•©λ‹ˆλ‹€.

code
#include <string>
#include <vector>
#include <string>
#include <iostream>

using namespace std;

bool isOpening(string pos, string start, string end) {
    if(start.substr(0,2) > pos.substr(0,2)) return false;
    if(start.substr(0,2) == pos.substr(0,2)  && start.substr(3,2) > pos.substr(3,2)) return false;
    if(end.substr(0,2) < pos.substr(0,2)) return false;
    if(end.substr(0,2) == pos.substr(0,2) && end.substr(3,2) < pos.substr(3,2)) return false;
    return true;
}

string jump(string pos, int step) {
    int min = stoi(pos.substr(3,2)) + step;
    int hour = stoi(pos.substr(0,2));
    if(min > 59) {
        hour += 1;
        min -= 60;
    }
    if(min < 0) {
        if(hour <= 0) return "00:00";
        hour -=1;
        min += 60;
    }
    string format = "00:00";
    format[0] += hour/10;
    format[1] += hour%10;
    format[3] += min/10;
    format[4] += min%10;
    return format;
}

bool isOver(string video, string pos) {
    if(video.substr(0,2) < pos.substr(0,2)) return true;
    if(video.substr(0,2) == pos.substr(0,2) && video.substr(3,2) < pos.substr(3,2)) return true;
    return false;
}

string solution(string video_len, string pos, string op_start, string op_end, vector<string> commands) {
    if(isOpening(pos, op_start, op_end)) pos = op_end;
    for(string& command : commands) {
        if(command == "next") pos = jump(pos, 10);
        else pos = jump(pos, -10);
        
        if(isOver(video_len, pos)) pos = video_len; // μ˜μƒμ˜ 길이λ₯Ό λ„˜μ„ λ•Œ
        if(isOpening(pos, op_start, op_end)) pos = op_end; // μ˜€ν”„λ‹μΌ λ•Œ
    }
    return pos;
}

Copy link
Collaborator

@wnsmir wnsmir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

뢄을 초둜 λ§Œλ“€κ³ , prev와 nextλ₯Ό ν•¨μˆ˜λ‘œ λ§Œλ“€μ–΄ κ³„μ‚°ν•˜λŠ”κ²ƒκΉŒμ§€λŠ” ꡉμž₯히 쉽고 μˆ˜μ›”ν•˜κ²Œ μ§„ν–‰λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

κ·ΈλŸ¬λ‚˜ μ œκ°€ 이해λ₯Ό 잘λͺ»ν•œκ±΄μ§€ λ¬Έμ œκ°€ λͺ¨ν˜Έν–ˆλ˜κ±΄μ§€ prevκ°€ μ €λ₯Ό 미치게 λ§Œλ“€μ—ˆμŠ΅λ‹ˆλ‹€.
단 ν•˜λ‚˜μ˜ ν…ŒμŠ€νŠΈμΌ€μ΄μŠ€κ°€ 계속 λΉ—λ‚˜κ°”λŠ”λ° κ²°κ΅­ 꼼꼼히 μƒκ°ν•΄λ³΄λ‹ˆ

μ˜€ν”„λ‹μ΄ 10초 λ―Έλ§ŒμΌλ•Œ, μ˜€ν”„λ‹μ•ˆμ—μ„œ prevλ₯Ό λˆŒλ €μ„λ•Œλ₯Ό 생각λͺ»ν–ˆμŠ΅λ‹ˆλ‹€.
μ˜€ν”„λ‹μ΄ 10μ΄ˆκ°€ λ„˜λŠ”λ‹€λ©΄, μ˜€ν”„λ‹μ•ˆμ—μ„œ prevλ₯Ό λˆŒλ €μ„λ•Œ μ˜€ν”„λ‹κ±΄λ„ˆλ›°κΈ°λ‘œ μ˜€ν”„λ‹ λλ‚˜λŠ”μ‹œμ μœΌλ‘œ κ°€λŠ”κ²Œ λ§žμ§€λ§Œ,

μ˜€ν”„λ‹μ΄ 10초λ₯Ό λ„˜κΈ°μ§€ μ•ŠλŠ”λ‹€λ©΄, μ˜€ν”„λ‹μ˜ λμ§€μ μ—μ„œ 10μ‘° μ΄μ „μœΌλ‘œ λŒμ•„κ°€λŠ”κ²ƒμ΄ λ§žλŠ” κ²ƒμ΄μ—ˆμŠ΅λ‹ˆλ‹€. (μ—¬λŸ¬ μ‹œλ„λμ— μ•Œμ•„λƒˆμŠ΅λ‹ˆλ‹€)

λ”°λΌμ„œ μ½”λ“œλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€,, μ œκ°€ 맞게 μƒκ°ν•˜κ³  μžˆλŠ”κ±΄μ§€ ν•œλ²ˆ ν¬λ‘œμŠ€μ²΄ν‚Ή λΆ€νƒλ“œλ¦½λ‹ˆλ‹€...

def solution(video_len, pos, op_start, op_end, commands):
    
    def time_to_seconds(time):
        minutes, seconds = map(int, time.split(":"))
        return minutes * 60 + seconds
    
    def seconds_to_time(seconds):
        minutes = seconds // 60
        seconds = seconds % 60
        return f"{minutes:02}:{seconds:02}"
        
    def prev(video_len, current, start, end):
        if start <= current <= end:  # μ˜€ν”„λ‹ ꡬ간
            if end > 10 and end - start < 10:
                current = end - 10
            else:
                current = end
            
        elif end + 10 >= current > end:  # μ˜€ν”„λ‹ λ’€μ—μ„œ μ•ˆμœΌλ‘œ λ“€μ–΄κ°ˆ 경우
            current = end
            
        elif current <= 10:  # μ˜μƒ μ‹œμž‘ 10초 미만
            current = 0
        
        else:
            current -= 10  # 일반적인 경우 10초 λ’€λ‘œ 이동
                
        return current
                
    def next(video_len, current, start, end):
        if current > video_len - 10:  # μ’…λ£ŒκΉŒμ§€ 10초 미만으둜 λ‚¨μ•˜μ„ 경우
            current = video_len
            
        elif start <= current <= end:  # μ˜€ν”„λ‹ μ•ˆμ—μ„œ nextλ₯Ό λˆŒλ €μ„ 경우
            if end + 10 > video_len:
                current = video_len
            else: current = end + 10
            
        elif start - 10 <= current < start:  # μ˜€ν”„λ‹ μ „μ—μ„œ μ•ˆμœΌλ‘œ λ“€μ–΄κ°ˆ 경우
            current = end
            
        else:  # 일반적인 경우
            current += 10
            
        return current
        
    # λ¬Έμžμ—΄ μ‹œκ°„μ„ 초 λ‹¨μœ„λ‘œ λ³€ν™˜
    video_len = time_to_seconds(video_len)  # video_len λ³€ν™˜ μΆ”κ°€
    current = time_to_seconds(pos)
    start = time_to_seconds(op_start)
    end = time_to_seconds(op_end)
        
    # λͺ…λ Ήμ–΄ 처리
    for command in commands:
        if command == 'prev':
            current = prev(video_len, current, start, end)
        elif command == 'next':
            current = next(video_len, current, start, end)
    
    answer = seconds_to_time(current)
    
    return answer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants