[LeetCode] 2558. Take Gifts From the Richest Pile (Python)

2024. 12. 12. 19:46·알고리즘/LeetCode

 

난이도: Easy

 

문제 설명


You are given an integer array gifts denoting the number of gifts in various piles. Every second, you do the following:

  • Choose the pile with the maximum number of gifts.
  • If there is more than one pile with the maximum number of gifts, choose any.
  • Leave behind the floor of the square root of the number of gifts in the pile. Take the rest of the gifts.

Return the number of gifts remaining after k seconds.

 

문제 예제


Example 1:

Input: gifts = [25,64,9,4,100], k = 4
Output: 29
Explanation:
The gifts are taken in the following way:
- In the first second, the last pile is chosen and 10 gifts are left behind.
- Then the second pile is chosen and 8 gifts are left behind.
- After that the first pile is chosen and 5 gifts are left behind.
- Finally, the last pile is chosen again and 3 gifts are left behind.
The final remaining gifts are [5,8,9,4,3], so the total number of gifts remaining is 29.

Example 2:

Input: gifts = [1,1,1,1], k = 4
Output: 4
Explanation:
In this case, regardless which pile you choose, you have to leave behind 1 gift in each pile.
That is, you cant take any pile with you.
So, the total gifts remaining are 4.

 

제한 사항


  • 1 <= gifts.length <= 10^3
  • 1 <= gifts[i] <= 10^9
  • 1 <= k <= 10^3

 

✏️Solution(솔루션)


class Solution(object):
    def pickGifts(self, gifts, k):
        gifts = [-gift for gift in gifts]
        heapq.heapify(gifts)

        for _ in range(k):
            max_gift = heapq.heappop(gifts)
            heapq.heappush(gifts, -floor(sqrt(-max_gift)))
        
        return int(-sum(gifts))

 

문제만 잘 이해하면 별로 어렵지 않은 문제인 것 같다.

 

결국에는 gifts에서 max값을 뽑아 square root를 취해 내림하는데 이 작업을 k번 하면된다.

 

모든 작업이 끝났을 때 gifts의 전체 요소 합을 return 하면 정답을 맞출 수 있다.

 

풀 수 있는 다양한 방법이 있겠지만, 나는 heapq를 사용해 제일 큰 값을 뽑고 그 값에 필요한 연산을 취한다음 다시 heapq에 넣는 방식으로 문제를 해결했다.

 


2558. Take Gifts From the Richest Pile

'알고리즘 > LeetCode' 카테고리의 다른 글

[LeetCode] 1945. Sum of Digits of String After Convert (Python)  (2) 2024.12.14
[LeetCode] 2593. Find Score of an Array After Marking All Elements (Python)  (0) 2024.12.13
[LeetCode] 2779. Maximum Beauty of an Array After Applying Operation (Python)  (0) 2024.12.11
[LeetCode] 2981. Find Longest Special Substring That Occurs Thrice I (Python)  (0) 2024.12.10
[LeetCode] 3152. Special Array II (Python)  (2) 2024.12.09
'알고리즘/LeetCode' 카테고리의 다른 글
  • [LeetCode] 1945. Sum of Digits of String After Convert (Python)
  • [LeetCode] 2593. Find Score of an Array After Marking All Elements (Python)
  • [LeetCode] 2779. Maximum Beauty of an Array After Applying Operation (Python)
  • [LeetCode] 2981. Find Longest Special Substring That Occurs Thrice I (Python)
Laewon Jeong
Laewon Jeong
  • Laewon Jeong
    래원
    Laewon Jeong
    글쓰기 | 관리
  • GitHub

    • github.com/laewonJeong
  • 전체
    오늘
    어제
    • 분류 전체보기 (172)
      • Docker 및 Kubernetes (11)
        • Docker (5)
        • Kubernetes (6)
      • Data Engineering (18)
        • Hadoop (5)
        • Spark (5)
        • Kafka (5)
        • Airflow (3)
      • CI|CD (3)
      • 알고리즘 (136)
        • 알고리즘 (2)
        • LeetCode (118)
        • 프로그래머스 (11)
        • BOJ (1)
        • 코딩테스트 대비 (4)
      • 서버 (2)
        • 미니 서버 (2)
      • 잡담 (1)
  • 태그

    Python
    알고리즘
    프로그래머스
    코딩테스트
    문자열
    아파치 스파크
    그래프
    docker
    programmers
    String
    파이썬
    리트코드
    DP
    Kubernetes
    누적합
    heapq
    이진탐색
    쿠버네티스
    우선순위큐
    분산
    도커
    분산처리
    leetcode
    오블완
    BFS
    아파치 하둡
    백트래킹
    dfs
    티스토리챌린지
    Apache Spark
  • 인기 글

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Laewon Jeong
[LeetCode] 2558. Take Gifts From the Richest Pile (Python)
상단으로

티스토리툴바