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:
parent
96f59f9f54
commit
80ae33d3d0
1 changed files with 22 additions and 0 deletions
22
cs/count-good-triplets.cs
Normal file
22
cs/count-good-triplets.cs
Normal 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();
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue