본문 바로가기

코딩테스트/프로그래머스

[프로그래머스 C++] 250137. 1번 / 붕대 감기

문제 링크


 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

접근 방법


공격 시간에 따라 현재 체력을 계산합니다.

스 코드


#include <string>
#include <vector>

using namespace std;

int solution(vector<int> bandage, int health, vector<vector<int>> attacks)
{
    int s = 1, time, curHealth = health;
    
    for (auto &a : attacks)
    {
        time = a[0] - s;
        curHealth += (time / bandage[0]) * (bandage[2] + bandage[1] * bandage[0]) + (time % bandage[0]) * bandage[1];
        if (curHealth > health)
            curHealth = health;
        
        curHealth -= a[1];
        if (curHealth <= 0)
            return -1;
        
        s = a[0] + 1;
    }
    
    return curHealth;
}

코드 설명


현재 체력을 공격 맞기 전 시간으로 계산합니다. 추가 체력 회복과 매초 회복량을 계산하고,

최대 체력을 넘어가면 최대체력으로 바꿔줍니다.

그다음 피해량만큼 체력을 감소시키며 음수보다 작으면 -1을 리턴합니다.

공격당한 그 다음시간을 시작시간으로 설정합니다.