aboutsummaryrefslogtreecommitdiff
path: root/other/arithmetic_slices2.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
committerJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
commit7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch)
treeeb7a24ee2067f92245c57bb118e85a1baf15df50 /other/arithmetic_slices2.cpp
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'other/arithmetic_slices2.cpp')
-rw-r--r--other/arithmetic_slices2.cpp35
1 files changed, 35 insertions, 0 deletions
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<int> &nums) {
+ unordered_map<int, vector<int>> num_idx;
+ vvd dp(nums.size(), vector<int>(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<int> nums = {2, 4, 6, 8, 10};
+ cout << obj.numberOfArithmeticSlices(nums);
+}