您的位置:首页 > 其它

[LeetCode] A Distance Maximizing Problem

2015-01-19 09:42 1601 查看
Question:
Given an array A of integers, find the maximum of j-i subjected to the constraint of A[i] < A[j].

// O(n)
public int maxDistance(int[] A)
{
// Assumptions...

int local = Integer.MIN_VALUE;
int global = Integer.MIN_VALUE;

for (int i = 1 ; i < A.length ; i ++)
{
if (A[i] > A[i - 1])
{
local += 1;
}
else
{
local = 0;
}
global = Math.max(global, local);
}

return global;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LeetCode