본문 바로가기
코딩 테스트/leetcode

Count Pairs Whose Sum is Less than Target

by ornni 2024. 6. 29.
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

 

반응형

'코딩 테스트 > leetcode' 카테고리의 다른 글

Delete Greatest Value in Each Row  (0) 2024.07.15
Number of Good Pairs  (0) 2024.07.12
Neither Minimum nor Maximum  (0) 2024.06.28
Final Prices With a Special Discount in a Shop  (0) 2024.06.27
Removing Stars From a String  (0) 2024.06.26