[코테 풀이] Number of Unequal Triplets in Array

시내·2024년 7월 28일
0

Q_2475) Number of Unequal Triplets in Array

출처 : https://leetcode.com/problems/number-of-unequal-triplets-in-array/

You are given a 0-indexed array of positive integers nums. Find the number of triplets (i, j, k) that meet the following conditions:

  • 0 <= i < j < k < nums.length

  • nums[i], nums[j], and nums[k] are pairwise distinct.

  • In other words, nums[i] != nums[j], nums[i] != nums[k], and nums[j] != nums[k].

Return the number of triplets that meet the conditions.

class Solution {
    public int unequalTriplets(int[] nums) {
        int count = 0;
        for (int i = 0; i < nums.length - 2; i++) {
            for (int j = i + 1; j < nums.length - 1; j++) {
                if (nums[i] != nums[j]) {
                    for (int k = j + 1; k < nums.length; k++) {
                        if (nums[j] != nums[k] && nums[i] != nums[k]) {
                            count++;
                        }
                    }
                }
            }
        }
        return count;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글