파비의 매일매일 공부기록

2023.04.02 Today's Challenge 본문

Problem Solving/LeetCode

2023.04.02 Today's Challenge

fabichoi 2023. 4. 2. 23:45

https://leetcode.com/problems/successful-pairs-of-spells-and-potions/

 

Successful Pairs of Spells and Potions - LeetCode

Can you solve this real interview question? Successful Pairs of Spells and Potions - You are given two positive integer arrays spells and potions, of length n and m respectively, where spells[i] represents the strength of the ith spell and potions[j] repre

leetcode.com

무식하게 풀면 바로 TL 나옴.
정렬도 했는 TL.
정렬 + 이분 탐색 해야 통과됨.

class Solution:
    def successfulPairs(self, spells: List[int], potions: List[int], success: int) -> List[int]:
        res = []
        l = len(potions)
        potions.sort()
        max_potion = potions[l-1]

        for spell in spells:
            min_potion = (success + spell - 1) // spell
            if min_potion > max_potion:
                res.append(0)
                continue
            idx = bisect.bisect_left(potions, min_potion)
            res.append(l - idx)          
              
        return res
반응형

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

2023.04.04 Today's Challenge  (0) 2023.04.04
2023.04.03 Today's Challenge  (0) 2023.04.03
2023.04.01 Today's Challenge  (0) 2023.04.01
2023.03.31 Today's Challenge  (0) 2023.03.31
2023.03.30 Today's Challenge  (0) 2023.03.30
Comments