From f3b334ef5b1b5e304b2438b9551974b73ea6f352 Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Sat, 1 Mar 2025 09:00:22 +0000 Subject: [PATCH 1/8] add 1143 --- .../java/com/github/contest/dp/DpLeetcode.kt | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt b/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt index 257afe6..56d6e7c 100644 --- a/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt +++ b/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt @@ -350,4 +350,26 @@ fun maxAbsoluteSum(nums: IntArray): Int { } return maxOf(maxSoFar, abs(minSoFar)) -} \ No newline at end of file +} + +/** + * 1143. Longest Common Subsequence + */ + +fun longestCommonSubsequence(text1: String, text2: String): Int { + val n = text1.length + val m = text2.length + val dp = Array(n + 1) { IntArray(m + 1) } + + for (i in 1..n) { + for (j in 1..m) { + if (text1[i - 1] == text2[j - 1]) { + dp[i][j] = dp[i - 1][j - 1] + 1 + } else { + dp[i][j] = maxOf(dp[i][j - 1], dp[i - 1][j]) + } + } + } + + return dp[n][m] +} From b60a50b244e1126693e70968724153e249f03a3b Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Sat, 1 Mar 2025 09:09:00 +0000 Subject: [PATCH 2/8] add prod variant for 1143 --- .../com/github/contest/dp/DpProdVariant.kt | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/contest/src/main/java/com/github/contest/dp/DpProdVariant.kt b/contest/src/main/java/com/github/contest/dp/DpProdVariant.kt index 747835b..c0aa715 100644 --- a/contest/src/main/java/com/github/contest/dp/DpProdVariant.kt +++ b/contest/src/main/java/com/github/contest/dp/DpProdVariant.kt @@ -85,3 +85,27 @@ fun longestIdealStringProdVariantII(s: String, k: Int): Int = } }.max() + +/** + * 1143. Longest Common Subsequence + * Prod Variant + */ + +fun longestCommonSubsequenceProdVariant(text1: String, text2: String): Int { + val n = text1.length + val m = text2.length + val dp = Array(n + 1) { IntArray(m + 1) } + + (1..n).forEach { row -> + (1..m).forEach { col -> + dp[row][col] = when { + text1[row - 1] == text2[col - 1] -> dp[row - 1][col - 1] + 1 + else -> maxOf(dp[row - 1][col], dp[row][col - 1]) + } + } + } + + return dp.lastElement() +} + +private fun Array.lastElement(): Int = this.last().last() From 76ec0bc82d5f2a0535f3f233a0beb473c86328ec Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Sat, 1 Mar 2025 09:18:04 +0000 Subject: [PATCH 3/8] add 2460 --- .../com/github/contest/array/ArrayLeetcode.kt | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt b/contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt index 2ea6ad7..41233ad 100644 --- a/contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt +++ b/contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt @@ -62,4 +62,37 @@ fun sortArrayByParityII(nums: IntArray): IntArray { return nums } +/** + * 2460. Apply Operations to an Array + */ + +fun applyOperations(nums: IntArray): IntArray { + for (i in 0 until nums.size - 1) { + if (nums[i] == nums[i + 1]) { + nums[i] *= 2 + nums[i + 1] = 0 + } + } + var i = 0 + var j = 1 + while (i < nums.size && j < nums.size) { + while (i < nums.size && nums[i] != 0) i++ + j = i + while (j < nums.size && nums[j] == 0) j++ + if (i < nums.size && j < nums.size) { + nums.swap(i, j) + i++ + j++ + } + } + + return nums +} + +private fun IntArray.swap(from: Int, to: Int) { + val temp = this[from] + this[from] = this[to] + this[to] = temp +} + From e037ba7dd5b43321e907f029d11f73ea4cd6d065 Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Sat, 1 Mar 2025 09:28:55 +0000 Subject: [PATCH 4/8] add 2460 alt sol --- .../contest/array/AlternativeSolutionArray.kt | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/contest/src/main/java/com/github/contest/array/AlternativeSolutionArray.kt b/contest/src/main/java/com/github/contest/array/AlternativeSolutionArray.kt index ac1009e..3d629b9 100644 --- a/contest/src/main/java/com/github/contest/array/AlternativeSolutionArray.kt +++ b/contest/src/main/java/com/github/contest/array/AlternativeSolutionArray.kt @@ -39,4 +39,34 @@ fun sortArrayByParityIIAlternativeSolution(nums: IntArray): IntArray { } return nums +} + +/** + * 2460. Apply Operations to an Array + * Alternative Solution + */ + +fun applyOperationsAlternativeSolution(nums: IntArray): IntArray { + for (i in 0 until nums.size - 1) { + if (nums[i] == nums[i + 1]) { + nums[i] *= 2 + nums[i + 1] = 0 + } + } + + var insertPosition = 0 + for (i in nums.indices) { + if (nums[i] != 0) { + nums[insertPosition] = nums[i] + insertPosition++ + } + } + + while (insertPosition < nums.size) { + nums[insertPosition] = 0 + insertPosition++ + } + + return nums + } \ No newline at end of file From d244491df558f6289639485f77a967b72535ab80 Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Sat, 1 Mar 2025 09:37:58 +0000 Subject: [PATCH 5/8] add 2460 prod variant --- .../github/contest/array/ArrayProdVariant.kt | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/contest/src/main/java/com/github/contest/array/ArrayProdVariant.kt b/contest/src/main/java/com/github/contest/array/ArrayProdVariant.kt index 89e5d2a..e934ec0 100644 --- a/contest/src/main/java/com/github/contest/array/ArrayProdVariant.kt +++ b/contest/src/main/java/com/github/contest/array/ArrayProdVariant.kt @@ -24,3 +24,32 @@ fun getCommonProdVariant(nums1: IntArray, nums2: IntArray): Int { fun isArraySpecialProdVariant(nums: IntArray): Boolean = nums.isEmpty() || nums.toList().windowed(2).all { (a, b) -> a % 2 != b % 2 } + + +/** + * 2460. Apply Operations to an Array + * Prod Variant + */ + +fun applyOperationsProdVariant(nums: IntArray): IntArray { + (0 until nums.size - 1).forEach { index -> + if (nums[index] == nums[index + 1]) { + nums[index] *= 2 + nums[index + 1] = 0 + } + } + + var insertIndex = 0 + nums.indices.forEach { + if (it != 0) { + nums[insertIndex] = it + insertIndex++ + } + } + + (insertIndex until nums.size).forEach { + nums[it] = 0 + } + + return nums +} \ No newline at end of file From 5ee93a817eb75f9260ec5c98d828fc10f3759b63 Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Sat, 1 Mar 2025 09:48:36 +0000 Subject: [PATCH 6/8] add yet prod variant --- .../github/contest/array/ArrayProdVariant.kt | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/contest/src/main/java/com/github/contest/array/ArrayProdVariant.kt b/contest/src/main/java/com/github/contest/array/ArrayProdVariant.kt index e934ec0..c2a2266 100644 --- a/contest/src/main/java/com/github/contest/array/ArrayProdVariant.kt +++ b/contest/src/main/java/com/github/contest/array/ArrayProdVariant.kt @@ -51,5 +51,25 @@ fun applyOperationsProdVariant(nums: IntArray): IntArray { nums[it] = 0 } + return nums +} + + +fun applyOperationsProdVariantII(nums: IntArray): IntArray { + nums.indices.drop(1).forEach { i -> + if (nums[i - 1] == nums[i]) { + nums[i - 1] *= 2 + nums[i] = 0 + } + } + + var insertPos = 0 + nums.forEach { num -> + if (num != 0) { + nums[insertPos++] = num + } + } + nums.fill(0, insertPos) + return nums } \ No newline at end of file From 8e7c86b92797836b52d67061febd407e96b673b4 Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Sat, 1 Mar 2025 11:46:54 +0000 Subject: [PATCH 7/8] add 1048 --- .../main/java/com/github/contest/Execute.kt | 4 ++-- .../java/com/github/contest/dp/DpLeetcode.kt | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/contest/src/main/java/com/github/contest/Execute.kt b/contest/src/main/java/com/github/contest/Execute.kt index a86a62d..9200524 100644 --- a/contest/src/main/java/com/github/contest/Execute.kt +++ b/contest/src/main/java/com/github/contest/Execute.kt @@ -1,7 +1,7 @@ package com.github.contest -import com.github.contest.array.sortArrayByParityIIAlternativeSolution +import com.github.contest.dp.longestStrChain import java.util.TreeMap @@ -11,7 +11,7 @@ import java.util.TreeMap fun main() { - sortArrayByParityIIAlternativeSolution(intArrayOf(4, 2, 5, 7)).also { it.printArray() } + longestStrChain(arrayOf("a", "b", "ba", "bca", "bda", "bdca")).also { println(it) } } diff --git a/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt b/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt index 56d6e7c..360b69f 100644 --- a/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt +++ b/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt @@ -373,3 +373,25 @@ fun longestCommonSubsequence(text1: String, text2: String): Int { return dp[n][m] } + +/** + * 1048. Longest String Chain + */ + +fun longestStrChain(words: Array): Int { + val sortedWords = words.sortedBy { it.length } + val dp = mutableMapOf() + var longestChain = 0 + + for (word in sortedWords) { + var currentChain = 1 + for (i in word.indices) { + val predecessor = word.removeRange(i, i + 1) + currentChain = maxOf(currentChain, dp.getOrDefault(predecessor, 0) + 1) + } + dp[word] = currentChain + longestChain = maxOf(longestChain, currentChain) + } + + return longestChain +} From baf711f45a6b77cad3dd4652b796d57f7064970f Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Sat, 1 Mar 2025 12:30:28 +0000 Subject: [PATCH 8/8] add 1027 --- .../main/java/com/github/contest/Execute.kt | 5 ++--- .../java/com/github/contest/dp/DpLeetcode.kt | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/contest/src/main/java/com/github/contest/Execute.kt b/contest/src/main/java/com/github/contest/Execute.kt index 9200524..dd43a43 100644 --- a/contest/src/main/java/com/github/contest/Execute.kt +++ b/contest/src/main/java/com/github/contest/Execute.kt @@ -1,7 +1,7 @@ package com.github.contest -import com.github.contest.dp.longestStrChain +import com.github.contest.dp.longestArithSeqLength import java.util.TreeMap @@ -11,8 +11,7 @@ import java.util.TreeMap fun main() { - longestStrChain(arrayOf("a", "b", "ba", "bca", "bda", "bdca")).also { println(it) } - + longestArithSeqLength(intArrayOf(3, 6, 9, 12)) } fun generateTesting() { diff --git a/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt b/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt index 360b69f..9e7c9d9 100644 --- a/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt +++ b/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt @@ -395,3 +395,25 @@ fun longestStrChain(words: Array): Int { return longestChain } + +/** + * 1027. Longest Arithmetic Subsequence + */ + +fun longestArithSeqLength(nums: IntArray): Int { + val n = nums.size + + val dp = Array(n) { mutableMapOf() } + var longest = 2 + + for (i in 1 until n) { + for (j in 0 until i) { + val diff = nums[i] - nums[j] + val length = dp[j].getOrDefault(diff, 1) + 1 + dp[i][diff] = length + longest = maxOf(longest, length) + } + } + + return longest +}