LeetCode之Queries on a Permutation With Key(Kotlin)

问题:



方法:
最简单解法,模拟整个操作,代码如下所示;网上还有一种FenwickTree的解法,有时间可以学习一下

package com.eric.leetcode

class QueriesOnAPermutationWithKey {
    // todo FenwickTree
    fun processQueries(queries: IntArray, m: Int): IntArray {
        val result = IntArray(queries.size) { 0 }
        val p = mutableListOf()
        for (index in 1..m) {
            p.add(index)
        }
        for (index in 0..queries.lastIndex) {
            val query = queries[index]
            for (i in 0 until m) {
                if (p[i] == query) {
                    result[index] = i
                    p.remove(query)
                    p.add(0, query)
                    break
                }
            }

        }
        return result
    }
}

fun main() {
    val q = QueriesOnAPermutationWithKey()
    q.processQueries(intArrayOf(), 5)
}

有问题随时沟通

具体代码实现可以参考Github

你可能感兴趣的:(LeetCode之Queries on a Permutation With Key(Kotlin))