zoukankan      html  css  js  c++  java
  • 446. Arithmetic Slices II

    A sequence of numbers 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 sequences:

    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 subsequence slice of that array is any sequence of integers (P0, P1, ..., Pk) such that 0 ≤ P0 < P1 < ... < Pk < N.

    A subsequence slice (P0, P1, ..., Pk) of array A is called arithmetic if the sequence A[P0], A[P1], ..., A[Pk-1], A[Pk] is arithmetic. In particular, this means that k ≥ 2.

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

    The input contains N integers. Every integer is in the range of -231 and 231-1 and 0 ≤ N ≤ 1000. The output is guaranteed to be less than 231-1.

    Example:

    Input: [2, 4, 6, 8, 10]
    
    Output: 7
    
    Explanation:
    All arithmetic subsequence slices are:
    [2,4,6]
    [4,6,8]
    [6,8,10]
    [2,4,6,8]
    [4,6,8,10]
    [2,4,6,8,10]
    [2,6,10]

    Approach #1: DP. [C++]

    class Solution {
    public:
        int numberOfArithmeticSlices(vector<int>& A) {
            if (A.size() == 0) return 0;
            vector<map<int, int>> dp(A.size()+1);
            
            int res = 0;
            for (int i = 0; i < A.size(); ++i) {
                for (int j = 0; j < i; ++j) {
                    long dif = (long)A[i] - A[j];
                    if (dif < INT_MIN || dif > INT_MAX) continue;
                    int d = (int)dif;
                    dp[i][d] += 1;
                    if (dp[j].find(d) != dp[j].end()) {
                        dp[i][d] += dp[j][d];
                        res += dp[j][d];
                    }
                }
            }
            
            return res;
        }
    };
    

      

    Analysis:

    1. res is the final count of all valid  arithmetic subsequence slices;

    2. dp will store the intermediate results [i, [dif, count]], with i indexed into the array and dif as the key. count is the number of result with the intermediate results.

    3. for each index i, we find the total number of "generalized" arithmetic subsequence slices ending at it with all possible differences. This is done by attaching A[i] to all slices of dp[j][d] with j less than i.

    4. Within the inner loop, we first use a long variable diff to filter out invalid cases, then get the count of all valid slices (with element >= 3) as dp[j][d] add it to the final count. At last we update the count of all "generalized" slices for dp[i][d] by adding the two parts together: the orginal value of dp[i][d], the counts from dp[j][d].

    Reference:

    https://leetcode.com/problems/arithmetic-slices-ii-subsequence/

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    联想笔记本如何开启笔记本的VT-x虚拟化技术功能
    《Python数据分析》环境搭建之安装Jupyter工具(一)
    SQLite文件查看工具DB Browser for SQLite
    《Python操作SQLite3数据库》快速上手教程
    市面上各类网盘(百度网盘、腾讯微云、亿方云、坚果云等)对比 2016年10月
    Selenium安装失败WebDriverException: Message: 'gechodriver' executable needs to be in PATH
    安装Python环境时遇到的问题
    ORA-12541:TNS没有监听器
    PL/SQL连接Oracle数据库,中文乱码,显示问号
    Selenium 简单的例子
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10448218.html
Copyright © 2011-2022 走看看