我的任務有問題,需要我解決類似於範圍最小查詢的問題。這個問題大致描述如下:僞範圍最小查詢
我應該編寫一個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](http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=lowestCommonAncestor#Range_Minimum_Query_%28RMQ%29)。 – Dukeling
嗨。感謝這篇文章。我發現它非常有幫助。是否可以使用基於堆棧的解決方案來解決此問題?我很想知道這一點,因爲任務簡介實際上指出rmq不是必需的,並且堆棧足以解決問題。 – LanceHAOH
如果您被告知要使用堆棧,我的猜測是您錯過了某個問題描述的詳細信息,或者錯誤地描述了它,因爲如前所述,它是RMQ,我不相信有一個有效的基於堆棧的解決方案(我們知道的)。 – Dukeling