[코테 풀이] Count Pairs Whose Sum is Less than Target

시내·2024년 6월 11일
0

Q_2824) Count Pairs Whose Sum is Less than Target

출처 : https://leetcode.com/problems/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.

class Solution {
    public int countPairs(List<Integer> nums, int target) {
        int sum = 0;
        for (int a = 0; a < nums.size() - 1; a++) {
            for (int b = a + 1; b < nums.size(); b++) {
                if (nums.get(a) + nums.get(b) < target) {
                    sum += 1;
                }
            }
        }
        return sum;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글