kt: add «2364. Count Number of Bad Pairs»
URL: https://leetcode.com/problems/count-number-of-bad-pairs/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
f15c12a030
commit
788199789d
1 changed files with 13 additions and 0 deletions
13
kt/count-number-of-bad-pairs.kt
Normal file
13
kt/count-number-of-bad-pairs.kt
Normal file
|
@ -0,0 +1,13 @@
|
|||
class Solution {
|
||||
fun countBadPairs(nums: IntArray): Long =
|
||||
mutableMapOf<Int, Int>().let { counters ->
|
||||
nums.indices.sumOf { i ->
|
||||
val diff = i - nums[i]
|
||||
val goodPairs = counters.get(diff) ?: 0
|
||||
|
||||
counters[diff] = goodPairs + 1
|
||||
|
||||
(i - goodPairs).toLong()
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue