Skip to content

Commit a18790a

Browse files
committed
feat: add solutions to lc problem: No.2475
No.2475.Number of Unequal Triplets in Array
1 parent f82cd38 commit a18790a

File tree

4 files changed

+187
-0
lines changed

4 files changed

+187
-0
lines changed

Diff for: solution/2400-2499/2475.Number of Unequal Triplets in Array/README.md

+77
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,84 @@ func unequalTriplets(nums []int) (ans int) {
301301
### **TypeScript**
302302

303303
```ts
304+
function unequalTriplets(nums: number[]): number {
305+
const n = nums.length;
306+
let ans = 0;
307+
for (let i = 0; i < n - 2; i++) {
308+
for (let j = i + 1; j < n - 1; j++) {
309+
for (let k = j + 1; k < n; k++) {
310+
if (
311+
nums[i] !== nums[j] &&
312+
nums[j] !== nums[k] &&
313+
nums[i] !== nums[k]
314+
) {
315+
ans++;
316+
}
317+
}
318+
}
319+
}
320+
return ans;
321+
}
322+
```
323+
324+
```ts
325+
function unequalTriplets(nums: number[]): number {
326+
const n = nums.length;
327+
const cnt = new Map<number, number>();
328+
for (const num of nums) {
329+
cnt.set(num, (cnt.get(num) ?? 0) + 1);
330+
}
331+
let ans = 0;
332+
let a = 0;
333+
for (const b of cnt.values()) {
334+
const c = n - a - b;
335+
ans += a * b * c;
336+
a += b;
337+
}
338+
return ans;
339+
}
340+
```
304341

342+
### **Rust**
343+
344+
```rust
345+
impl Solution {
346+
pub fn unequal_triplets(nums: Vec<i32>) -> i32 {
347+
let n = nums.len();
348+
let mut ans = 0;
349+
for i in 0..n - 2 {
350+
for j in i + 1..n - 1 {
351+
for k in j + 1..n {
352+
if nums[i] != nums[j] && nums[j] != nums[k] && nums[i] != nums[k] {
353+
ans += 1;
354+
}
355+
}
356+
}
357+
}
358+
ans
359+
}
360+
}
361+
```
362+
363+
```rust
364+
use std::collections::HashMap;
365+
impl Solution {
366+
pub fn unequal_triplets(nums: Vec<i32>) -> i32 {
367+
let mut cnt = HashMap::new();
368+
for num in nums.iter() {
369+
*cnt.entry(num).or_insert(0) += 1;
370+
}
371+
let n = nums.len();
372+
let mut ans = 0;
373+
let mut a = 0;
374+
for v in cnt.values() {
375+
let b = n - a - v;
376+
ans += v * a * b;;
377+
a += v;
378+
}
379+
ans as i32
380+
}
381+
}
305382
```
306383

307384
### **...**

Diff for: solution/2400-2499/2475.Number of Unequal Triplets in Array/README_EN.md

+77
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,84 @@ func unequalTriplets(nums []int) (ans int) {
269269
### **TypeScript**
270270

271271
```ts
272+
function unequalTriplets(nums: number[]): number {
273+
const n = nums.length;
274+
let ans = 0;
275+
for (let i = 0; i < n - 2; i++) {
276+
for (let j = i + 1; j < n - 1; j++) {
277+
for (let k = j + 1; k < n; k++) {
278+
if (
279+
nums[i] !== nums[j] &&
280+
nums[j] !== nums[k] &&
281+
nums[i] !== nums[k]
282+
) {
283+
ans++;
284+
}
285+
}
286+
}
287+
}
288+
return ans;
289+
}
290+
```
291+
292+
```ts
293+
function unequalTriplets(nums: number[]): number {
294+
const n = nums.length;
295+
const cnt = new Map<number, number>();
296+
for (const num of nums) {
297+
cnt.set(num, (cnt.get(num) ?? 0) + 1);
298+
}
299+
let ans = 0;
300+
let a = 0;
301+
for (const b of cnt.values()) {
302+
const c = n - a - b;
303+
ans += a * b * c;
304+
a += b;
305+
}
306+
return ans;
307+
}
308+
```
272309

310+
### **Rust**
311+
312+
```rust
313+
impl Solution {
314+
pub fn unequal_triplets(nums: Vec<i32>) -> i32 {
315+
let n = nums.len();
316+
let mut ans = 0;
317+
for i in 0..n - 2 {
318+
for j in i + 1..n - 1 {
319+
for k in j + 1..n {
320+
if nums[i] != nums[j] && nums[j] != nums[k] && nums[i] != nums[k] {
321+
ans += 1;
322+
}
323+
}
324+
}
325+
}
326+
ans
327+
}
328+
}
329+
```
330+
331+
```rust
332+
use std::collections::HashMap;
333+
impl Solution {
334+
pub fn unequal_triplets(nums: Vec<i32>) -> i32 {
335+
let mut cnt = HashMap::new();
336+
for num in nums.iter() {
337+
*cnt.entry(num).or_insert(0) += 1;
338+
}
339+
let n = nums.len();
340+
let mut ans = 0;
341+
let mut a = 0;
342+
for v in cnt.values() {
343+
let b = n - a - v;
344+
ans += v * a * b;;
345+
a += v;
346+
}
347+
ans as i32
348+
}
349+
}
273350
```
274351

275352
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use std::collections::HashMap;
2+
impl Solution {
3+
pub fn unequal_triplets(nums: Vec<i32>) -> i32 {
4+
let mut cnt = HashMap::new();
5+
for num in nums.iter() {
6+
*cnt.entry(num).or_insert(0) += 1;
7+
}
8+
let n = nums.len();
9+
let mut ans = 0;
10+
let mut a = 0;
11+
for v in cnt.values() {
12+
let b = n - a - v;
13+
ans += v * a * b;
14+
a += v;
15+
}
16+
ans as i32
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function unequalTriplets(nums: number[]): number {
2+
const n = nums.length;
3+
const cnt = new Map<number, number>();
4+
for (const num of nums) {
5+
cnt.set(num, (cnt.get(num) ?? 0) + 1);
6+
}
7+
let ans = 0;
8+
let a = 0;
9+
for (const b of cnt.values()) {
10+
const c = n - a - b;
11+
ans += a * b * c;
12+
a += b;
13+
}
14+
return ans;
15+
}

0 commit comments

Comments
 (0)