Skip to content

Commit a5fd915

Browse files
committed
leetcode
1 parent 3d4a581 commit a5fd915

File tree

4 files changed

+270
-0
lines changed

4 files changed

+270
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
3+
4+
5+
-* 1027. Longest Arithmetic Subsequence *-
6+
7+
8+
Given an array nums of integers, return the length of the longest arithmetic subsequence in nums.
9+
10+
Note that:
11+
12+
A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
13+
A sequence seq is arithmetic if seq[i + 1] - seq[i] are all the same value (for 0 <= i < seq.length - 1).
14+
15+
16+
Example 1:
17+
18+
Input: nums = [3,6,9,12]
19+
Output: 4
20+
Explanation: The whole array is an arithmetic sequence with steps of length = 3.
21+
Example 2:
22+
23+
Input: nums = [9,4,7,2,10]
24+
Output: 3
25+
Explanation: The longest arithmetic subsequence is [4,7,10].
26+
Example 3:
27+
28+
Input: nums = [20,1,15,3,10,5,8]
29+
Output: 4
30+
Explanation: The longest arithmetic subsequence is [20,15,10,5].
31+
32+
33+
Constraints:
34+
35+
2 <= nums.length <= 1000
36+
0 <= nums[i] <= 500
37+
38+
*/
39+
40+
import 'dart:collection';
41+
import 'dart:math';
42+
43+
class Solution {
44+
int longestArithSeqLength(List<int> nums) {
45+
int n = nums.length;
46+
if (n <= 2) return n;
47+
48+
int longest = 2;
49+
List<HashMap<int, int>> dp =
50+
List<HashMap<int, int>>.generate(n, (index) => HashMap<int, int>());
51+
52+
for (int i = 0; i < n; i++) {
53+
for (int j = 0; j < i; j++) {
54+
int diff = nums[i] - nums[j];
55+
dp[i][diff] = dp[j].containsKey(diff) ? dp[j][diff]! + 1 : 2;
56+
longest = longest > dp[i][diff]! ? longest : dp[i][diff]!;
57+
}
58+
}
59+
60+
return longest;
61+
}
62+
}
63+
64+
class B {
65+
int longestArithSeqLength(List<int> nums) {
66+
int n = nums.length;
67+
int max = 0;
68+
List<List<int>> dp = List.generate(n, (i) => List<int>.filled(1001, 0));
69+
70+
for (int i = 1; i < n; i++) {
71+
for (int j = 0; j < i; j++) {
72+
int diff = nums[j] - nums[i] + 500;
73+
dp[i][diff] = dp[j][diff] + 1;
74+
max = max < dp[i][diff] ? dp[i][diff] : max;
75+
}
76+
}
77+
return max + 1;
78+
}
79+
}
80+
81+
class C {
82+
int longestArithSeqLength(List<int> nums) {
83+
Map<int, List<int>> hash = {};
84+
for (int i = 0; i < nums.length; i++) {
85+
if (!hash.containsKey(nums[i])) {
86+
hash[nums[i]] = [];
87+
}
88+
hash[nums[i]]!.add(i);
89+
}
90+
int maxCount = 0;
91+
for (int i = 0; i < nums.length; i++) {
92+
for (int j = i + 1; j < nums.length; j++) {
93+
int d = nums[j] - nums[i], ls = nums[j], cnt = 2, li = j;
94+
while (ls >= 0) {
95+
List<int>? v = hash[ls + d];
96+
if (d == 0) {
97+
cnt = v!.length;
98+
break;
99+
}
100+
if (v != null && v.length > 0) {
101+
int ind = v.indexOf(li);
102+
if (ind >= 0 && ind < v.length) {
103+
cnt++;
104+
ls += d;
105+
li = v[ind];
106+
} else {
107+
break;
108+
}
109+
} else {
110+
break;
111+
}
112+
}
113+
maxCount = max(maxCount, cnt);
114+
}
115+
}
116+
return maxCount;
117+
}
118+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import "sort"
4+
5+
func longestArithSeqLength(nums []int) int {
6+
hash := make(map[int][]int)
7+
for i, num := range nums {
8+
hash[num] = append(hash[num], i)
9+
}
10+
11+
maxCount := 0
12+
for i := 0; i < len(nums); i++ {
13+
for j := i + 1; j < len(nums); j++ {
14+
d := nums[j] - nums[i]
15+
ls := nums[j]
16+
cnt := 2
17+
li := j
18+
for ls >= 0 {
19+
v := hash[ls+d]
20+
if d == 0 {
21+
cnt = len(v)
22+
break
23+
}
24+
ind := sort.Search(len(v), func(k int) bool { return v[k] > li })
25+
if ind >= len(v) {
26+
break
27+
}
28+
cnt++
29+
ls += d
30+
li = v[ind]
31+
}
32+
if cnt > maxCount {
33+
maxCount = cnt
34+
}
35+
}
36+
}
37+
return maxCount
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# 🔥 3 Solutions 🔥 || Simple Fast and Easy || with Explanation 😈
2+
3+
4+
## Intuition
5+
The intuition behind this solution is to use dynamic programming to find the length of the longest arithmetic subsequence in the given array. We iterate through each pair of indices (i, j), where i > j, and check if we can extend the arithmetic subsequence ending at index i by including the element at index j. We update the dynamic programming values based on the common difference between the elements at indices i and j. By keeping track of the maximum length encountered, we can find the length of the longest arithmetic subsequence in the array.
6+
7+
## Approach:
8+
1. If n is less than or equal to 2, we can immediately return n as the answer since any two elements form a valid arithmetic subsequence.
9+
2. We initialize the variable longest to 2, as explained earlier, the minimum length of an arithmetic subsequence is 2.
10+
3. We create a 2D vector dp of size n, where each element is an unordered map. The purpose of dp is to store the dynamic programming values for the lengths of the arithmetic subsequences ending at each index with different common differences.
11+
3. We iterate through each index i in the range [0, n-1]. This represents the current ending index of the arithmetic subsequences we are considering.
12+
4. For each i, we iterate through all previous indices j in the range [0, i-1]. This allows us to check all the potential elements that can form an arithmetic subsequence with the element at index i.
13+
5. We calculate the difference between the elements at indices i and j and store it in the variable diff. This difference represents the common difference of the potential arithmetic subsequence.
14+
6. Next, we update dp[i][diff] based on whether we have seen a previous arithmetic subsequence ending at index j with a common difference of diff.
15+
7. If dp[j].count(diff) returns true, it means we have encountered an arithmetic subsequence ending at index j with the common difference diff. In this case, we update dp[i][diff] to be dp[j][diff] + 1, which extends the subsequence and increments its length by 1.
16+
8. If dp[j].count(diff) returns false, it means we haven't seen an arithmetic subsequence ending at index j with the common difference diff. In this case, we initialize dp[i][diff] to 2 because we have found a new arithmetic subsequence of length 2 (nums[j], nums[i]).
17+
9. After updating dp[i][diff], we check if the current length dp[i][diff] is greater than the current longest arithmetic subsequence length longest. If so, we update longest to the new maximum length.
18+
10. Once we finish iterating through all pairs of indices, we have computed the lengths of all possible arithmetic subsequences ending at each index. The maximum length among these subsequences is stored in longest, so we return it as the result.
19+
20+
21+
22+
## Solution - 1
23+
24+
```dart
25+
import 'dart:collection';
26+
27+
class Solution {
28+
int longestArithSeqLength(List<int> nums) {
29+
final int n = nums.length;
30+
if (n <= 2) return n;
31+
32+
int longest = 2;
33+
final List<HashMap<int, int>> dp =
34+
List<HashMap<int, int>>.generate(n, (index) => HashMap<int, int>());
35+
36+
for (int i = 0; i < n; i++) {
37+
for (int j = 0; j < i; j++) {
38+
int diff = nums[i] - nums[j];
39+
dp[i][diff] = dp[j].containsKey(diff) ? dp[j][diff]! + 1 : 2;
40+
longest = longest > dp[i][diff]! ? longest : dp[i][diff]!;
41+
}
42+
}
43+
44+
return longest;
45+
}
46+
}
47+
```
48+
49+
## Solution - 2
50+
51+
```dart
52+
class Solution {
53+
int longestArithSeqLength(List<int> nums) {
54+
final int n = nums.length;
55+
int max = 0;
56+
final List<List<int>> dp = List.generate(n, (i) => List<int>.filled(1001, 0));
57+
58+
for (int i = 1; i < n; i++) {
59+
for (int j = 0; j < i; j++) {
60+
int diff = nums[j] - nums[i] + 500;
61+
dp[i][diff] = dp[j][diff] + 1;
62+
max = max < dp[i][diff] ? dp[i][diff] : max;
63+
}
64+
}
65+
return max + 1;
66+
}
67+
}
68+
```
69+
70+
## Solution - 3 - Stay Away this one Contain BUG Intentional BUG 😈
71+
72+
```dart
73+
class Solution {
74+
int longestArithSeqLength(List<int> nums) {
75+
Map<int, List<int>> hash = {};
76+
for (int i = 0; i < nums.length; i++) {
77+
if (!hash.containsKey(nums[i])) {
78+
hash[nums[i]] = [];
79+
}
80+
hash[nums[i]]!.add(i);
81+
}
82+
int maxCount = 0;
83+
for (int i = 0; i < nums.length; i++) {
84+
for (int j = i + 1; j < nums.length; j++) {
85+
int d = nums[j] - nums[i], ls = nums[j], cnt = 2, li = j;
86+
while (ls >= 0) {
87+
List<int>? v = hash[ls + d];
88+
if (d == 0) {
89+
cnt = v!.length;
90+
break;
91+
}
92+
if (v != null && v.length > 0) {
93+
int ind = v.indexOf(li);
94+
if (ind >= 0 && ind < v.length) {
95+
cnt++;
96+
ls += d;
97+
li = v[ind];
98+
} else {
99+
break;
100+
}
101+
} else {
102+
break;
103+
}
104+
}
105+
maxCount = max(maxCount, cnt);
106+
}
107+
}
108+
return maxCount;
109+
}
110+
}
111+
```

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ This repo contain leetcode solution using DART and GO programming language. Most
236236
- [**1732.** Find the Highest Altitude](FindTheHighestAltitude/find_the_highest_altitude.dart)
237237
- [**2090.** K Radius SubArray Averages](KRadiusSubarrayAverages/k_radius_subarray_averages.dart)
238238
- [**714.** Best Time to Buy and Sell Stock with Transaction Fee](BestTimeToBuyAndSellStockWithTransactionFee/best_time_to_buy_and_sell_stock_with_transaction_fee.dart)
239+
- [**1027.** Longest Arithmetic Subsequence](LongestArithmeticSubsequence/longest_arithmetic_subsequence.dart)
240+
241+
239242

240243
## Reach me via
241244

0 commit comments

Comments
 (0)