面试:数组:最大下标距离

  • 给定一个整形数组,找出最大下标距离 ji , 当且 A[i]<A[j]i<j
  • 复杂度:三次扫描,每次的复杂度 O(N)

  • 算法:{5,3,4,0,1,4,1}

    1. 找出从第一个元素开始的下降序列{5,3,0}
    2. i=3,j=6, j从尾部扫描
    3. 初始化,i=3, j=6, A[i]=0
//HelloDate.java
import java.util.*;


public class MyDemo{

    public int maxIndexDistance(int[] A){
        if(A==null || A.length<2)
            return 0;
        boolean inDescSeq[]=new boolean[A.length];
        int min=A[0], n=A.length;
        inDescSeq [0]=true;
        for(int i=1;iif(A[i]//下降序列的标记
                inDescSeq[i]=true;
                min=A[i];
            }
        }

        int maxDist=0,i=n-1,j=n-1;
        while(i>=0){
            if(inDescSeq[i]==false){
                i--; //倒叙找到下一个下降序列
                continue;
            }
            while((A[j]<=A[i])&& (j>i))
                j--; //从后往前移动,直至找到符合的元素
            if((j-i)>maxDist){
                maxDist=j-i;
            }
            i--;
        }
        return maxDist;

    }

     public static void main(String[] args) {
         MyDemo demo=new MyDemo();
         int[] test={5,2,4,0,1,4,1};
         int result=demo.maxIndexDistance(test);
         System.out.println(result);         

         }
     }

c++

//
//  main.cpp
//  testProject
//
//  Created by 健 米 on 16-4-25.
//  Copyright (c) 2016年 __MyCompanyName__. All rights reserved.
//

#include 
#include 
#include 
#include 
#include 
using namespace std;

class Solution{
public:
    int maxIndexDistance(int A[],int length){

        if(length<2)
            return 0;
        bool inDescSeq[length];
        int min=A[0],n=length;
        inDescSeq[0]=true;

        for(int i=1;iif(A[i]//标记下降序列
                inDescSeq[i]=true;
                min=A[i];
            }

        }
        int maxDist=0,i=n-1,j=n-1;
        while(i>=0){
            if(inDescSeq[i]==false){
                i--;
                continue;
            }
            while((A[j]<=A[i]) && (j>i))
                j--;
            if((j-i) > maxDist)
                maxDist=j-i;
            i--;
        }

        return maxDist;

    }


};

int main (int argc, const char * argv[])
{
    Solution s;
    int test[]={5,2,4,0,1,4,1};
    int length=sizeof(test)/sizeof(int);

    int res=s.maxIndexDistance(test,length);
    cout<

  • vector
//
//  main.cpp
//  testProject
//
//  Created by 健 米 on 16-4-25.
//  Copyright (c) 2016年 __MyCompanyName__. All rights reserved.
//

#include 
#include 
#include 
#include 
#include 
using namespace std;

class Solution{
public:
    int maxIndexDistance(vector<int> &nums){

        int length = nums.size();        
        if(length<2)
            return 0;
        bool inDescSeq[length];
        int min=nums[0],n=length;
        inDescSeq[0]=true;

        for(int i=1;iif(nums[i]//标记下降序列
                inDescSeq[i]=true;
                min=nums[i];
            }

        }
        int maxDist=0,i=n-1,j=n-1;
        while(i>=0){
            if(inDescSeq[i]==false){
                i--;
                continue;
            }
            while((nums[j]<=nums[i]) && (j>i))
                j--;
            if((j-i) > maxDist)
                maxDist=j-i;
            i--;
        }

        return maxDist;

    }


};

int main (int argc, const char * argv[])
{
    Solution s;
    vector<int> test;
    int n,t;
    cin >> n;

    while(n--)
    {
        cin>>t;
        test.push_back(t);
    }

   // int length=test.size();

    int res=s.maxIndexDistance(test);
    cout<

你可能感兴趣的:(面试(java))