【发布时间】:2015-08-07 21:03:31
【问题描述】:
由 N 个正整数组成的数组 A。数组 A 有 N × (N+1) / 2 非空连续子数组。
我们必须计算所有连续子数组中存在的最大元素。
例如:
1 2 3
Subarray List :
[1]
[2]
[3]
[1,2]
[2,3]
[1,2,3]
Maximum Element :
[1]
[2]
[3]
[2]
[3]
[3]
我的方法:
使用Segment Tree查询区间中存在的最大元素
代码:
public static void get_max_freq(int a , int b , ArrayList<Long> freq ,ArrayList<Integer> P , int n , int[] A){
if(a>b) return;
int index = query(1,0,n, a, b, A); // Segment Tree O(Logn)
long temp = (index-a+1)*(b-index+1);
freq.add(temp);
P.add(A[index));
get_max_freq(a,index-1, freq, P, n, A);
get_max_freq(index+1, b, freq, P,n, A);
}
如果元素在数组中不是唯一的,我想知道我的解决方案是否正确。
有没有比这更快更好的解决方案。
【问题讨论】:
-
如果您的代码有效,但您正在寻求改进,您应该在codereview.stackexchange.com 上提问这个问题,而不是 SO。
-
在回答之前知道这是来自在线竞赛!
标签: java algorithm data-structures