DEV Community

Rakesh Reddy Peddamallu
Rakesh Reddy Peddamallu

Posted on

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 )
};

Enter fullscreen mode Exit fullscreen mode

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 )
};
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)