Logo

dev-resources.site

for different kinds of informations.

Leetcode - 1431. Kids With the Greatest Number of Candies

Published at
9/9/2024
Categories
leetcode
Author
Rakesh Reddy Peddamallu
Categories
1 categories in total
leetcode
open
Leetcode - 1431. Kids With the Greatest Number of Candies

Got the brute force solution

Javascript Code

/**
 * @param {number[]} candies
 * @param {number} extraCandies
 * @return {boolean[]}
 */
var kidsWithCandies = function(candies, extraCandies) {
    let max = -1 ;
    candies.forEach((candy)=>{
        if(candy >max){
            max = candy
        }
    })
return candies.map((candy)=> candy+extraCandies >= max )
};

but it only beats 6%

Image description

and this beats 88%

/**
 * @param {number[]} candies
 * @param {number} extraCandies
 * @return {boolean[]}
 */
var kidsWithCandies = function(candies, extraCandies) {
    let max = Math.max(...candies) ;
return candies.map((candy)=> candy+extraCandies >= max )
};

what does

javascript Math.max(...candies)

doing so great here ?

** GPT says **
Math.max when called with multiple arguments, it needs to evaluate each argument to determine the maximum value. In this case, the time complexity is 𝑂(𝑛), where n is the number of arguments passed to the function. This is because the function must iterate over all arguments to find the maximum value.

Okay got it , not sure why but each time i submit it beats different percentage

Featured ones: