호지

[프로그래머스] [PCCP 기출문제] 1번 / 붕대 감기 문제풀이 JS 본문

알고리즘/프로그래머스

[프로그래머스] [PCCP 기출문제] 1번 / 붕대 감기 문제풀이 JS

_hoji

attack을 for문으로 돌려 해당 attack전에 회복량이 얼마인지 판별하면 되는 문제이다.

도중에 체력이 0이하가 되면, 바로 return -1을 한다.

현재 체력은 초당 회복량을 더하고, 연속 성공을 했을 경우 추가 회복량을 더해주면 된다.

모든 for문 종료 후 체력이 0이하일 때 return -1을 하고

그 외의 경우엔 currentHealth를 return 하면 된다.

const solution = (bandage, health, attacks) => {
  let time = 0
  let currentHealth = health
  for (const [attackTime, damage] of attacks) {
    let healTime = attackTime - 1 - time
    currentHealth = currentHealth + bandage[1] * healTime
    while (healTime >= bandage[0]) {
      currentHealth += bandage[2]
      healTime -= bandage[0]
    }
    if (currentHealth >= health) currentHealth = health
    currentHealth -= damage
    time = attackTime

    if (currentHealth <= 0) return -1
  }
  if (currentHealth <= 0) return -1
  return currentHealth
}

 

 

 

https://school.programmers.co.kr/learn/courses/30/lessons/250137

Comments