[LeetCode] A Distance Maximizing Problem
Question:
Given an array A of integers, find the maximum of j-i subjected to the constraint of A[i] < A[j].
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[i - 1])
{
local += 1;
}
else
{
local = 0;
}
global = Math.max(global, local);
}
return global;
}