[LeetCode] 1431. Kids With the Greatest Number of Candies, Easy

2025. 4. 15. 13:08·CodingTest/LeetCode
반응형

1. 문제

  • n명의 학생이 가지고 있는 사탕의 수를 나타내는 배열 candies와 남은 사탕 extraCandies가 주어질 때, 각 학생에게 extraCandies를 준 뒤 학생들 중 가장 많은 사탕의 수이면 true를 담고 아니면 false를 담아 반환하라.

2. 해결

function kidsWithCandies(candies: number[], extraCandies: number): boolean[] {
    let max = Number.MIN_SAFE_INTEGER;

    const ans = []
    for(const candy of candies) max = Math.max(max, candy);

    for(const candy of candies) {
        if(candy + extraCandies >= max) ans.push(true)
        else ans.push(false)
    }

    return ans;
};
  • 학생들이 가지고 있는 가장 많은 사탕의 수를 구한다.
  • 각 학생들의 사탕 수 + 나머지 사탕 수를 더한 뒤, 앞서 구한 max값보다 크거나 같으면 true를 담고 아니면 false를 담는다.
저작자표시 (새창열림)
'CodingTest/LeetCode' 카테고리의 다른 글
  • [LeetCode] 345. Reverse Vowels of a String, Easy
  • [LeetCode] 605. Can Place Flowers, Easy
  • [LeetCode] 1071. Greatest Common Divisor of Strings, Easy
  • [LeetCode] 1768. Merge Strings Alternately
뜸부깅
뜸부깅
코딩에 대한 여러 개인적인 생각을 정리하고 공부를 하는 공간입니다!!
  • 뜸부깅
    코오오딩
    뜸부깅
  • 전체
    오늘
    어제
    • Note (429)
      • Skill (31)
        • Java & Spring (9)
        • Javascript & HTML & CSS (0)
        • React (0)
        • Next.js (22)
      • CodingTest (389)
        • 백준 온라인 저지(BOJ) (140)
        • 프로그래머스(Programmers) (79)
        • LeetCode (170)
      • Algorithm & Data Structure (6)
      • [Project] 포트폴리오 (3)
        • Front end (3)
        • Back end (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    자바
    component-scan
    boj2108
    백준7576
    알고리즘
    Java
    leetcode 2236
    BOJ
    백준7576자바
    TypeScript
    프로그래머스
    백준1260
    백준1427
    next 14
    boj1427
    meidum
    medium
    백준2751
    Easy
    백준
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
뜸부깅
[LeetCode] 1431. Kids With the Greatest Number of Candies, Easy
상단으로

티스토리툴바