[LeetCode] 2824. Count Pairs Whose Sum is Less than Target

Chobby·4일 전

LeetCode

목록 보기
802/826

😎풀이

  1. nums를 2중 순회
    1-1. nums[i]nums[j]의 합이 target미만인 경우 카운트
  2. 카운트 반환
function countPairs(nums: number[], target: number): number {
    const n = nums.length
    let pairs = 0
    for(let i = 0; i < n - 1; i ++) {
        for(let j = i + 1; j < n; j++) {
            if(nums[i] + nums[j] >= target) continue
            pairs++
        }
    }
    return pairs
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글