https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/

 

Kids With the Greatest Number of Candies - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

candies 양의 정수 배열은 각 index 별로 아이들이 가지고 있는 캔디 수를 의미합니다.

아이에게 extraCandies를 주면 아이들 중에 가장 많은 캔디를 가질 수 있는지 없는지를 판별하여

boolean 배열을 반환합니다.

 

javascript

/**
 * @param {number[]} candies
 * @param {number} extraCandies
 * @return {boolean[]}
 */
const kidsWithCandies = (candies, extraCandies) => {
  const greatestNum = Math.max(...candies)
  const result = candies.map(kid => kid + extraCandies >= greatestNum)
  return result
}

typescript

const kidsWithCandiesTS = (candies: number[], extraCandies: number): boolean[] => {
  const greatestNum: number = Math.max(...candies)
  const result: boolean[] = candies.map(kid => kid + extraCandies >= greatestNum)
  return result
}

golang

func kidsWithCandies(candies []int, extraCandies int) []bool {
	var result = make([]bool, len(candies))
	var greatestNum int = candies[0]
	for i := 0; i < len(candies); i++ {
		if greatestNum < candies[i] {
			greatestNum = candies[i]
		}
	}
	for index, kid := range candies {
		if kid+extraCandies >= greatestNum {
			result[index] = true
		}
	}
	return result
}

'코딩 테스트 > LeetCode' 카테고리의 다른 글

Easy) 1512. Number of Good Pairs  (0) 2020.09.15
Easy) 1108. Defanging an IP Address  (0) 2020.09.15
Easy) 771. Jewels and Stones  (0) 2020.09.15
Easy) Shuffle the Array  (0) 2020.09.13
Easy. Running Sum of 1d Array  (0) 2020.07.02

+ Recent posts