Dynamic Programming Series
Dynamic Programming Series
Day 12
Longest Arithmetic Sequence
(Leetcode)
Problem Description
Given an array A of integers, return the length of the longest arithmetic subsequence in A.
Problem Description
Given an array A of integers, return the length of the longest arithmetic subsequence in A.
Example :
A=[1, 7, 10, 15, 27, 29]
Problem Description
[1, 7, 10, 15, 27, 29]
Given an array A of integers, return the length of the longest arithmetic subsequence in A.
Example :
A=[1, 7, 10, 15, 27, 29]
Problem Description
[1, 7, 10, 15, 27, 29]
Given an array A of integers, return the length of the longest arithmetic subsequence in A.
Example :
A=[1, 7, 10, 15, 27, 29]
14
14
Problem Description
[1, 7, 10, 15, 27, 29]
Given an array A of integers, return the length of the longest arithmetic subsequence in A.
Example :
A=[1, 7, 10, 15, 27, 29]
14
14
Example 2:
A = [9,4,7,2,10]
Problem Description
[1, 7, 10, 15, 27, 29]
Given an array A of integers, return the length of the longest arithmetic subsequence in A.
Example :
A=[1, 7, 10, 15, 27, 29]
14
14
Example 2:
A = [9,4,7,2,10]
[9,4,7,2,5]
How To approach this problem?
How To approach this problem?
let A = [9,4,7,2,10]
How To approach this problem?
let A = [9,4,7,2,10]
4 - 9 = -5
How To approach this problem?
7 - 9 = -2
4 - 9 = -5
let A = [9,4,7,2,10]
How To approach this problem?
7 - 9 = -2
4 - 9 = -5
let A = [9,4,7,2,10]
2 - 9 = -7
How To approach this problem?
7 - 9 = -2
4 - 9 = -5
let A = [9,4,7,2,10]
10 - 9 = 1
2 - 9 = -7
How To approach this problem?
7 - 9 = -2
4 - 9 = -5
let A = [9,4,7,2,10]
10 - 9 = 1
2 - 9 = -7
7 - 4 = 3
How To approach this problem?
7 - 9 = -2
4 - 9 = -5
let A = [9,4,7,2,10]
10 - 9 = 1
2 - 9 = -7
2 - 4 = -2
7 - 4 = 3
How To approach this problem?
7 - 9 = -2
4 - 9 = -5
let A = [9,4,7,2,10]
10 - 9 = 1
2 - 9 = -7
10 - 4 = 6
7 - 4 = 3
2 - 4 = -2
How To approach this problem?
7 - 9 = -2
4 - 9 = -5
let A = [9,4,7,2,10]
10 - 9 = 1
2 - 9 = -7
2 - 7 = -5
7 - 4 = 3
2 - 4 = -2
How To approach this problem?
7 - 9 = -2
4 - 9 = -5
let A = [9,4,7,2,10]
10 - 9 = 1
2 - 9 = -7
10 - 7 = 3
7 - 4 = 3
2 - 4 = -2
10 - 4 = 6
2 - 7 = -5
How To approach this problem?
7 - 9 = -2
4 - 9 = -5
let A = [9,4,7,2,10]
10 - 9 = 1
2 - 9 = -7
10 - 7 = 3
7 - 4 = 3
2 - 4 = -2
10 - 4 = 6
2 - 7 = -5
How To approach this problem?
Observations from this Example.
There will be two states in our DP solution
1. Difference
2. last value of A.P. with that difference
DP[val][diff] will contain the number of elements of A.P. whose last element is val and difference is diff.
Let us code the problem.
int longestArithSeqLength(vector<int>& A) {
int n = A.size();
if (n <= 2)
return n;
vector <unordered_map<int,int>> dp(n);
int max_len = 2;
for (int i = 0;i < n;i++)
{
for (int j = i+1;j < n;j++)
{
int diff = A[j] - A[i];
if (dp[i].find(diff) != dp[i].end())
{
dp[j][diff] = dp[i][diff] + 1;
}
else
{
dp[j][diff] = 2;
}
max_len = max(max_len, dp[j][diff]);
}
}
return max_len;
}So that's It for Day 12
So that's It for Day 12
Concepts that we have learnt
So that's It for Day 12
We have taken a very first step in Dynamic Programming.
Concepts that we have learnt
1. How to think of states in DP.
So that's It for Day 12
We have taken a very first step in Dynamic Programming.
Concepts that we have learnt
1. How to think of states in DP.
2. Use of optimal Substructure with memoization
So that's It for Day 12
We have taken a very first step in Dynamic Programming.
Concepts that we have learnt
1. How to think of states in DP.
2. Use of optimal Substructure with memoization
Still, Have any Doubts???
Still, Have any Doubts???
Comment it You will get a reply
OR
Send your doubt to email provided in description.
Copy of Longest Arithmetic Progression
By gauravsahu
Copy of Longest Arithmetic Progression
- 44