logn* loglogn

问题描述

昨晚我正在解决一个问题,必须将它插入优先级队列n次。因此,渐近复杂度为n log n。但是n可能高达10 ^ 16,所以我必须做得更好。我找到了一个解决方案,该解决方案使我仅需插入n次优先级队列日志即可,而其他所有时间均保持不变。因此,复杂度为log(n)* log(log(n))。是我的渐近复杂性,还是可以进一步简化?

这是算法。通过使用哈希图计算将插入优先级队列的重复优先级并基于此提供单个计算,我能够降低复杂性。

通过我的代码,我知道如何将n log n的复杂度降低到log n log log n并不能理解。我必须通过示例来找出n减少为log n的情况。尽管solvedUpTo以前以与n相同的速率增加,但现在〜n

该代码故意与所要解决的内容模棱两可:

fun  solve(n:Long,x:Long,y:Long): Long {

    val numCount = mutableMapOf<Long,Long>()

    val minQue: PriorityQueue<Long> = PriorityQueue<Long>()

    addToQueue(numCount,minQue,x,1)
    addToQueue(numCount,y,1)

    var answer = x + y

    var solvedUpTo = 2L

    while (solvedUpTo < n) {

        val next = minQue.poll()

        val nextCount = numCount.remove(next)!!

        val quantityToSolveFor = min(nextCount,n - solvedUpTo)

        answer = ((answer + ((next + x + y) * quantityToSolveFor))).rem(1000000007)

        addToQueue(numCount,next + x,quantityToSolveFor)
        addToQueue(numCount,next + y,quantityToSolveFor)

        solvedUpTo += quantityToSolveFor
    }

    return answer
}

fun <K> addToQueue(numCount: MutableMap<K,Long>,minQue: PriorityQueue<K>,num: K,incrementBy: Long) {

    if (incrementMapAndCheckIfNew(numCount,num,incrementBy)) {
        minQue.add(num)
    }
}

//Returns true if just added
fun <K> incrementMapAndCheckIfNew(map: MutableMap<K,key: K,incrementBy: Long): Boolean {

    val prevKey = map.putIfAbsent(key,0L)

    map[key] = map[key]!! + incrementBy

    return prevKey == null
}

解决方法

不,O(log n log log n)被简化为表达式将要得到。有时您会在数论上下文中看到像O(n log n log log n)这样的运行时,并且没有简单的通用函数可以使这些数量相等。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...