1. 程式人生 > >面試:陣列:最大下標距離

面試:陣列:最大下標距離

  • 給定一個整形陣列,找出最大下標距離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;i<n;i++){ if(A[i]<min){ //下降序列的標記 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 <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
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;i<n;i++){
            if(A[i]<min){
                //標記下降序列
                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<<res<<endl;

}

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

#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
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;i<n;i++){
            if(nums[i]<min){
                //標記下降序列
                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<<res<<endl;

}