From 7c60dee83b436e8e8a667bf9f191beced8de4fc3 Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Mon, 10 Mar 2025 00:36:13 -0600 Subject: chore: move cpp and rust code to directory --- other/arithmetic_slices2.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 other/arithmetic_slices2.cpp (limited to 'other/arithmetic_slices2.cpp') diff --git a/other/arithmetic_slices2.cpp b/other/arithmetic_slices2.cpp new file mode 100644 index 0000000..38668b7 --- /dev/null +++ b/other/arithmetic_slices2.cpp @@ -0,0 +1,35 @@ +#include "leetcode.h" + +class Solution { +public: + int numberOfArithmeticSlices(vector &nums) { + unordered_map> num_idx; + vvd dp(nums.size(), vector(nums.size())); + int ans = 0; + for (auto i = 0; i < nums.size(); ++i) + num_idx[nums[i]].push_back(i); + for (auto i = 0; i < nums.size(); ++i) { + for (int j = 0; j < i; j++) { + auto prev = 2l * nums[j] - nums[i]; + if (prev < INT_MIN || prev > INT_MAX) + continue; + auto it = num_idx.find(prev); + if (it != end(num_idx)) { + for (auto k : it->second) { + if (k >= j) + break; + dp[i][j] += dp[j][k] + 1; + } + } + ans += dp[i][j]; + } + } + return ans; + } +}; + +int main() { + Solution obj; + vector nums = {2, 4, 6, 8, 10}; + cout << obj.numberOfArithmeticSlices(nums); +} -- cgit v1.2.3