Range minimum query

Finds the position of the minimum element between two given indices in an array in constant time.
123.0 次下载
更新时间 2014/12/22

查看许可证

Given an array A[1...N], it finds the position of the element with the minimum value between two given indices.
It returns the answer in constant time. The RMQ problem can be used to recover the lca (lowest common ancestor) in a graph in constant time. See http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=lowestCommonAncestor for more details.
Time complexity: O(N log(N))
Space complexity: O(N log(N))
Example
A = [-1 5 0 3 -6 4 2 1 0 -1];
N = length(A);
M = rmq(A);
% Find the position of the minimum element between indices i, j
i = 2;
j = 6;
k = floor(log2(j - i + 1));
if A(M(i,k+1)) <= A(M(j-2^k+1,k+1))
idx = M(i,k+1);
else
idx = M(j-2^k+1,k+1);
end

引用格式

Georgios Papachristoudis (2024). Range minimum query (https://www.mathworks.com/matlabcentral/fileexchange/48841-range-minimum-query), MATLAB Central File Exchange. 检索来源 .

MATLAB 版本兼容性
创建方式 R2014b
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Matrices and Arrays 的更多信息
标签 添加标签
rmq

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
版本 已发布 发行说明
1.1.0.0

I changed the title to a more descriptive one and added an image.

1.0.0.0