강의로 돌아가기
김수현

[js] 뭐가 틀렸는지 모르겠어요..

어떤부분에서 오류가 있는지 모르겠어요 반례 부탁드리게요

작성중인 코드―solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function solution(bandage, health, attacks) {


    const length = attacks.length;
    let ss = 0;

    let heal = health;



    //시간 설정
    for(var i = 1 ; i <= attacks[attacks.length-1][0]; i++){
        let attacked = false;
        const time = i;
        //console.log("time : " + time);


        for(var j = 0; j < attacks.length ; j++){

            //console.log("attack : " + attacks[j][0]);
            if(time == attacks[j][0]){
                ss= 0;
                heal = heal - attacks[j][1];
                attacked = true;
                break;
            } 

        }

        if(heal <= 0){
                return -1;
        }



        if(!attacked){
            if(heal < health){
                heal += bandage[1];
            }
            //if(i != 0){}
                ss += 1;



            if(ss == bandage[0] ){
                ss = 0;
                heal += bandage[2];
                if(heal >= health){
                    heal = health;
                }
            }

        }
        if(heal <= 0){
                return -1;
        }


        //console.log("heal :" + heal);
    }


    return heal;

}
1 개의 답변
낙방여우

반례입니다.
입력값 〉 [5, 3, 2], 30, [[1, 1], [4, 12]]
기댓값 〉 18

  • 김수현

    감사합니다 ..ㅜㅜ

    김수현―2025.02.05 20:48
답변 쓰기
이 입력폼은 마크다운 문법을 지원합니다.