코딩 테스트/leetcode

Count Pairs Whose Sum is Less than Target

ornni 2024. 6. 29. 10:00
728x90
반응형

첫번째 코드

 

인덱스 순서대로 더한 후target보다 작은 경우 answer에 1씩 더하는 방법으로 결과를 내면 된다!

 

class Solution:
    def countPairs(self, nums: List[int], target: int) -> int:
        answer = 0
        for i in range(len(nums)):
            for j in nums[i+1:]:
                if nums[i] + j < target:
                    answer += 1
        return answer

 

통과 :)


링크

https://github.com/ornni/leetcode/tree/main/2824-count-pairs-whose-sum-is-less-than-target

 

leetcode/2824-count-pairs-whose-sum-is-less-than-target at main · ornni/leetcode

Collection of LeetCode questions to ace the coding interview! - Created using [LeetHub v3](https://github.com/raphaelheinz/LeetHub-3.0) - ornni/leetcode

github.com

 

반응형