Programmers / [PCCP 모의고사 1회] 3번 / 붕대 감기 / python

2025. 2. 14. 20:29·코딩테스트/programmers (python)

 

 

문제 : https://school.programmers.co.kr/learn/courses/30/lessons/250137?language=python3

 

나의 풀이

def attack(health, damage, max_health):
    health -= damage
    if health <= 0:
        return -1, 0
    return health, 0

def recover(health, count, bandage, max_health):
    health += bandage[1]
    count += 1

    if count == bandage[0]:
        health += bandage[2]
        count = 0

    health = min(health, max_health)
    return health, count

def solution(bandage, health, attacks):
    max_health = health
    count = 0
    attack_count = 0
    time = 1

    while time <= attacks[-1][0]:
        if attack_count < len(attacks) and attacks[attack_count][0] == time:
            health, count = attack(health, attacks[attack_count][1], max_health)
            attack_count += 1
            
            if health == -1:
                return -1
        else:
            health, count = recover(health, count, bandage, max_health)

        time += 1

    return health

 

 

+ 함수 하나로 표현한 더 간단한 풀이

def solution(bandage, health, attacks):
    max_health = health
    count = 0
    attack_count = 0
    time = 1
    
    while time <= attacks[-1][0]:
        if attack_count < len(attacks) and time == attacks[attack_count][0]:
            health -= attacks[attack_count][1]
            count = 0
            attack_count += 1
            
            if health <= 0:
                return -1
        else:
            health += bandage[1]
            count += 1
        
            if count == bandage[0]:
                health += bandage[2]
                count = 0
            
            health = min(health, max_health)
            
        time += 1
    return health

 

 

'코딩테스트 > programmers (python)' 카테고리의 다른 글

Programmers / [PCCP 기출문제] 1번 / 동영상 재생기 / python  (0) 2025.02.23
Programmers / [PCCP 기출문제] 2번 / 석유 시추 / python  (0) 2025.02.18
Programmers / [PCCP 모의고사 1회] 3번 / 유전법칙 / python  (0) 2025.02.14
Programmers / 2단계 / 행렬 테두리 회전하기 / python  (0) 2024.09.02
Programmers / 2단계 / 수식 최대 / python / 2020 카카오 인턴십  (0) 2024.07.29
'코딩테스트/programmers (python)' 카테고리의 다른 글
  • Programmers / [PCCP 기출문제] 1번 / 동영상 재생기 / python
  • Programmers / [PCCP 기출문제] 2번 / 석유 시추 / python
  • Programmers / [PCCP 모의고사 1회] 3번 / 유전법칙 / python
  • Programmers / 2단계 / 행렬 테두리 회전하기 / python
seulll
seulll
개인 공부 / 정리 블로그입니다
  • seulll
    seulll
    seulll
  • 전체
    오늘
    어제
    • 분류 전체보기 (339)
      • 코딩테스트 (231)
        • programmers (python) (156)
        • 백준 (python) (73)
      • 자료구조 | 알고리즘 (14)
      • 개발 | 프로젝트 (43)
        • Python (4)
        • Java | Spring (7)
        • Android (5)
        • Unity (3)
        • API (4)
      • CS (15)
        • Network (5)
        • SQL (2)
        • OS (4)
      • 데이터 분석 (14)
      • 기타 (13)
  • 블로그 메뉴

    • 홈
    • 태그
    • 글쓰기
    • 설정
  • 링크

    • GitHub
  • 인기 글

  • 태그

    모델 성능 평가
    바다코끼리
    solving environment
    train_test_split
    프렌즈4블록
    오블완
    야근 지수
    Boxplot
    코딩테스트
    대입 표현식
    Greedy
    프로그래머스
    카카오맵 api
    asterisk
    카카오맵
    백엔드 개발자
    박스플롯
    웹크롤링
    kakao map api
    2 x n 타일링
    데이터분석
    백엔드
    API
    파이썬
    confusion matrix
    그리디 알고리즘
    오차행렬
    백엔드 개발자 역량
    Python
    티스토리챌린지
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
seulll
Programmers / [PCCP 모의고사 1회] 3번 / 붕대 감기 / python
상단으로

티스토리툴바