Maximum Width Ramp LT962

时间:2023-03-09 03:41:16
Maximum Width Ramp LT962

Given an array A of integers, a ramp is a tuple (i, j) for which i < j and A[i] <= A[j].  The width of such a ramp is j - i.

Find the maximum width of a ramp in A.  If one doesn't exist, return 0.

Example 1:

Input: [6,0,8,2,1,5]
Output: 4
Explanation:
The maximum width ramp is achieved at (i, j) = (1, 5): A[1] = 0 and A[5] = 5.

Example 2:

Input: [9,8,1,0,1,9,4,0,4,1]
Output: 7
Explanation:
The maximum width ramp is achieved at (i, j) = (2, 9): A[2] = 1 and A[9] = 1.

Note:

  1. 2 <= A.length <= 50000
  2. 0 <= A[i] <= 50000
 Idea 1. building decreasing array, binary search the largest element not smaller than the target(the other elements in the array).
Time complexity: O(nlogn)
Space complexity: O(n)
 class Solution {
private int findLargestElementIndexNotLargerThan(int[] A, List<Integer> indexA, int target) {
int left = 0, right = indexA.size()-1;
while(left <= right) {
int mid = left + (right - left)/2;
if(A[indexA.get(mid)] == target) {
return indexA.get(mid);
}
else if(A[indexA.get(mid)] > target) {
left = mid + 1;
}
else {
right = mid-1;
}
} return indexA.get(left);
} public int maxWidthRamp(int[] A) {
List<Integer> indexA = new ArrayList<>();
int result = 0;
for(int i = 0; i < A.length; ++i) {
if(indexA.isEmpty() || A[indexA.get(indexA.size()-1)] > A[i]) {
indexA.add(i);
}
else {
int targetIndex = findLargestElementIndexNotLargerThan(A, indexA, A[i]);
result = Math.max(result, i - targetIndex);
}
} return result;
}
}

binary search

 class Solution {
private int findLargestElementIndexNotLargerThan(int[] A, List<Integer> indexA, int target) {
int left = 0, right = indexA.size()-1;
while(left < right) {
int mid = left + (right - left)/2;
if(A[indexA.get(mid)] > target) {
left = mid + 1;
}
else {
right = mid;
}
} return indexA.get(left);
} public int maxWidthRamp(int[] A) {
List<Integer> indexA = new ArrayList<>();
int result = 0;
for(int i = 0; i < A.length; ++i) {
if(indexA.isEmpty() || A[indexA.get(indexA.size()-1)] > A[i]) {
indexA.add(i);
}
else {
int targetIndex = findLargestElementIndexNotLargerThan(A, indexA, A[i]);
result = Math.max(result, i - targetIndex);
}
} return result;
}
}

Idea 1.b. using treeSet in java, using floor to save writing binary search

 class Solution {
public int maxWidthRamp(int[] A) {
Comparator<Integer> cmp = (a, b) -> Integer.compare(A[a], A[b]); TreeSet<Integer> indexA = new TreeSet<>(cmp);
int result = 0;
for(int i = 0; i < A.length; ++i) {
if(indexA.isEmpty() || A[indexA.first()] > A[i]) {
indexA.add(i);
}
else {
int targetIndex = indexA.floor(i);
result = Math.max(result, i - targetIndex);
}
} return result;
}
}

Idea 1.c using TreeMap with index, saving customerised comparator

 class Solution {
public int maxWidthRamp(int[] A) {
TreeMap<Integer, Integer> indexA = new TreeMap<>();
int result = 0;
for(int i = 0; i < A.length; ++i) {
Integer targetValue = indexA.floorKey(A[i]);
if(targetValue == null) {
indexA.put(A[i], i);
}
else {
result = Math.max(result, i - indexA.get(targetValue));
}
} return result;
}
}

Idea 2. buiding the decreasing array to maintain all the possible smaller candidates during the first loop of the array,  during 2nd loop, scanning the array from right to left, the top of element is at least <= current number, this also explains why descending order, we need to look back for a smaller or equal value, a descending order stack can guarantee that the top element is always smaller or equal to the current element.

if A[stack.top()] <= A[right], there is no pair between stack.top() and right which could have bigger gap than right - stack.top(), hence stack pop(), continue

Time complexity: O(n)

Space compexity: O(n)

 class Solution {
public int maxWidthRamp(int[] A) {
Deque<Integer> indexA = new ArrayDeque<>();
int result = 0;
for(int i = 0; i < A.length; ++i) {
if(indexA.isEmpty() || A[indexA.peek()] > A[i]) {
indexA.push(i);
}
} for(int right = A.length-1; right+1 > result; --right) {
while(!indexA.isEmpty() && A[indexA.peek()] <= A[right]) {
result = Math.max(result, right - indexA.peek());
indexA.pop();
}
}
return result;
}
}

Idea 3. Sorted the array based on index, the maximum ramp ending at each index i = i - min(previousIndex), the smallest index which has smaller value

Time complexity: O(nlogn)

Space complexity: O(n)

 class Solution {
public int maxWidthRamp(int[] A) {
List<Integer> indexA = new ArrayList<>();
for(int i = 0; i < A.length; ++i) {
indexA.add(i);
} Comparator<Integer> cmp = (a, b) -> {
int c = Integer.compare(A[a], A[b]);
if(c == 0) {
return Integer.compare(a, b);
}
return c;
}; Collections.sort(indexA, cmp); int minPrev = A.length;
int result = 0;
for(int index: indexA) {
result = Math.max(result, index - minPrev);
minPrev = Math.min(minPrev, index);
} return result;
}
}