Skip to content

add new problem 13.02 #149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .idea/other.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions contest/src/main/java/com/github/contest/Execute.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.contest


import com.github.contest.dp.divisorGameDp
import com.github.contest.heap.halveArray


/**
Expand All @@ -10,6 +9,7 @@ import com.github.contest.dp.divisorGameDp

fun main() {

divisorGameDp(6)
halveArray(intArrayOf(7, 7, 7, 31, 2)).also { println(it) }


}
116 changes: 114 additions & 2 deletions contest/src/main/java/com/github/contest/dp/DpLeetcode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,128 @@ 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<String>, groups: IntArray): List<String> {
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<String>()
for (i in words.indices) {
if (dp[i]) res.add(words[i])
}

return res
}

/**
* 64. Minimum Path Sum
*/

fun minPathSum(grid: Array<IntArray>): 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]
}

/**
* 1981. Minimize the Difference Between Target and Chosen Elements
*/

fun minimizeTheDifference(mat: Array<IntArray>, target: Int): Int {
val m = mat.size
val n = mat[0].size

var possibleSums = mutableSetOf<Int>()
for (num in mat[0]) {
possibleSums.add(num)
}

for (i in 1 until m) {
val nextPossibleSums = mutableSetOf<Int>()
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
}

/**
* 2063. Vowels of All Substrings
*/

fun countVowels(word: String): Long {
val n = word.length
val dp = LongArray(n)
val vowels = setOf('a', 'e', 'i', 'o', 'u')
var totalVowels = 0L

if (word[0] in vowels) {
dp[0] = 1
}

totalVowels += dp[0]

for (i in 1 until n) {
dp[i] = dp[i - 1]
if (word[i] in vowels) {
dp[i] += (i + 1).toLong()
}
totalVowels += dp[i]
}

return totalVowels
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int, Counter>()
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,84 @@ 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<Int, Long>()
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
}

/**
*
*/

fun findPairs(nums: IntArray, k: Int): Int {
if (k < 0) return 0

val numCounts = mutableMapOf<Int, Int>()
for (num in nums) {
numCounts[num] = numCounts.getOrDefault(num, 0) + 1
}

if (k == 0) {
return numCounts.count { it.value >= 2 }
}

val uniquePairs = mutableSetOf<Pair<Int, Int>>()
for (num in nums) {
if (numCounts.containsKey(num + k)) {
uniquePairs.add(Pair(minOf(num, num + k), maxOf(num, num + k)))
}
}

return uniquePairs.size
}

/**
* 2342. Max Sum of a Pair With Equal Sum of Digits
*/

fun maximumSum(nums: IntArray): Int {
var maxSum = -1
val map = mutableMapOf<Int, Int>()

for (i in 0 until nums.size) {
val sum = sumOfDigit(nums[i])
if (map.contains(sum)) {
val first = map.getOrDefault(sum, -1)
maxSum = maxOf(maxSum, nums[first] + nums[i])
map[sum] = when {
nums[i] > nums[first] -> i
else -> first
}
} else map[sum] = map.getOrDefault(sum, i)
}

return maxSum
}

private fun sumOfDigit(num: Int): Int {
var num = num
var res = 0
while (num != 0) {
res += num % 10
num /= 10
}

return res
}
46 changes: 46 additions & 0 deletions contest/src/main/java/com/github/contest/heap/HeapLeetcode.kt
Original file line number Diff line number Diff line change
@@ -1,2 +1,48 @@
package com.github.contest.heap

import java.util.PriorityQueue

/**
* 3066. Minimum Operations to Exceed Threshold Value II
*/

fun minOperations(nums: IntArray, k: Int): Int {
var operation = 0
val pq = PriorityQueue<Long>()
for (num in nums) pq.offer(num.toLong())

while (pq.size > 1 && pq.peek() < k) {
val x = pq.poll()
val y = pq.poll()
val new = minOf(x, y) * 2 + maxOf(x, y)
pq.offer(new)
operation++
}

return operation
}

/**
* 2208. Minimum Operations to Halve Array Sum
*/

fun halveArray(nums: IntArray): Int {
val pq = PriorityQueue<Double>(reverseOrder())
var sum = 0.0
for (num in nums) {
pq.offer(num.toDouble())
sum += num
}

var operations = 0
var halvedSum = 0.0
while (halvedSum < (sum / 2)) {
val largest = pq.poll()
val halved = largest / 2
halvedSum += halved
pq.offer(halved)
operations++
}

return operations
}
42 changes: 42 additions & 0 deletions contest/src/main/java/com/github/contest/stack/StackLeetcode.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.github.contest.stack

/**
* 3174. Clear Digits
*/

fun clearDigits(s: String): String {
val stack = ArrayDeque<Char>()
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"


/**
* 1910. Remove All Occurrences of a Substring
*/

fun removeOccurrences(s: String, part: String): String {
val res = StringBuilder()
val m = part.length

for (c in s.toCharArray()) {
res.append(c)
if (res.length >= m && res.substring(res.length - m) == part) {
res.delete(res.length - m, res.length)
}
}
return res.toString()
}


Loading