프라이D
프라이Develog(❁´◡`❁)
프라이D
전체 방문자
오늘
어제
  • ALL (378)
    • TDD, Cleancode with JavaScr.. (5)
    • 프로젝트 (32)
      • work (3)
      • 직접 만드는 기술 블로그 (2)
      • 데일리 옥션 (19)
      • 모락모락 (8)
    • Computer Science (1)
    • Algorithm & 자료구조 (94)
      • 알고리즘 w.JavaScript (53)
      • 자료구조 (5)
      • (인프런) 자바스크립트 알고리즘 문제풀이 (34)
    • JavaScript (45)
      • JavaScript (41)
      • 모던 자바스크립트 Deep Dive (4)
    • WEB (13)
    • 회고 (12)
    • TIL (109)
    • WIL (7)
    • Stacks (20)
      • React.js (6)
      • Next.js (1)
      • Redux (3)
      • Node.js (2)
      • GIT (2)
      • SAP (1)
    • 15일 메이킹 프로젝트 (15)
    • 이전 기록 (14)
    • ETC. (5)
    • ---------------2021 (6)
      • 내일배움단-웹개발 5주 (2)
      • 정보처리기사 (4)

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

인기 글

태그

  • 투포인터알고리즘
  • 내일배움카드
  • 스파르타코딩클럽
  • 코드스테이츠
  • 자바스크립트
  • 코딩프로젝트
  • JavaScript
  • vanilaJS
  • 내일배움단
  • nomadcoder
  • 알고리즘
  • Til
  • 모던자바스크립트딥다이브
  • 자바스크립트비트마스크
  • 2023 인프콘 후기
  • 비트마스크
  • nomadcoders
  • MySQL
  • 국비지원
  • 자바스크립트알고리즘

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
프라이D

프라이Develog(❁´◡`❁)

Algorithm & 자료구조/알고리즘 w.JavaScript

[Leetcode] 1431. Kids With the Greatest Number of Candies

2023. 12. 31. 20:37
 

Kids With the Greatest Number of Candies - LeetCode

Can you solve this real interview question? Kids With the Greatest Number of Candies - There are n kids with candies. You are given an integer array candies, where each candies[i] represents the number of candies the ith kid has, and an integer extraCandie

leetcode.com

There are n kids with candies. You are given an integer array candies, where each candies[i] represents the number of candies the ith kid has, and an integer extraCandies, denoting the number of extra candies that you have.
Return a boolean array result of length n, where result[i] is true if, after giving the ith kid all the extraCandies, they will have the greatest number of candies among all the kids, or false otherwise.
Note that multiple kids can have the greatest number of candies.
 /*
    [문제]
    - n명의 어린이가 캔디를 가지고 있다.
    - 각 캔디[i] 는 i 번째 어린이가 몇 개의 캔디를 갖고 있는지 나타낸다.
    - extraCandies 값은 내가 가진 캔디의 총량을 나타낸다.
    - n 개의 길이를 가진 boolean 배열 result 를 리턴하라
    - i번째 어린이에게 extraCandies 만큼을 주고 난뒤, 
    - 모든 어린이 중에서 가장 많은 캔디를 가지고 있으면 result[i]는 참이다.
    - 참고로 여러명의 어린이가 가장 많은 캔디를 가질 수 있다.
    => 그러니까, 모든 어린이에게 extraCandies 를 다 주는게 아니고,
    준다는 가정 하에 그 어린이가 다른 어린이보다 많은 갯수의 캔디를 가질 수 있느냐 없느냐를 보는 것.
  */ 
var kidsWithCandies = function(candies, extraCandies) {
  // 방법 1.
  // 반복문 돌며 각각 더하고, 나머지 값과 비교하는 것.
  // O(n)^2 의 시간 복잡도를 갖는다.

  // 방법 2.
  // 전체 배열에서 extraCandies 를 받기 전 최대값을 변수에 저장
  // 배열을 돌며 extraCandies 를 더하고 그 값보다 크면 true, 아니라면 false

  const max = Math.max(...candies);
  const result = candies.map((child) => {
    return child + extraCandies >= max;
  });

  return result;
};

 

추가된 풀이

var kidsWithCandies = function(candies, extraCandies) {
    const maxCandy = Math.max(...candies);
    return candies.map(candy => extraCandies+candy >= maxCandy)
};
  • 흠 여전히 똑같은 생각을 하는군...
저작자표시 (새창열림)

'Algorithm & 자료구조 > 알고리즘 w.JavaScript' 카테고리의 다른 글

[Leetcode] 345. Reverse Vowels of a String  (0) 2024.01.03
[Leetcode] 605. Can Place Flowers  (0) 2023.12.31
[Leetcode] 1071. Greatest Common Divisor of Strings  (0) 2023.12.28
[Leetcode] 1768.Merge Strings Alternately  (2) 2023.12.18
[알고리즘 JS] Sliding window - 중복이 없는 가장 긴 문자열의 길이 찾기  (0) 2023.03.24
    'Algorithm & 자료구조/알고리즘 w.JavaScript' 카테고리의 다른 글
    • [Leetcode] 345. Reverse Vowels of a String
    • [Leetcode] 605. Can Place Flowers
    • [Leetcode] 1071. Greatest Common Divisor of Strings
    • [Leetcode] 1768.Merge Strings Alternately
    프라이D
    프라이D
    틀린내용 정정 및 개선사항은 언제든지 댓글 달아주세요 :D

    티스토리툴바