Skip to content

Commit 3aa5b55

Browse files
author
Partho Biswas
committed
1027_Longest_Arithmetic_Sequence
1 parent bb25a46 commit 3aa5b55

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
701701
|48| **[1320. Minimum Distance to Type a Word Using Two Fingers](https://tinyurl.com/ur876a6)** | [Python](https://tinyurl.com/wu6rdaw/1320_Minimum_Distance_to_Type_a_Word_Using_Two_Fingers.py), [Swift](https://tinyurl.com/wuja3c4/1320_Minimum_Distance_to_Type_a_Word_Using_Two_Fingers.swift)| [Vid 1](https://tinyurl.com/u5fcb3z), [Art 1](https://tinyurl.com/w386adm), [Art 2](https://tinyurl.com/rnkbhp3), [Art 3](https://tinyurl.com/tqwgraz), [Art 4](https://tinyurl.com/v46fuzm) | Hard | **Important. TODO: Check again** |
702702
|49| **[688. Knight Probability in Chessboard](https://tinyurl.com/y8qgukyr)** | [Python](https://tinyurl.com/wu6rdaw/688_Knight_Probability_in_Chessboard.py), [Swift](https://tinyurl.com/wuja3c4/688_Knight_Probability_in_Chessboard.swift)| [Art 1](https://tinyurl.com/ycfu5ux4), [Art 2](https://tinyurl.com/y987zy5g), [Art 3](https://tinyurl.com/ybtv5p5m) | Medium(Really!) | **Important, and fucking diifficult. TODO: Check again** |
703703
|50| **[140. Word Break II](https://tinyurl.com/yb9yotou)** | [Python](https://tinyurl.com/wu6rdaw/140_Word_Break_II.py), [Swift](https://tinyurl.com/wuja3c4/140_Word_Break_II.swift)| [Art 1](https://tinyurl.com/yc9jvbpl), [Art 2](https://tinyurl.com/yaa7b7lu) | Hard | **OHH Boy, you must recheck this, Important. TODO: Check again. An unique combination of DP, DFS and Backtracking** |
704+
|51| **[1027. Longest Arithmetic Sequence](https://tinyurl.com/y7w7mmfn)** | [Python](https://tinyurl.com/wu6rdaw/1027_Longest_Arithmetic_Sequence.py), [Swift](https://tinyurl.com/wuja3c4/1027_Longest_Arithmetic_Sequence.swift)| [Art 1](https://tinyurl.com/ycto2x5o), [Art 2](https://tinyurl.com/yab7byj5) | Medium | **I didn't know that map can be used in 1d bottom-up dp. learned new things** |
704705

705706

706707
</p>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from collections import defaultdict
2+
3+
4+
class Solution(object):
5+
def longestArithSeqLength(self, A):
6+
"""
7+
:type A: List[int]
8+
:rtype: int
9+
"""
10+
dp = defaultdict(lambda: 1) # setting the default value of defaultdict to 1
11+
for i in range(len(A)):
12+
for j in range(i + 1, len(A)):
13+
ai, aj = A[i], A[j]
14+
dp[j, aj - ai] = dp[i, aj - ai] + 1
15+
return max(dp.values())
16+
17+
18+
"""
19+
[9,4,7,2,10] >> [4,7,10]
20+
21+
j>
22+
i 9,4,7,2,10
23+
V
24+
9 x
25+
4 x
26+
7 x
27+
2 x
28+
10 x
29+
30+
"""

0 commit comments

Comments
 (0)