{"id":9886,"date":"2022-11-21T15:45:24","date_gmt":"2022-11-21T23:45:24","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9886"},"modified":"2022-11-21T15:46:31","modified_gmt":"2022-11-21T23:46:31","slug":"leetcode-2475-number-of-unequal-triplets-in-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-2475-number-of-unequal-triplets-in-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2475. Number of Unequal Triplets in Array"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;array of positive integers&nbsp;<code>nums<\/code>. Find the number of triplets&nbsp;<code>(i, j, k)<\/code>&nbsp;that meet the following conditions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>0 &lt;= i &lt; j &lt; k &lt; nums.length<\/code><\/li><li><code>nums[i]<\/code>,&nbsp;<code>nums[j]<\/code>, and&nbsp;<code>nums[k]<\/code>&nbsp;are&nbsp;<strong>pairwise distinct<\/strong>.<ul><li>In other words,&nbsp;<code>nums[i] != nums[j]<\/code>,&nbsp;<code>nums[i] != nums[k]<\/code>, and&nbsp;<code>nums[j] != nums[k]<\/code>.<\/li><\/ul><\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the number of triplets that meet the conditions.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [4,4,2,4,3]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> The following triplets meet the conditions:\n- (0, 2, 4) because 4 != 2 != 3\n- (1, 2, 4) because 4 != 2 != 3\n- (2, 3, 4) because 2 != 4 != 3\nSince there are 3 triplets, we return 3.\nNote that (2, 0, 4) is not a valid triplet because 2 &gt; 0.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,1,1,1,1]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> No triplets meet the conditions so we return 0.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>3 &lt;= nums.length &lt;= 100<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 1000<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Brute Force<\/strong><\/h2>\n\n\n\n<p>Enumerate i, j, k.<\/p>\n\n\n\n<p>Time complexity: O(n<sup>3<\/sup>)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int unequalTriplets(vector<int>& nums) {\n    int ans = 0;\n    const int n = nums.size();\n    for (int i = 0; i < n; ++i)\n      for (int j = i + 1; j < n; ++j)\n        for (int k = j + 1; k < n; ++k)\n          if (nums[i] != nums[j] &#038;&#038; nums[j] != nums[k] &#038;&#038; nums[i] != nums[k])\n            ++ans;\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;array of positive integers&nbsp;nums. Find the number of triplets&nbsp;(i, j, k)&nbsp;that meet the following conditions: 0 &lt;= i &lt; j &lt; k&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,222,82],"class_list":["post-9886","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","tag-hashtable","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9886","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9886"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9886\/revisions"}],"predecessor-version":[{"id":9888,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9886\/revisions\/9888"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}