← Home

1027. Longest Arithmetic Sequence

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
dynamic programmingC++Markdown
102

The trick here is to name the state correctly, then let the implementation follow. For 1027. Longest Arithmetic Sequence, the solution in this repository is mainly a dynamic programming solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: dynamic programming.

The notes already sitting in the source point us in the right direction:

  • https://leetcode.com/problems/longest-arithmetic-sequence/discuss/274611/JavaC%2B%2BPython-DP

Guide

When?

This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are longestArithSeqLength.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//https://leetcode.com/problems/longest-arithmetic-sequence/discuss/274611/JavaC%2B%2BPython-DP
02//Runtime: 3468 ms, faster than 17.64% of C++ online submissions for Longest Arithmetic Sequence.
03//Memory Usage: 192 MB, less than 86.67% of C++ online submissions for Longest Arithmetic Sequence.
04class Solution {
05public:
06    int longestArithSeqLength(vector<int>& A) {
07        unordered_map<int, unordered_map<int, int>> dp;
08        int ans = 0;
09        
10        for(int i = 0; i < A.size(); i++){
11            for(int j = i+1; j < A.size(); j++){
12                int d = A[j]-A[i];
13                //sequence length of d and ends at j
14                /*
15                when dp[d][i] not exist, 
16                dp[d][j] is first of such sequence, 
17                so its length is 2.
18                otherwise, we can concat j to the previous sequence ends at i
19                */
20                dp[d][j] = (dp[d].find(i) != dp[d].end()) ? dp[d][i]+1 : 2;
21                // cout << "j: " << j << ", [" << d << "][" << i << "]: ";
22                // if(dp[d].find(i) != dp[d].end()){
23                //     cout << dp[d][i] << endl;
24                // }else{
25                //     cout << "not exist" << endl;;
26                // }
27                ans = max(ans, dp[d][j]);
28            }
29        }
30        
31        return ans;
32    }
33};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.