C++ DP using std::transform


  • 0
    G
    class Solution {
    public:
        int numberOfArithmeticSlices(vector<int>& A) 
        {
            if(A.size() < 3)
            {
                return (0);
            }
            
            vector<int> D(A.size(),1);
            
            D[1] = 2;
            int Res = 0;
            int Idx = 2;
            
            transform(A.begin() + 2,A.end(),D.begin() + 2,[&](int N)->int
            {
                N = 2;
                
                if(A[Idx] - A[Idx - 1] == A[Idx - 1] - A[Idx - 2])
                {
                    N = D[Idx - 1] + 1;
                    
                    if(N > 2)
                    {
                        Res += 1 + N - 3;
                    }
                }
                
                Idx++;
                return (N);
            });
            
            return (Res);
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.