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

  • 给定一个整形数组,找出最大下标距离 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;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值