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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more