본문 바로가기
python-algorithm

leetcode 2824. Count Pairs Whose Sum is Less than Target

by 무적김두칠 2024. 1. 2.

https://leetcode.com/problems/count-pairs-whose-sum-is-less-than-target/description/

 

Count Pairs Whose Sum is Less than Target - LeetCode

Can you solve this real interview question? Count Pairs Whose Sum is Less than Target - Given a 0-indexed integer array nums of length n and an integer target, return the number of pairs (i, j) where 0 <= i < j < n and nums[i] + nums[j] < target.   Exampl

leetcode.com

 

1
2
3
4
5
6
7
8
9
10
class Solution:
    def countPairs(self, nums: List[int], target: int-> int:
        answer = 0
        for i in range(len(nums)):
            for j in range(i+1len(nums)):
                if nums[i] + nums[j] < target:
                    answer += 1
 
        return answer
        
cs
반응형

댓글