반응형
https://leetcode.com/problems/arithmetic-slices-ii-subsequence
Arithmetic Slices II - Subsequence - LeetCode
Can you solve this real interview question? Arithmetic Slices II - Subsequence - Given an integer array nums, return the number of all the arithmetic subsequences of nums. A sequence of numbers is called arithmetic if it consists of at least three elements
leetcode.com
class Solution {
public:
unordered_map<int, int> dp[1001];
int numberOfArithmeticSlices(vector<int>& nums) {
int c = 0;
for(int i=1; i<nums.size(); i++) {
for(int j=0; j<i; j++) {
long diffLong = (long)nums[j] - nums[i];
if (diffLong < INT_MIN || diffLong > INT_MAX) continue;
int diff = diffLong;
int cnt = dp[j][diff];
dp[i][diff] += cnt + 1;
c += cnt;
}
}
return c;
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 1347. Minimum Number of Steps to Make Two Strings Anagram (0) | 2024.01.13 |
---|---|
LeetCode 938. Range Sum of BST (0) | 2024.01.08 |
LeetCode 2125. Number of Laser Beams in a Bank (0) | 2024.01.06 |
LeetCode 2870. Minimum Number of Operations to Make Array Empty (0) | 2024.01.04 |
LeetCode 2610. Convert an Array Into a 2D Array With Conditions (0) | 2024.01.02 |