java 找到最长的比特子阵列

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 找到最长的比特子阵列相关的知识,希望对你有一定的参考价值。

import java.util.*;
public class BitonicSubArray {
    class Pair {
        int start;
        int end;
        Pair(int start, int end) {
            this.start = start;
            this.end = end;
        }
        public String toString() {
            return "["+start + "," + end+"]";
        }
    }
    public static void main(String args[]) {
        BitonicSubArray bitonicSubArray = new BitonicSubArray();
        int[][] input = {
          null,
          {},
          {1},
          {3,5,8,4,5,9,10,8,5,3,4},
          {1,2,3,4,5},
          {5,4,3,2,1}
        };
        for(int i=0; i< input.length; i++) {
            System.out.println("Input: " + Arrays.toString(input[i]) + " Result: " + bitonicSubArray.find(input[i]));
            System.out.println("Input: " + Arrays.toString(input[i]) + " Result: " + bitonicSubArray.findUsingOneLoop(input[i]));
        }
    }
    
    // For future reference
    //index -> 0, 1, 2, 3, 4, 5,  6, 7, 8, 9, 10
    
    //input -> 3, 5, 8, 4, 5, 9, 10, 8, 5, 3,  4
    //inc   -> 1, 2, 3, 1, 2, 3,  4, 1, 1, 2,  2
    //dec   -> 1, 1, 2, 1, 1, 1,  4, 3, 2, 1,  1
    
    //add   -> 2, 3, 5, 2, 3, 4,  8, 4, 3, 3,  3
    
    public Pair find(int[] input) {
        if(input == null || input.length < 1) {
            return null;
        }
        int[] inc = new int[input.length];
        inc[0]=1;
        // left -> right
        for(int i=1;i<input.length;i++) {
            if(input[i] > input[i-1]) {
                inc[i] = inc[i-1]+1;
            } else {
                inc[i] = 1;
            }
        }
        
        // right -> left
        int[] dec = new int[input.length];
        dec[input.length-1] = 1;
        for(int i=input.length-2;i>=0;i--) {
            if(input[i] > input[i+1]) {
                dec[i] = dec[i+1]+1;
            } else {
                dec[i] = 1;
            }
        }
        
        // now compare
        int maxBitonicLen = 0;
        Pair result = new Pair(-1,-1);
        for(int i=0;i<input.length;i++) {
            int currentLen = inc[i] + dec[i]-1;
            if(currentLen > maxBitonicLen) {
                maxBitonicLen = currentLen;
                result.start = i - inc[i] + 1;
                result.end = i + dec[i] -1;
            }
        }
        
        return result;
    }
    
    public Pair findUsingOneLoop(int[] input) {
        if(input == null || input.length < 1) {
            return null;
        }
        int i = 0;
        int maxBitonicLen = 0;
        Pair result = new Pair(0,0);
        while(i+1 < input.length) {
            int currentLen = 0;
            while(i+1 < input.length && input[i+1] > input[i]) {
                currentLen++;
                i++;
            }
            while(i+1 < input.length && input[i+1] < input[i]) {
                currentLen++;
                i++;
            }
            if(currentLen > maxBitonicLen){
                maxBitonicLen = currentLen;
                result.start = i - maxBitonicLen;
                result.end = i;
            }
        }
        return result;
    }
}

以上是关于java 找到最长的比特子阵列的主要内容,如果未能解决你的问题,请参考以下文章

java 找到具有给定总和的最大长度子阵列

java 找到具有相等数字1和0的最大长度子阵列

使用分而治之的最大子阵列产品有人吗?

leetcode 16. 最长回文子序列 java

leetcode 673. 最长递增子序列的个数 java

Java 求解最长递增子序列