Maximum Index

Given an array arr[], find the maximum j – i such that arr[j] > arr[i]

Given an array arr[], find the maximum j – i such that arr[j] > arr[i].

Examples:

  Input: {34, 8, 10, 3, 2, 80, 30, 33, 1}
  Output: 6  (j = 7, i = 1)

  Input: {9, 2, 3, 4, 5, 6, 7, 8, 18, 0}
  Output: 8 ( j = 8, i = 0)

  Input:  {1, 2, 3, 4, 5, 6}
  Output: 5  (j = 5, i = 0)

  Input:  {6, 5, 4, 3, 2, 1}
  Output: -1 
思路1: 暴力解,用两层循环扫,更新最大值。
思路2:想再提高就得保存之前的信息,我们生成一个leftmin,保存的是到i为止,往左看最小的信息,然后生成一个rightmax,是往右看,目前为止最大的值,这样做的好处就是,我们就不用管最左边和最右边的信息了,只用管当前就行了,这样子的更本目的就是用空间换时间,存储了一部分信息,这样再次遍历的时候,就可以提速。

To solve this problem, we need to get two optimum indexes of arr[]: left index i and right index j. For an element arr[i], we do not need to consider arr[i] for left index if there is an element smaller than arr[i] on left side of arr[i]. Similarly, if there is a greater element on right side of arr[j] then we do not need to consider this j for right index. So we construct two auxiliary arrays LMin[] and RMax[] such that LMin[i] holds the smallest element on left side of arr[i] including arr[i], and RMax[j] holds the greatest element on right side of arr[j] including arr[j]. After constructing these two auxiliary arrays, we traverse both of these arrays from left to right. While traversing LMin[] and RMa[] if we see that LMin[i] is greater than RMax[j], then we must move ahead in LMin[] (or do i++) because all elements on left of LMin[i] are greater than or equal to LMin[i]. Otherwise we must move ahead in RMax[j] to look for a greater j – i value.

Thanks to celicom for suggesting the algorithm for this method.

import java.util.*;
import java.lang.*;
import java.io.*;

class GFG {
    public static int calcualteMax1(int[] array) {
        int max = 0;
        for(int i=0; i<array.length; i++){
            for(int j=i+1; j<array.length; j++){
                if(array[i]<array[j]){
                    max = Math.max(max, j-i);
                }
            }
        }
        return max;
    }
    
    public static int calcualteMax2(int[] array) {
        int[] leftmin = new int[array.length];
        int[] rightmax = new int[array.length];
        
        leftmin[0] = array[0];
        for(int i=1; i<array.length; i++){
            if(array[i]<leftmin[i-1]){
                leftmin[i] = array[i];
            } else {
                leftmin[i] = leftmin[i-1];
            }
        }
        
        rightmax[array.length-1] = array[array.length-1];
        for(int j=array.length-2; j>=0; j--){
            if(array[j]>rightmax[j+1]){
                rightmax[j] = array[j];
            } else{
                rightmax[j] = rightmax[j+1];
            }
        }
        
        int i=0;  int j=0; int max = 0;
        while(i<array.length && j<array.length){
            if(leftmin[i]<rightmax[j]){
                max = Math.max(max, j-i);
                j++;
            }else{
                i++;
            }
        }
        return max;
    }
    
	public static void main (String[] args) {
		//code
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		
		for(int i=0; i<n; i++){
		    int length = scanner.nextInt();
		    int[] array = new int[length];
		    for(int j=0; j<length; j++){
		        array[j] = scanner.nextInt();
		    }
		    System.out.println(calcualteMax2(array));
		}
	}
	
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值