All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g2801_2900.s2869_minimum_operations_to_collect_elements.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g2801_2900.s2869_minimum_operations_to_collect_elements

// #Easy #Array #Hash_Table #2023_12_21_Time_156_ms_(100.00%)_Space_35.2_MB_(53.85%)

import kotlin.math.max

class Solution {
    fun minOperations(nums: List, k: Int): Int {
        val visited = arrayOfNulls(k + 1)
        visited[0] = Pair(0)
        var count = 0
        for (i in nums.indices.reversed()) {
            count++
            if (nums[i] <= k && visited[nums[i]] == null) {
                visited[nums[i]] = Pair(count)
            }
        }
        var fin = -1
        for (pair in visited) {
            if (pair != null) {
                fin = max(fin, pair.totalVisitedTillNow)
            }
        }
        return fin
    }

    private class Pair(var totalVisitedTillNow: Int)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy