728x90
반응형
https://school.programmers.co.kr/learn/courses/30/lessons/250137?language=cpp
1. Logic
그냥 구현문제
부분문제를 나누자면
1. 몬스터에게 공격받을 때
2. 몬스터에게 공격 안받을 때 (붕대 감아야 함)
부분문제를 나눠서 상황에 따라 구현하면 실수를 줄일 수 있다.
2. Code
#include <string>
#include <vector>
#include <iostream>
using namespace std;
int solution(vector<int> bandage, int health, vector<vector<int>> attacks) {
int hp = health;
int bandageCnt = 0; //연속으로 밴드 감은 시간
int monsterTime = attacks[0][0]; //몬스터가 공격하는 시점
int monsterAttack = attacks[0][1]; //몬스터 공격량
int mCnt = 0; //몬스터 배열 접근
//오름차순으로 정렬되어 있기 때문에 마지막 공격 시점까지
for(int i = 1; i <= attacks[attacks.size() -1][0]; i++) {
if(monsterTime == i) { //몬스터가 공격할 차례일 때
bandageCnt = 0;
hp -= monsterAttack;
if(hp <= 0) return -1;//죽으면 리턴
if(mCnt + 1 < attacks.size()) {
mCnt++;
monsterTime = attacks[mCnt][0];
monsterAttack = attacks[mCnt][1];
}
}
else { // 공격 안해서 붕대 감을 떄
bandageCnt++;
hp += bandage[1];
if(bandageCnt == bandage[0]) { //밴드 연속해서 감으면 추가 회복
bandageCnt = 0;
hp += bandage[2];
}
if(hp > health) hp = health; //기본 체력보다 넘으면 최대 체력으로 초기화
}
}
return hp;
}
알고리즘 Solve 후 제가 생각한 Logic을 기록하는 개인 공부 블로그입니다.
내용 중 최적화가 가능한 부분등은 언제든지 댓글로 틀린 부분 및 피드백 주시면 공부 및 반영하겠습니다🧐
728x90
반응형
'Algorithm > Programmers' 카테고리의 다른 글
[백준/Baekjoon] 2342 Dance Dance Revolution C++ :: Dynamic Programming (1) | 2024.02.04 |
---|---|
[프로그래머스/Programmers] Level1 가장 많이 받은 선물 C++ :: Implementation (0) | 2024.02.02 |
[프로그래머스/Programmers] Level 3 숫자 게임 C++ :: Implementation (0) | 2023.10.03 |
[프로그래머스/Programmers] Level 2 마법의 엘리베이터 C++ :: Implementation (0) | 2023.09.27 |
[프로그래머스/Programmers] 점프와 순간 이동 Level.2 C++ (0) | 2023.07.28 |