【发布时间】:2011-01-24 22:10:53
【问题描述】:
鉴于以下问题,
给定一个长度为 n 的整数数组 A,找出最长的序列 {i_1, ..., i_k} 使得 i_j
这是我的解决方案,正确吗?
max_start = 0; // store the final result
max_end = 0;
try_start = 0; // store the initial result
try_end = 0;
FOR i=0; i<(A.length-1); i++ DO
if A[i] <= A[i+1]
try_end = i+1; // satisfy the condition so move the ending point
else // now the condition is broken
if (try_end - try_start) > (max_end - max_start) // keep it if it is the maximum
max_end = try_end;
max_start = try_start;
endif
try_start = i+1; // reset the search
try_end = i+1;
endif
ENDFOR
// Checking the boundary conditions based on comments by Jason
if (try_end - try_start) > (max_end - max_start)
max_end = try_end;
max_start = try_start;
endif
不知何故,我不认为这是一个正确的解决方案,但我找不到不赞成该解决方案的反例。
有人可以帮忙吗?
谢谢
【问题讨论】:
-
对我来说看起来不错。你能解释一下为什么你认为它不正确吗?
标签: algorithm