【发布时间】:2013-10-23 00:02:42
【问题描述】:
我的作业有问题,需要我解决类似于 range-minimum-query 的问题。问题大致描述如下:
我应该编写一个 java 程序,它读取大量整数(大约 100,000)并将它们存储到一些数据结构中。然后,我的程序必须回答给定范围 [i,j] 中的最小数字的查询。我已经成功地设计了一种算法来解决这个问题。但是,它还不够快。
我的算法的伪代码如下:
// Read all the integers into an ArrayList
// For each query,
// Read in range values [i,j] (note that i and j is "actual index" + 1 in this case)
// Push element at index i-1 into a Stack
// Loop from index i to j-1 in the ArrayList (tracking the current index with variable k)
[Begin loop]
// If element at k is lesser than the one at the top of the stack, push the element at k into the Stack.
[End of loop]
有人可以告诉我我可以做什么,以便我的算法足够快来解决这个问题吗?
作业文件可以在这个链接找到:http://bit.ly/1bTfFKa
我被这个问题困扰了好几天。任何帮助将非常感激。 谢谢。
【问题讨论】:
-
您的问题听起来像完全的范围最小查询,有足够的材料可供使用。例如,请参阅this。
-
嗨。谢谢你的文章。我发现它很有帮助。是否可以使用基于堆栈的解决方案来解决此问题?我想知道这一点,因为任务简介实际上指出不需要 rmq 并且堆栈足以解决问题。
-
如果你被告知使用堆栈,我猜你错过了问题描述的一些细节或描述不正确,因为如前所述,它是 RMQ,我不相信那里是一种有效的基于堆栈的解决方案(我们知道)。