Java 求最大連續子序列積及其起始結束座標

來源:互聯網
上載者:User
public class Test {    // 最大連續子序列積    // 假設有集合A,Max[n]表示從A[0]~A[n]的以A[n]結尾的最大連續子序列積,    // Min[n]表示從A[0]~A[n]的以A[n]結尾的最小子序列積,則有如下遞推式    // Max[n] = Max{ Max[n-1]*A[n], A[n], Min[n-1]*A[n] }    // Min[n] = Min{ Max[n-1]*A[n], A[n], Min[n-1]*A[n] }    private static void getMaxProduct(float[] a) {        float[] maxArr = new float[a.length];        float[] minArr = new float[a.length];        maxArr[0] = a[0];        minArr[0] = a[0];        float maxProduct = a[0];        int maxStartInx = 0;    //最大連續序列積開始座標        int maxEndInx = 0;  //最大連續序列積結束座標        int curMaxStartInx = 0; //當前最大積開始座標        int preMaxStartInx = 0; //上一個最大積開始座標        int curMinStartInx = 0; //當前最小積開始座標        int preMinStartInx = 0; //上一個最小積開始座標        for (int i = 1; i < a.length; i++) {            maxArr[i] = Math.max(a[i], Math.max(maxArr[i - 1] * a[i], minArr[i - 1] * a[i]));            minArr[i] = Math.min(a[i], Math.min(maxArr[i - 1] * a[i], minArr[i - 1] * a[i]));            if (maxProduct < maxArr[i]) {                maxProduct = maxArr[i];                                //序列座標更新                if (maxArr[i] == a[i]) {                    maxStartInx = maxEndInx = i;                } else if (maxArr[i] == maxArr[i - 1] * a[i]){
            maxStartInx = preMaxStartInx;
            maxEndInx = i; } else { // 如果maxArr[i]是通過minArr[i-1]*a[i]得到, // 說明其序列開始座標等於上一個元素的最小連續序列積的開始座標 maxStartInx = preMinStartInx; maxEndInx = i; } } //記錄當前最小連續子序列積的開始座標 if (minArr[i] == a[i]) { curMinStartInx = i; } else if (minArr[i] == maxArr[i - 1] * a[i]) { curMinStartInx = preMaxStartInx; } //記錄當前最大連續子序列積的開始座標 if (maxArr[i] == a[i]) { curMaxStartInx = i; } else if (maxArr[i] == minArr[i - 1] * a[i]){ curMaxStartInx = preMinStartInx; } preMaxStartInx = curMaxStartInx; preMinStartInx = curMinStartInx; } System.out.println("MaxProduct: " + maxProduct + " Start: " + maxStartInx + " End: " + maxEndInx); } public static void main(String[] args) { float[] a = {1.4f, 8, 0.9f, -2, 0.7f, -4, 0, 3, 9, -1, 0.55f, 100, -2}; getMaxProduct(a); }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.