HackerRank-生日蛋糕蜡烛

时间:2019-09-05 03:05:59

标签: javascript

我只是在练习Hackerrank的Javascript。我正在研究的问题如下:

Colleen快n岁了!因此,她的蛋糕上有n根高度各不相同的蜡烛,而蜡烛i的高度为i。因为较高的蜡烛比较短的蜡烛高,所以Colleen只能吹出较高的蜡烛。

给出每个蜡烛的高度,找到并打印她可以成功吹灭的蜡烛数量。

输入格式

第一行包含一个整数,表示蛋糕上蜡烛的数量。第二行包含以空格分隔的整数,其中每个整数描述了Candle的高度。

输出格式

打印Colleen在新行中吹灭的蜡烛数。

我执行了以下代码,正确的答案反映在控制台日志中。但是,当我尝试在Hackerrank上运行代码时,它说我的答案是不确定的。为什么会这样?

function birthdayCakeCandles(ar) {
    var maxHeight = Math.max(...ar);
    var maxHeightCount = 0;  
    for(var i = 0; i < ar.length; i++){
        if (ar[i] == maxHeight){
            maxHeightCount = maxHeightCount + 1;
        }
    }
    console.log(maxHeightCount);
}

7 个答案:

答案 0 :(得分:2)

function birthdayCakeCandles(arr) {

    const velas = arr.filter(value => Math.max(...arr) === value)
    return velas.length;
}

答案 1 :(得分:0)

尝试返回值:

`

function birthdayCakeCandles(ar) {
    var maxHeight = Math.max(...ar);
    var maxHeightCount = 0;  
    for(var i = 0; i < ar.length; i++){
        if (ar[i] == maxHeight){
            maxHeightCount = maxHeightCount + 1;
        }
    }
    console.log(maxHeightCount);
    return maxHeightCount;
}

`

答案 2 :(得分:0)

function birthdayCakeCandles(candles) {
    var max_candles = Math.max(...candles) 
    var number_candles = 0
    for(var i=0; i<candles.length; i++){
        if(candles[i] == max_candles){
            number_candles = number_candles + 1   
        }
    }
    return number_candles;
}

答案 3 :(得分:0)

function birthdayCakeCandles(candles) {
    // Write your code here
   let max = candles.reduce((a, b)=>Math.max(a, b));
   return candles.filter(x => x == max).length

}

答案 4 :(得分:0)

试试这个解决方案:

.text       
        .globl __start 
__start:        # execution starts here  */

la $a0, ans # load str "count ="
li $v0, 4
syscall     
            la $t0,nums    # load array into address
            li $t1,0      # setting counter to 0
            addi $s0, $zero, 50    # adding constant of 50
            addi $s1, $zero, 100   #adding constant of 100
            lw $t5,count           # loading word "9"
            
again :         lw $t4 ($t0)      # load first element of array into t4
                bgt $t4,$s0,passif50       #comparing if first element is greater than 50
                

passif50 :      blt $t4,$s1,passif100      #comparing if first element is less than 100
                

passif100 :     add $t1, $t1,1             #incrementing counter by 1
                add $t0, $t0,4             #increment current array element pointer
                sub $t5,$t5,1              #deincrement "count" word by 1
                beqz $t5,done              #when "count" word is equal to 0, exit loop
        
                
done :          move $a0,$t1
                li $v0,1
                syscall                # print the count

                li $v0, 10
                syscall                # end program
        

     .data          
nums:   .word 55,68,2,60,2000,51,99,1,67
count:  .word 9
ans:    .asciiz "count = "
endl:   .asciiz "\n"

答案 5 :(得分:0)

这是我通过所有测试用例的解决方案。

function birthdayCakeCandles(candles) {
    const bigCandle = Math.max(...candles);
    const count = candles.filter(x => x === bigCandle).length;
    return count;
}

答案 6 :(得分:0)

这是我使用 Array Reduce 方法的解决方案

function birthdayCakeCandles(candles) {
  let maxHeight = Math.max.apply(null, candles);
  let totalTallCandles = candles.reduce((cum, cur) => {
    if (cur == maxHeight) {
      cum += 1;
    }
    return cum;
  }, 0);
  return totalTallCandles;
}

let total = birthdayCakeCandles([3, 5, 2, 1, 6, 5, 6]);

alert(total);