arrow_forward همه ارسال‌ها

جزئیات ارسال کد#638

verified
وضعیت داوری نهایی: پذیرفته شد
terminal
زبان برنامه‌نویسی: Kotlin
schedule
ارسال‌کننده و زمان:
Faraz Azadi (۱ سال پیش)
descriptionsolution.kt ۲۴ خط • ۶۸۸ بایت
1import java.io.BufferedReader
2import java.io.InputStreamReader
3
4fun minimumWiringLength(n: Int, houseNumbers: List<Int>): Long {
5 val positions = mutableMapOf<Int, Int>()
6 var totalLength = 0L
7
8 for ((index, house) in houseNumbers.withIndex()) {
9 if (positions.containsKey(house)) {
10 totalLength += index - positions[house]!!
11 } else {
12 positions[house] = index
13 }
14 }
15
16 return totalLength
17}
18
19fun main() {
20 val reader = BufferedReader(InputStreamReader(System.`in`))
21 val n = reader.readLine().toInt()
22 val houseNumbers = reader.readLine().split(" ").map { it.toInt() }
23 println(minimumWiringLength(n, houseNumbers))
24}
کد ارسال‌شده برای داوری خودکار هاتکدز UTF-8