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 8966926 commit f7cfce2
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions src/main/kotlin/p32xx/Problem3250.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,17 @@ import util.expect
fun main() {
class Solution {
fun countOfPairs(nums: IntArray): Int {
val m = 1000000007
val m = 1000000007L

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

for (index in 1 until nums.size) {
val newDp = IntArray(51)

for (cur in 0..nums[index]) {
for (prev in 0..cur) {
if (nums[index - 1] - prev >= nums[index] - cur) {
newDp[cur] = (newDp[cur] + dp[prev]) % m
}
}
dp = LongArray(nums[index] + 1) { cur ->
(0..cur).filter { nums[index - 1] - it >= nums[index] - cur }.sumOf { dp.getOrElse(it) { 0L } } % m
}

dp = newDp
}

return dp.fold(0) { sum, c ->
(sum + c) % m
}
return (dp.sum() % m).toInt()
}
}

Expand Down

0 comments on commit f7cfce2

Please sign in to comment.