From eb534ea18cb6fe00bb56e8acc775e26a38876967 Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Sat, 8 Feb 2025 19:12:39 +0500 Subject: [PATCH 01/12] add 2900 --- .../java/com/github/contest/dp/DpLeetcode.kt | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) 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 36e4574c..315805dd 100644 --- a/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt +++ b/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt @@ -197,16 +197,38 @@ fun longestIdealString(s: String, k: Int): Int { fun divisorGameDp(n: Int): Boolean { if (n <= 1) return false val dp = BooleanArray(n + 1) - dp[1] = false // Base case: If n is 1, the current player loses + dp[1] = false for (i in 2..n) { for (x in 1 until i) { if (i % x == 0 && !dp[i - x]) { dp[i] = true - break // If we find a winning move, we can stop checking + break } } } return dp[n] +} + +/** + * 2900. Longest Unequal Adjacent Groups Subsequence I + */ + +fun getLongestSubsequence(words: Array, groups: IntArray): List { + val dp = BooleanArray(words.size) { false } + dp[0] = true + var j = 0 + for (i in 1 until groups.size) { + if (groups[i] != groups[j]) { + dp[i] = true + j = i + } + } + var res = mutableListOf() + for (i in words.indices) { + if (dp[i]) res.add(words[i]) + } + + return res } \ No newline at end of file From dbd70e5e2fec2b25816716f16021d1ec0d5c7fb4 Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Sun, 9 Feb 2025 17:33:35 +0500 Subject: [PATCH 02/12] add 64 --- .../main/java/com/github/contest/Execute.kt | 10 ++++-- .../java/com/github/contest/dp/DpLeetcode.kt | 32 +++++++++++++++++++ 2 files changed, 40 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 80e1473c..a4478cc0 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.divisorGameDp +import com.github.contest.dp.minPathSum /** @@ -10,6 +10,12 @@ import com.github.contest.dp.divisorGameDp fun main() { - divisorGameDp(6) + minPathSum( + arrayOf( + intArrayOf(1, 3, 1), + intArrayOf(1, 5, 1), + intArrayOf(4, 2, 1) + ) + ).also { println(it) } } \ No newline at end of file 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 315805dd..3ec9eb4b 100644 --- a/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt +++ b/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt @@ -231,4 +231,36 @@ fun getLongestSubsequence(words: Array, groups: IntArray): List } return res +} + +/** + * 64. Minimum Path Sum + */ + +fun minPathSum(grid: Array): Int { + val m = grid.size + val n = grid[0].size + val dp = Array(m) { IntArray(n) } + + + dp[0][0] = grid[0][0] + + + for (j in 1 until n) { + dp[0][j] = dp[0][j - 1] + grid[0][j] + } + + + for (i in 1 until m) { + dp[i][0] = dp[i - 1][0] + grid[i][0] + } + + + for (i in 1 until m) { + for (j in 1 until n) { + dp[i][j] = minOf(dp[i - 1][j], dp[i][j - 1]) + grid[i][j] + } + } + + return dp[m - 1][n - 1] } \ No newline at end of file From 606a833f7c44485a80b53e71be1627ca90ba7835 Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Sun, 9 Feb 2025 18:26:26 +0500 Subject: [PATCH 03/12] add 2364 --- .../main/java/com/github/contest/Execute.kt | 10 ++-------- .../contest/hashTable/HashTableLeetcode.kt | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/contest/src/main/java/com/github/contest/Execute.kt b/contest/src/main/java/com/github/contest/Execute.kt index a4478cc0..286515f2 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.minPathSum +import com.github.contest.hashTable.countBadPairs /** @@ -10,12 +10,6 @@ import com.github.contest.dp.minPathSum fun main() { - minPathSum( - arrayOf( - intArrayOf(1, 3, 1), - intArrayOf(1, 5, 1), - intArrayOf(4, 2, 1) - ) - ).also { println(it) } + countBadPairs(intArrayOf(4, 1, 3, 3)) } \ No newline at end of file diff --git a/contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt b/contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt index af147ce6..338e1b0a 100644 --- a/contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt +++ b/contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt @@ -147,4 +147,24 @@ private fun String.hasSingle(): Boolean = when { private fun isDigit(s: Char): Boolean { return s in "0123456789" +} + +/** + * 2364. Count Number of Bad Pairs + */ + + +fun countBadPairs(nums: IntArray): Long { + val n = nums.size + val diffCounts = mutableMapOf() + var goodPairs = 0L + + for (j in 0 until n) { + val diff = nums[j] - j + goodPairs += diffCounts.getOrDefault(diff, 0L) + diffCounts[diff] = diffCounts.getOrDefault(diff, 0L) + 1 + } + + val totalPairs = n.toLong() * (n - 1) / 2 + return totalPairs - goodPairs } \ No newline at end of file From 604855d73e5b549dc080a09a76eec2e43d158ed3 Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Sun, 9 Feb 2025 20:06:49 +0500 Subject: [PATCH 04/12] save alt sol --- .../main/java/com/github/contest/Execute.kt | 4 +-- .../hashTable/HashTableAlternativeSolution.kt | 26 +++++++++++++++++++ 2 files changed, 28 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 286515f2..7e2f96c3 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.hashTable.countBadPairs +import com.github.contest.hashTable.countBadPairsAltSolution /** @@ -10,6 +10,6 @@ import com.github.contest.hashTable.countBadPairs fun main() { - countBadPairs(intArrayOf(4, 1, 3, 3)) + countBadPairsAltSolution(intArrayOf(4, 1, 3, 3, 4)).also { println(it) } } \ No newline at end of file diff --git a/contest/src/main/java/com/github/contest/hashTable/HashTableAlternativeSolution.kt b/contest/src/main/java/com/github/contest/hashTable/HashTableAlternativeSolution.kt index 24e0b06f..e720e671 100644 --- a/contest/src/main/java/com/github/contest/hashTable/HashTableAlternativeSolution.kt +++ b/contest/src/main/java/com/github/contest/hashTable/HashTableAlternativeSolution.kt @@ -19,4 +19,30 @@ fun areAlmostEqualAltSolution(s1: String, s2: String): Boolean { s1[diff[0]] == s2[diff[1]] && s2[diff[0]] == s1[diff[1]] -> true else -> false } +} + +/** + * + */ + +fun countBadPairsAltSolution(nums: IntArray): Long { + val allPairs = nums.size.toLong() * (nums.size - 1) + val allOrderedPairs = allPairs / 2 + + val map = HashMap() + nums.forEachIndexed { i, value -> + map.getOrPut(value - i) { Counter() }.count++ + } + + var goodOrderedPairs = 0L + for (c in map.values) { + val goodPairs = c.count.toLong() * (c.count - 1) + goodOrderedPairs += goodPairs / 2 + } + + return allOrderedPairs - goodOrderedPairs +} + +class Counter { + var count: Int = 0 } \ No newline at end of file From 0b30373e000ea001506e9097d7979fe409f583ad Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Sun, 9 Feb 2025 23:36:20 +0500 Subject: [PATCH 05/12] add 1981 --- .../java/com/github/contest/dp/DpLeetcode.kt | 31 +++++++++++++++++++ .../contest/hashTable/HashTableLeetcode.kt | 26 ++++++++++++++++ 2 files changed, 57 insertions(+) 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 3ec9eb4b..88de17f1 100644 --- a/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt +++ b/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt @@ -263,4 +263,35 @@ fun minPathSum(grid: Array): Int { } return dp[m - 1][n - 1] +} + +/** + * 1981. Minimize the Difference Between Target and Chosen Elements + */ + +fun minimizeTheDifference(mat: Array, target: Int): Int { + val m = mat.size + val n = mat[0].size + + var possibleSums = mutableSetOf() + for (num in mat[0]) { + possibleSums.add(num) + } + + for (i in 1 until m) { + val nextPossibleSums = mutableSetOf() + for (sum in possibleSums) { + for (num in mat[i]) { + nextPossibleSums.add(sum + num) + } + } + possibleSums = nextPossibleSums + } + + var minDiff = Int.MAX_VALUE + for (sum in possibleSums) { + minDiff = minOf(minDiff, abs(sum - target)) + } + + return minDiff } \ No newline at end of file diff --git a/contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt b/contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt index 338e1b0a..4fa97354 100644 --- a/contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt +++ b/contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt @@ -167,4 +167,30 @@ fun countBadPairs(nums: IntArray): Long { val totalPairs = n.toLong() * (n - 1) / 2 return totalPairs - goodPairs +} + +/** + * + */ + +fun findPairs(nums: IntArray, k: Int): Int { + if (k < 0) return 0 + + val numCounts = mutableMapOf() + for (num in nums) { + numCounts[num] = numCounts.getOrDefault(num, 0) + 1 + } + + if (k == 0) { + return numCounts.count { it.value >= 2 } + } + + val uniquePairs = mutableSetOf>() + for (num in nums) { + if (numCounts.containsKey(num + k)) { + uniquePairs.add(Pair(minOf(num, num + k), maxOf(num, num + k))) + } + } + + return uniquePairs.size } \ No newline at end of file From bce4d5d1fc50cdf9b3117e34a95a47f37cfaca4f Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Mon, 10 Feb 2025 12:33:36 +0500 Subject: [PATCH 06/12] add 3174 --- .../com/github/contest/stack/StackLeetcode.kt | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 contest/src/main/java/com/github/contest/stack/StackLeetcode.kt diff --git a/contest/src/main/java/com/github/contest/stack/StackLeetcode.kt b/contest/src/main/java/com/github/contest/stack/StackLeetcode.kt new file mode 100644 index 00000000..0497de17 --- /dev/null +++ b/contest/src/main/java/com/github/contest/stack/StackLeetcode.kt @@ -0,0 +1,22 @@ +package com.github.contest.stack + +/** + * 3174. Clear Digits + */ + +fun clearDigits(s: String): String { + val stack = ArrayDeque() + for (char in s) { + if (isDigit(char)) { + if (stack.isNotEmpty()) stack.removeLast() + } else stack.addLast(char) + } + return if (stack.isEmpty()) "" else buildString { + while (stack.isNotEmpty()) { + val temp = stack.removeFirst() + append(temp) + } + } +} + +private fun isDigit(char: Char) = char in "0123456789" From d417eb4ff4177690ce45dd2112c0edacfe1e5515 Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Tue, 11 Feb 2025 12:57:37 +0500 Subject: [PATCH 07/12] add 1910 --- .idea/other.xml | 11 ++++++++++ .../main/java/com/github/contest/Execute.kt | 5 ++--- .../com/github/contest/stack/StackLeetcode.kt | 20 +++++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/.idea/other.xml b/.idea/other.xml index 25d338aa..a15677d4 100644 --- a/.idea/other.xml +++ b/.idea/other.xml @@ -47,6 +47,17 @@