• 54.7%

https://leetcode.com/problems/arithmetic-slices/?tab=Description

A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.

A zero-indexed array A consisting of N numbers is given. A slice of that array is any pair of integers (P, Q) such that 0 <= P < Q < N.

A slice (P, Q) of array A is called arithmetic if the sequence:

A[P], A[p + 1], …, A[Q - 1], A[Q] is arithmetic. In particular, this means that P + 1 < Q.

The function should return the number of arithmetic slices in the array A.

#### cpp

https://discuss.leetcode.com/topic/62992/3ms-c-standard-dp-solution-with-very-detailed-explanation

3ms C++ Standard DP Solution with Very Detailed Explanation

https://discuss.leetcode.com/topic/62162/3ms-question-maker-solution-in-cpp-o-n-time-and-in-space

3ms Question Maker Solution in CPP O(n) time and in space

I met this question in friend’s friend’s OA (recursive friend) lol

It could be solved in O(n) with small math trick~~ O(n) time and in space. Thanks @JianShi for this solution!

https://discuss.leetcode.com/topic/62162/3ms-question-maker-solution-in-cpp-o-n-time-and-in-space/3

@codeRiya Hi, thank you for your question!

As I said, this solution uses a small math trick.

At first, it change A to an array of difference of adjacent items in original A.

For example, if A is [1,2,3,4], then A should be [1,1,1]. So for adjusted A, 2 or more continuous same values indicates there is an arithmetic slice in original A. If there is N continuous same values in adjusted A, there should be an arithmetic slice with length of N + 1.

Now let me show you how many arithmetic slices in a N + 1 long arithmetic slice.

If you have more question, please let me know~ cheers! :P

#### python

https://discuss.leetcode.com/topic/63873/python-dp-solution

Python DP solution

https://discuss.leetcode.com/topic/63873/python-dp-solution/2

@zhan430 Why not use a variable instead of an array?

#### java

https://discuss.leetcode.com/topic/63302/simple-java-solution-9-lines-2ms

Simple Java solution 9 lines, 2ms