kt: add «1726. Tuple with Same Product»
URL: https://leetcode.com/problems/tuple-with-same-product/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
e3be56387e
commit
e6e431d528
1 changed files with 16 additions and 0 deletions
16
kt/tuple-with-same-product.kt
Normal file
16
kt/tuple-with-same-product.kt
Normal file
|
@ -0,0 +1,16 @@
|
|||
class Solution {
|
||||
fun tupleSameProduct(nums: IntArray): Int =
|
||||
buildMap<Int, Int> {
|
||||
nums.indices
|
||||
.flatMap { i ->
|
||||
nums.indices.drop(i + 1).map { j ->
|
||||
nums[i] to nums[j]
|
||||
}
|
||||
}.forEach { (x, y) ->
|
||||
put(x * y, 1 + (get(x * y) ?: 0))
|
||||
}
|
||||
}.entries.sumOf { (p, count) ->
|
||||
val pairs = (count - 1) * count / 2
|
||||
8 * pairs
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue