首页 > 其他分享 >[LeetCode] 2475. Number of Unequal Triplets in Array

[LeetCode] 2475. Number of Unequal Triplets in Array

时间:2023-06-13 13:47:21浏览次数:40  
标签:2475 because Unequal nums int 元素 Number 三元组 triplets

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.

Example 1:

Input: nums = [4,4,2,4,3]
Output: 3
Explanation: The following triplets meet the conditions:
- (0, 2, 4) because 4 != 2 != 3
- (1, 2, 4) because 4 != 2 != 3
- (2, 3, 4) because 2 != 4 != 3
Since there are 3 triplets, we return 3.
Note that (2, 0, 4) is not a valid triplet because 2 > 0.

Example 2:

Input: nums = [1,1,1,1,1]
Output: 0
Explanation: No triplets meet the conditions so we return 0.

Constraints:

  • 3 <= nums.length <= 100
  • 1 <= nums[i] <= 1000

数组中不等三元组的数目。

给你一个下标从 0 开始的正整数数组 nums 。请你找出并统计满足下述条件的三元组 (i, j, k) 的数目:

0 <= i < j < k < nums.length
nums[i]、nums[j] 和 nums[k] 两两不同 。
换句话说:nums[i] != nums[j]、nums[i] != nums[k] 且 nums[j] != nums[k] 。
返回满足上述条件三元组的数目。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/number-of-unequal-triplets-in-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这道题有多种思路,这里我提供一个次优解。

注意题目的数据范围,这道题可以用暴力解做,时间复杂度是O(n^3)。

我的思路是 hashmap + 排序。分如下几步

  • 用 hashmap 统计 input 数组中每个不同元素分别出现了几次
  • 对 input 数组排序
  • 再次扫描 input 数组,以当前扫描到的元素 nums[i] 为中间元素所组成的三元组的数量 = nums[i] 左边元素的个数 * nums[i] 的个数 * 剩下元素的个数

时间O(n^2)

空间O(n)

Java实现

 1 class Solution {
 2     public int unequalTriplets(int[] nums) {
 3         HashMap<Integer, Integer> map = new HashMap<>();
 4         for (int num : nums) {
 5             map.put(num, map.getOrDefault(num, 0) + 1);
 6         }
 7         
 8         int res = 0;
 9         Arrays.sort(nums);
10         int n = nums.length;
11         // i 指向中间的元素的第一个位置
12         // [0, ......, i, ....., i + j, .....,n - 1]
13         int i = 0;
14         while (i < nums.length) {
15             int j = map.get(nums[i]);
16             // 0 - i这一段是左边元素
17             // i - i + j这一段是中间元素
18             // i + j到最后是右边元素
19             res += i * j * (n - i - j);
20             i += j;
21         }
22         return res;
23     }
24 }

 

LeetCode 题目总结

标签:2475,because,Unequal,nums,int,元素,Number,三元组,triplets
From: https://www.cnblogs.com/cnoodle/p/17477265.html

相关文章

  • 力扣---2475. 数组中不等三元组的数目
    给你一个下标从0开始的正整数数组nums。请你找出并统计满足下述条件的三元组(i,j,k)的数目:0<=i<j<k<nums.lengthnums[i]、nums[j]和nums[k]两两不同。换句话说:nums[i]!=nums[j]、nums[i]!=nums[k]且nums[j]!=nums[k]。返回满足上述条件三元组的数目......
  • oracle中rownum和row_number()
     oracle中rownum和row_number() row_number()over(partitionbycol1orderbycol2)表示根据col1分组,在分组内部根据col2排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的)。与rownum的区别在于:使用rownum进行排序的时候是先对结果集加入伪劣rownum然后......
  • Educational Codeforces Round 7-D. Optimal Number Permutation
    原题链接D.OptimalNumberPermutationtimelimitpertestmemorylimitpertestinputoutputYouhavearray a thatcontainsallintegersfrom 1 to n twice.Youcanarbi......
  • Codeforces Round #232 (Div. 2)-B. On Corruption and Numbers
    原题链接B.OnCorruptionandNumberstimelimitpertestmemorylimitpertestinputoutputAlexey,amerryBerlandentrant,gotsickofthegrayrealityandhezealouslywa......
  • HDU 1394 Minimum Inversion Number(树状数组)
    题意:有一个n个整数的排列,这n个整数就是0,1,2,3...n-1这n个数(但不一定按这个顺序给出)。现在先计算一下初始排列的逆序数,然后把第一个元素a1放到an后面去,形成新排列a2a3a4...ana1,然后再求这个排列的逆序数。继续执行类似操作(一共要执行n-1次)直到产生排列ana1a2...an-1为止。......
  • Palindrome Number
    Givenanintegerx,returntrueifxisapalindrome,andfalseotherwise.Example1:Input:x=121Output:trueExplanation:121readsas121fromlefttorightandfromrighttoleft.Example2:Input:x=-121Output:falseExplanation:Fromleftto......
  • django 中存储手机号的字段, 使用 Django 库 pip install django-phonenumber-field[ph
    原文参见:https://www.delftstack.com/zh/howto/django/django-phone-number-field/使用第三方Django应用程序的 PhoneNumberField 存储电话号码要存储电话号码,我们可以使用实现此字段的第三方Django应用程序或库:PhoneNumberField。你可以在此处找到此库或应用程序的Git......
  • 遇到chrome_options.add_experimental_option ("debuggerAddress", port_number)调起
    1、查看谷歌版本和chromedriver版本是否一致:手动查找ChromeDriver路径。在终端中输入以下命令:whichchromedriver这将输出ChromeDriver的路径,例如:/usr/local/bin/chromedriver可以在Chrome浏览器中输入以下网址来查看版本信息: chrome://version/在命令行中,你可以......
  • 微信小程序开发笔记 进阶篇⑤——getPhoneNumber 获取用户手机号码(基础库 2.21.2 之前
    文章目录一、前言二、前端代码wxml三、前端代码js四、后端java五、程序流程六、参考一、前言微信小程序开发笔记——导读大部分微信小程序开发者都会有这样的需求:获取小程序用户的手机号码。但是,因为小程序用户的手机号码属于重要信息,为了安全,所以需要如下一系列较为复杂的方法和......
  • 微信小程序开发笔记 进阶篇⑥——getPhoneNumber 获取用户手机号码(基础库 2.21.2 之后
    文章目录一、前言二、前端代码wxml三、前端代码js四、后端java五、程序流程六、参考一、前言微信小程序开发笔记——导读大部分微信小程序开发者都会有这样的需求:获取小程序用户的手机号码。但是,因为小程序用户的手机号码属于重要信息,为了安全,所以需要如下一系列较为复杂的方法和......