Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
isaiahliu committed Nov 28, 2024
1 parent f7cfce2 commit f50d07a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/main/kotlin/p32xx/Problem3250.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,29 @@ import util.expect
fun main() {
class Solution {
fun countOfPairs(nums: IntArray): Int {
val m = 1000000007L
val m = 1000000007

var dp = LongArray(nums[0] + 1) { 1 }
var dp = IntArray(nums[0] + 1) { it + 1 }

for (index in 1 until nums.size) {
dp = LongArray(nums[index] + 1) { cur ->
(0..cur).filter { nums[index - 1] - it >= nums[index] - cur }.sumOf { dp.getOrElse(it) { 0L } } % m
var sum = 0
dp = IntArray(nums[index] + 1) { cur ->
dp.getOrNull(minOf(nums[index - 1] - nums[index], 0) + cur)?.also {
sum += it
sum %= m
}

sum
}
}

return (dp.sum() % m).toInt()
return dp.last()
}
}

expect {
Solution().countOfPairs(
intArrayOf()
intArrayOf(2, 3, 2)
)
}
}
33 changes: 33 additions & 0 deletions src/main/kotlin/p32xx/Problem3251.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package p32xx

import util.expect

fun main() {
class Solution {
fun countOfPairs(nums: IntArray): Int {
val m = 1000000007

var dp = IntArray(nums[0] + 1) { it + 1 }

for (index in 1 until nums.size) {
var sum = 0
dp = IntArray(nums[index] + 1) { cur ->
dp.getOrNull(minOf(nums[index - 1] - nums[index], 0) + cur)?.also {
sum += it
sum %= m
}

sum
}
}

return dp.last()
}
}

expect {
Solution().countOfPairs(
intArrayOf()
)
}
}

0 comments on commit f50d07a

Please sign in to comment.