URL: https://leetcode.com/problems/count-good-triplets/ Signed-off-by: Matej Focko <me@mfocko.xyz>
22 lines
726 B
C#
22 lines
726 B
C#
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();
|
|
}
|