파비의 매일매일 공부기록

Today's Challenge 본문

Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 6. 24. 23:45

https://leetcode.com/problems/construct-target-array-with-multiple-sums/

 

Construct Target Array With Multiple Sums - 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

우선순위 큐를 사용해서 값을 하나씩 줄여가면 된다.
해설을 보고도 처음엔 뭔 말인지를 이해 못했네 =_=

class Solution:
    def isPossible(self, target: List[int]) -> bool:
        heapq._heapify_max(target)
        s = sum(target)
        
        while target[0] != 1:
            sub = s - target[0]
            if sub == 0:
                return False
            
            n = max((target[0] - 1) // sub, 1)
            s -= n * sub
            
            target0 = target[0] - n * sub
            if target0 < 1:
                return False
            
            heapq._heapreplace_max(target, target0)
        
        return True
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

Today's Challenge  (0) 2022.06.26
Today's Challenge  (0) 2022.06.25
Today's Challenge  (0) 2022.06.23
Today's Challenge  (0) 2022.06.22
Today's Challenge  (0) 2022.06.21
Comments