[LeetCode] Arithmetic Slices

Posted immjc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode] Arithmetic Slices相关的知识,希望对你有一定的参考价值。

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.

For example, these are arithmetic sequence:

1, 3, 5, 7, 9
7, 7, 7, 7
3, -1, -5, -9

The following sequence is not arithmetic.

1, 1, 2, 5, 7

 

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.

Example:

A = [1, 2, 3, 4]

return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.

找出一个数组中的子分片并统计。

思路:也就是蛮力算法,找出每一个子分片并判断。时间复杂度O(n^3)。

代码中注释了造成超时的表达式。

class Solution {
public:
    int numberOfArithmeticSlices(vector<int>& A)
    {
        int res = 0;
        if (A.size() < 3)
            return res;
        for (int i = 0; i < A.size() - 2; i++)
        {
            for (int j = i + 2; j < A.size(); j++)
            {
                int cnt = 0;
                int tmp = A[i + 1] - A[i];
                for (int k = i + 1; k <= j; k++)
                {
                    /* Using this expression causes TLE  
                    if (A[k] - A[k - 1] == tmp)
                        cnt++;
                    */
                    if (A[k] - A[k - 1] != tmp)
                        break;
                    cnt++;
                }
                if (cnt == j - i)
                    res++;
            }
        }
        return res;
    }
};
// 84 ms

第一次使用了注释的代码,结果造成TLE。通过验证得出算法没问题,就是时间复杂度极高导致不能AC

第二次使用break提前挑出循环,减少了一丢丢计算量。成功AC

———————————————————————————————————————————————————————

通过了解蛮力算法过程,确实重复了太多次判断,感觉使用DP也可以求解。下次想通了再更新~~


以上是关于[LeetCode] Arithmetic Slices的主要内容,如果未能解决你的问题,请参考以下文章

leetcode 413. Arithmetic Slices 等差数列划分

[LeetCode]413 Arithmetic Slices

Leetcode: Arithmetic Slices

[leetcode-413-Arithmetic Slices]

413. Arithmetic Slices(LeetCode)

[LeetCode] Arithmetic Slices