简体   繁体   English

我为这个hackerrank测试编写的代码有效,但资源太多,现在我没有想法了。 我究竟做错了什么?

[英]The code I wrote for this hackerrank test works but is too resource and now I am out of ideas. What am I doing wrong?

New Year Chaos Problem: Hackerrank 新年混乱问题:Hackerrank

This is the program I wrote and it tells me that my code didn't finish in time (and that I need to optimize it for it to finish in less time).这是我编写的程序,它告诉我我的代码没有及时完成(我需要优化它以在更短的时间内完成)。 I have tried everything I could and I am out of ideas, what should I do?我已经尽我所能,但我没有想法,我该怎么办?

fun minimumBribes(q: Array<Int>) {
    var bribeCount = 0
    for ((i, x) in q.withIndex()) {

        val last = q.lastIndex - i + 1
        val first = i + 1


        // Checks if any of the elements have more than 2 elements smaller than them behind them in the queue and returns out of the function if so (because no element can bribe more than 2 elements)
        // Drops all the elements in the queue before x and counts the number of smaller elements after x in the queue
        if (q.drop(first).count { it < x } > 2) {
            println("Too chaotic")
            return
        }

        // Adds the number of elements bigger than x that are in the queue standing ahead of x to bribeCount (since the number of bribes taken by all elements will be equal to the total number of bribes given in total and no element can get past ahead of a smaller element without bribing it)
        bribeCount += q.dropLast(last).count { it > x }

    }
    println(bribeCount)
}

Read the problem first linked at the top of the page.阅读页面顶部首先链接的问题。 You probably won't understand anything below this line before reading the problem statement.在阅读问题陈述之前,您可能不会理解此行以下的任何内容。

As you can see, to check the number of people that have bribed an element, I count the number of elements that have a bigger value from that element and are standing before it in the queue.如您所见,为了检查贿赂一个元素的人数,我计算了该元素中具有更大值并在队列中站在它前面的元素的数量。 By summing the number of bribes each element has recieved, we can count the number of bribes given in total.通过将每个元素收到的贿赂数量相加,我们可以计算出总共提供的贿赂数量。 But before that, I calculate the number of elements smaller than an elements standing after it in the queue and if that number is greater than 2, it means said element has bribed more than 2 people which is "Too chaotic" and I return.但在此之前,我计算了比队列中站在它后面的元素小的元素数,如果该数字大于 2,则表示该元素贿赂了 2 个人以上,这“太混乱了”,我返回。

And this is the error message I get: Screenshot这是我收到的错误消息:截图

The problem with your code is that it uses too many loops.您的代码的问题在于它使用了太多循环。

  1. Both Array.drop(Int) and Array.dropLast(Int) use a loop to construct a List , which they return. Array.drop(Int)Array.dropLast(Int)使用循环来构造它们返回的List
  2. Iterable<T>.count((T) -> Boolean) uses a loop to count the number of times the condition specified is true. Iterable<T>.count((T) -> Boolean)使用循环来计算指定条件为真的次数。

To optimize your code, drop these extension functions that use loop.要优化您的代码,请删除这些使用循环的扩展函数。

fun minimumBribes(queue: Array<Int>): Unit {
    var bribeCount = 0

    for ((index, initialPosition) in queue.withIndex()) {
        val currentPosition = index + 1

        if ((initialPosition - currentPosition) > 2) {
            println("Too chaotic")
            return
        }

        val startingCheckIndex = max(0, initialPosition - 2)

        for (checkIndex in startingCheckIndex until index) {
            val hasBribed = queue[checkIndex] > initialPosition
            if (hasBribed) bribeCount++
        }
    }

    println(bribeCount)
}

Clarification:澄清:

For each number in the queue:对于队列中的每个数字:

1: We check how far a person has moved up in the queue ie initialPosition - currentPosition . 1:我们检查一个人在队列中移动了多远,即initialPosition - currentPosition If he/she has moved up more than 2 positions then we print "Too chaotic" and return control because it is not possible for the person to bribe more than 2 people ahead of him/her.如果他/她的位置上升了 2 个以上,那么我们打印“Too chaotic”并返回控制权,因为这个人不可能贿赂他/她前面的 2 个以上的人。

For example, if the person's sticker says 6 and his current position is 3 then the person has moved up 3 positions, which is not possible.例如,如果此人的贴纸是 6,而他当前的位置是 3,则此人已向上移动了 3 个位置,这是不可能的。

2: You mentioned in the question, I count the number of elements that have a bigger value from that element and are standing before it in the queue . 2:您在问题中提到, I count the number of elements that have a bigger value from that element and are standing before it in the queue

This is correct as we are counting how many bribes a person has taken but we cannot check right up to the first position.这是正确的,因为我们正在计算一个人接受了多少贿赂,但我们无法直接检查到第一个位置。 As a person that bribes another person can only move one position ahead of him, we will only check up to 1 position ahead of the initial position of the person.由于贿赂他人的人只能向前移动一个位置,因此我们只会在该人的初始位置前最多检查一个位置。 I have called the index for this position startingCheckIndex in my code.我在我的代码中调用了这个位置的索引startingCheckIndex检查索引。

For example: If the person's sticker says 5 and the current position is 10. then we will check only from position 4 up to 10.例如:如果这个人的贴纸是 5 而当前位置是 10,那么我们只会从位置 4 到 10 进行检查。

Note that I have used max(0, initialPosition - 2) .请注意,我使用了max(0, initialPosition - 2)
-2 because the array index starts from 0 and initialPosition starts from 1. -2因为数组索引从 0 开始,而 initialPosition 从 1 开始。
max(0,..) because for initialPosition 1, 1 - 2 = -1 so we need to maintain minimum value of 0. max(0,..)因为对于 initialPosition 1, 1 - 2 = -1所以我们需要保持最小值为 0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM