cs: add «1534. Count Good Triplets»

URL:	https://leetcode.com/problems/count-good-triplets/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2025-04-14 13:06:39 +02:00
parent 96f59f9f54
commit 80ae33d3d0
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

22
cs/count-good-triplets.cs Normal file
View file

@ -0,0 +1,22 @@
public class Solution {
private IEnumerable<(int, int, int)> Triplets(int n) =>
Enumerable.Range(0, n).SelectMany(
i => Enumerable.Range(i + 1, n - i - 1).SelectMany(
j => Enumerable.Range(j + 1, n - j - 1).Select(
k => (i, j, k)
)
)
);
public int CountGoodTriplets(int[] arr, int a, int b, int c) =>
Triplets(arr.Length)
.Select(idxs => {
var (i, j, k) = idxs;
return (arr[i], arr[j], arr[k]);
})
.Where(nums => {
var (x, y, z) = nums;
return Math.Abs(x - y) <= a && Math.Abs(y - z) <= b && Math.Abs(x - z) <= c;
})
.Count();
}