File tree Expand file tree Collapse file tree 2 files changed +21
-0
lines changed
contest/src/main/java/com/github/contest/dp Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -417,3 +417,5 @@ fun longestArithSeqLength(nums: IntArray): Int {
417
417
418
418
return longest
419
419
}
420
+
421
+
Original file line number Diff line number Diff line change @@ -129,4 +129,23 @@ fun longestStrChainProdVariant(words: Array<String>): Int {
129
129
dp[word] = currentChain
130
130
currentChain
131
131
}
132
+ }
133
+
134
+ /* *
135
+ * 1027. Longest Arithmetic Subsequence
136
+ * Prod Variant
137
+ */
138
+
139
+ fun longestArithSeqLengthProdVariant (nums : IntArray ): Int {
140
+ val dp = Array (nums.size) { mutableMapOf<Int , Int >() }
141
+ var longest = 0
142
+
143
+ return (1 until nums.size).maxOf { i ->
144
+ (0 until i).maxOf { j ->
145
+ val diff = nums[i] - nums[j]
146
+ val len = dp[j].getOrDefault(diff, 1 ) + 1
147
+ dp[i][diff] = len
148
+ len
149
+ }
150
+ }
132
151
}
You can’t perform that action at this time.
0 commit comments