简体   繁体   English

优化数组算法以找到 j 的最大值 - i 受到 A[i] <= A[j] 的约束

[英]Optimize array algo for finding maximum of j - i subjected to the constraint of A[i] <= A[j]

Given an array A[] of N positive integers.给定一个包含 N 个正整数的数组 A[]。 The task is to find the maximum of j - i subjected to the constraint of A[i] <= A[j].任务是在 A[i] <= A[j] 的约束下找到 j - i 的最大值。

Input: The first line contains an integer T, depicting total number of test cases.输入:第一行包含一个 integer T,描述了测试用例的总数。 Then T test case follows.然后是 T 测试用例。 First line of each test case contains an integer N denoting the size of the array.每个测试用例的第一行包含一个 integer N 表示数组的大小。 Next line contains N space separated integeres denoting the elements of the array.下一行包含 N 个空格分隔的整数,表示数组的元素。

Output: Print the maximum difference of the indexes i and j in a separtate line. Output:在单独的行中打印索引 i 和 j 的最大差值。

Constraints: 1 ≤ T ≤ 1000 1 ≤ N ≤ 107 0 ≤ A[i] ≤ 1018约束: 1 ≤ T ≤ 1000 1 ≤ N ≤ 107 0 ≤ A[i] ≤ 1018

Example: Input: 1 9 34 8 10 3 2 80 30 33 1示例:输入:1 9 34 8 10 3 2 80 30 33 1

Output: 6 Output:6

I have written solution for O(n).我已经为 O(n) 编写了解决方案。 But I am getting "time limit exceeded" error while submitting this solution.但是在提交此解决方案时出现“超出时间限制”错误。

To solve this problem, we need to get two optimum indexes of arr[]: left index i and right index j.为了解决这个问题,我们需要得到arr[]的两个最优索引:左索引i和右索引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].对于一个元素 arr[i],如果在 arr[i] 的左侧有一个小于 arr[i] 的元素,我们不需要考虑 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.类似地,如果 arr[j] 右侧有一个更大的元素,那么我们不需要考虑这个 j 作为右索引。 So we construct two auxiliary arrays min[] and max[] such that min[i] holds the smallest element on left side of arr[i] including arr[i], and max[j] holds the greatest element on right side of arr[j] including arr[j].所以我们构造了两个辅助 arrays min[] 和 max[] 使得 min[i] 保存 arr[i] 左侧的最小元素,包括 arr[i], max[j] 保存右侧的最大元素arr[j] 包括 arr[j]。 After constructing these two auxiliary arrays, we traverse both of these arrays from left to right.在构造完这两个辅助 arrays 之后,我们从左到右遍历这两个 arrays。 While traversing min[] and max[] if we see that min[i] is greater than max[j], then we must move ahead in min[] (or do i++) because all elements on left of min[i] are greater than or equal to min[i].在遍历 min[] 和 max[] 时,如果我们看到 min[i] 大于 max[j],那么我们必须在 min[] 中前进(或执行 i++),因为 min[i] 左侧的所有元素都是大于或等于 min[i]。 Otherwise we must move ahead in max[j] to look for a greater j – i value否则我们必须在 max[j] 中继续寻找更大的 j - i 值

Can we optimize it more?我们可以进一步优化它吗?

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

 class GFG {
   public static void main (String[] args) {

        Scanner in =new Scanner(System.in);
        int noOfUsecases=in.nextInt();

        for(int i=0;i<noOfUsecases;i++){
        int arrayLength=in.nextInt();
        int[] arr=new int[arrayLength];
        if(in.hasNextInt()){

            for(int j=0;j<arrayLength;j++){
                arr[j]=in.nextInt();
            }

        }
        maximumIndex(arr,arrayLength);
    }
}
public static void maximumIndex(int[] arr,int length){
    int i = 1,j = length-2;
    int maxDiff=0;
    int[] min=new int[length];
    min[0]=arr[0];
    for(;i<length;++i){
        min[i]=arr[i]<min[i-1]?arr[i]:min[i-1];
    }
    int[] max=new int[length];
    max[length-1]=arr[length-1];
    for(;j>=0;--j){
        max[j]=arr[j]>max[j+1]?arr[j]:max[j+1];
    }
    i = 0;j = 0;
    while (j < length && i < length)  
    { 
        if (min[i] <= max[j])  
        { 
            maxDiff = maxDiff>(j - i)?maxDiff:(j-i); 
            j = j + 1; 
        }  
        else 
            i = i + 1; 
    }
    System.out.println(maxDiff);
}}

This problem obviously can't be solved faster, than O(n) (you need to read input data, at least), but your problem is that you are using the scanner to input your data, and it is very slow.这个问题显然不能比 O(n) 更快地解决(至少您需要读取输入数据),但是您的问题是您正在使用扫描仪输入数据,而且速度很慢。 Here you can find multiple solutions to this problem. 在这里,您可以找到此问题的多种解决方案。 Pick one that meets your requirements.选择一个符合您要求的。

找到对 (i,j) 使得 i <j and (a[i] + a[j]) is maximum< div><div id="text_translate"><p> 给定一个未排序的数组——找到一对 arr[i] 和 arr[j] 使得arr[i] &lt; arr[j] &amp; i&lt;j并且(arr[i] + arr[j])是最大的。</p><p> 预期时间复杂度 - O(n)</p><p> 对于数组a = {4, 1, 3, 2, 5, 3}</p><pre> pair is (4, 5).</pre><p> 这是我试过的代码..</p><pre> void findPair(int[] a){ int n = a.length; int max = a[0]; int secondMax = Integer.MIN_VALUE; for(int i=1; i&lt;n; i++){ if(a[i]&gt;max){ secondMax = max; max = a[i]; } } if(secondMax == Integer.MIN_VALUE){ System.out.println("-1 -1"); } else{ System.out.println(secondMax+" "+max); } }</pre></div></j> - Find the pair (i,j) such that i<j and and (a[i] + a[j]) is maximum

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找数组中A [i] * A [j]&gt; A [i] + A [j]的对数 - Finding number of pairs in array where A[i] * A[j] > A[i] + A[j] 最大ij,因此A [i]&gt; = A [j] - maximum i-j , so that A[i]>=A[j] 最大值 (a[j] - a[i]) * min(b[j], b[i]) 使得 j &gt; i? - Maximum (a[j] - a[i]) * min(b[j], b[i]) such that j > i? 找到对 (i,j) 使得 i <j and (a[i] + a[j]) is maximum< div><div id="text_translate"><p> 给定一个未排序的数组——找到一对 arr[i] 和 arr[j] 使得arr[i] &lt; arr[j] &amp; i&lt;j并且(arr[i] + arr[j])是最大的。</p><p> 预期时间复杂度 - O(n)</p><p> 对于数组a = {4, 1, 3, 2, 5, 3}</p><pre> pair is (4, 5).</pre><p> 这是我试过的代码..</p><pre> void findPair(int[] a){ int n = a.length; int max = a[0]; int secondMax = Integer.MIN_VALUE; for(int i=1; i&lt;n; i++){ if(a[i]&gt;max){ secondMax = max; max = a[i]; } } if(secondMax == Integer.MIN_VALUE){ System.out.println("-1 -1"); } else{ System.out.println(secondMax+" "+max); } }</pre></div></j> - Find the pair (i,j) such that i<j and and (a[i] + a[j]) is maximum 给定未排序的数组,找到A [j] - A [i]的最大值,其中j> i..in O(n)时间 - Given an unsorted Array find maximum value of A[j] - A[i] where j>i..in O(n) time 找到A的最大值[J] + - A [I] - Finding Max Value Of A[J] +- A[I] 在o(1)的整数数组中找到i和j之间的元素数 - Finding the number of elements between i and j in an integer array in o(1) 求|的最大值 Ai-Aj | + | i-j | - Find the maximum value of | Ai - Aj | + | i - j | 使用J在数组中查找 - Finding ones in an array with J 是否有更短的方法来找到答案,其中m [i,j] = m [i-1,j] + m [i,j-1] + 1的m [i,j]值? - Is there a shorter method to finding the answer the value of m[i,j] where m[i, j] = m[i-1,j] + m[i, j-1] + 1?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM