add: 230331-CN

This commit is contained in:
Eatswap 2023-03-31 18:17:13 +08:00
parent 60316eaccc
commit c16d1460c7
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 25 additions and 1 deletions

24
cpp/2303/230331-CN.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <unordered_set>
#include <vector>
#include <algorithm>
/**
* 2367. Number of Arithmetic Triplets
*
* You are given a 0-indexed, strictly increasing integer array nums and a positive integer diff. A triplet (i, j, k) is an arithmetic triplet if the following conditions are met:
*
* i < j < k,
* nums[j] - nums[i] == diff, and
* nums[k] - nums[j] == diff.
* Return the number of unique arithmetic triplets.
*/
class Solution {
public:
static int arithmeticTriplets(const std::vector<int>& nums, int diff) {
std::unordered_set<int> s(nums.begin(), nums.end());
return std::count_if(nums.begin(), nums.end(), [&](int x) {
return s.count(x + diff) && s.count(x + 2 * diff);
});
}
};

View File

@ -3,4 +3,4 @@ PROJECT(2303)
SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2303 230330-CN.cpp)
ADD_EXECUTABLE(2303 230331-CN.cpp)