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
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%
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
Articles
12 articles in total
Salary breakdown in india
read article
Leetcode - 392. Is Subsequence
read article
Leetcode - 283. Move Zeroes
read article
Leetcode - 443. String Compression
read article
Leetcode - 334. Increasing Triplet Subsequence
read article
Leetcode - 238. Product of Array Except Self
read article
Leetcode - 151. Reverse Words in a String
read article
Leetcode - 345. Reverse Vowels of a String
read article
Leetcode - 605. Can Place Flowers
read article
Leetcode - 1431. Kids With the Greatest Number of Candies
currently reading
Leetcode - 1071. Greatest Common Divisor of Strings
read article
Leetcode - 1768. Merge Strings Alternately
read article
Featured ones: