jeudi 16 juillet 2020

Can someone please find what exactly the flaw is in my code? [closed]

So I have been working on this problem for like 3 days now, I am using Kotlin as the language here since I am learning Kotlin. Also, I should mention Kotlin is my frist language so it's not like I know Java or had experience with programming prior to starting to learn Kotlin. I have tried 3 different approaches before but all of them failed. They gave the right answer but were too resource heavy that the compilation never finished.

here's the other three versions of this code

Now this time I re-wrote the whole code and this is what I came up with

fun climbingLeaderboard(scores: Array<Int>, alice: Array<Int>): MutableList<Int> {
    val highScores = scores.distinct()
    val aliceRank = mutableListOf<Int>()
    var lastI = highScores.lastIndex
    loop@ for (al in alice.indices) {
        for (hi in lastI.downTo(0)) {
            if (alice[al] >= highScores[hi]) {
                try {
                    aliceRank[al] = hi + 1
                } catch (e: IndexOutOfBoundsException) {
                    aliceRank.add(hi + 1)
                }
                lastI--
                println("if")
            } else if (alice[al] < highScores[hi]) {
                aliceRank.add(hi + 2)
                println("else if")
                continue@loop
            }
        }
    }
    return aliceRank
}

This deals with problem of being too resource heavy from the other three ways I tried above but it doesn't give the right answer and I have no idea why. I want to know what exactly is the flaw in my code, I have tried everything I could think of. Any help is appreciated.

Aucun commentaire:

Enregistrer un commentaire