<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 97,  comments - 93,  trackbacks - 0
    To find a max segment in a array which includes negative and positive no.There r several methods to solve this question.And in the works, ignore the zero position.


      1package com.ibm.nicky.maxsegment;
      2
      3/**
      4 * @author QuQiang
      5 * 
      6 * The program can calculate the max sum segment
      7 * source[i]+source[i+1]+…+source[j]in source array,and return the
      8 * position.Ignore the zero position.
      9 */

     10public class MaxSegment {
     11
     12    /**
     13     * if start = -1 end = -1 is returned, the array should be initialized
     14     */

     15    private static int start = -1;
     16    private static int end = -1;
     17    private static int sum = 0;
     18
     19    private static int[] source = {-1,-2,-1,-10,-3,0,2,6,12,-5,6,-4};
     20
     21    /*
     22     * The main function to test the method.
     23     */

     24/*    public static void main(String[] args) {
     25        System.out.println(MaxSegment.getThrOptimizedMaxSegment()
     26                + ":" + MaxSegment.start + ":" + MaxSegment.end);
     27    }*/

     28
     29    /**
     30     * @return the sum of the max segment but the method is not very nice. the
     31     *         algorithmic complexity is T(n)=O(n^3)
     32     */

     33    public static int getMaxSegment() {
     34        start = -1;end = -1;sum = 0;
     35        for (int i = 0; i < source.length; i++{
     36            for (int j = i + 1; j < source.length; j++{
     37                int temp = 0;
     38                for (int k = i; k < j; k++{
     39                    temp += source[k];
     40                }

     41                if (temp > sum) {
     42                    sum = temp;
     43                    start = i;
     44                    end = j - 1;
     45                }

     46            }

     47        }

     48        return sum;
     49    }

     50
     51    /**
     52     * @return the sum of the max segment && use the previous result
     53     *         sum[i+1]=sum[i]+source[i+1]. algorithmic complexity is (n)=O(n^2)
     54     */

     55    public static int getFirOptimizedMaxSegment() {
     56        start = -1;end = -1;sum = 0;
     57        for (int i = 0; i < source.length; i++{
     58            int temp = 0;
     59            for (int j = i; j < source.length; j++{
     60                temp += source[j];
     61                if (temp > sum) {
     62                    sum = temp;
     63                    start = i;
     64                    end = j;
     65                }

     66            }

     67        }

     68        return sum;
     69    }

     70
     71    /**
     72     * @return the sum of the max segment && use the recursion
     73     *         formula.Division-and-Conquer and Recursion algorithm algorithmic
     74     *         complexity is T(n)=2T(n/2)+O(n),T(n)=O(nlogn).
     75     */

     76    public static int getSecondOptimizedMaxSegment(int leftend, int rightend) {
     77        start = -1;end = -1;sum = 0;
     78        int centerpiont = 0, leftsum = 0, rightsum = 0;// ,tempstart = -1
     79                                                        // ,tempend = -1;
     80        if (leftend == rightend)
     81            sum = source[leftend] > 0 ? source[leftend] : 0;
     82        else {
     83            centerpiont = (leftend + rightend) / 2;
     84            leftsum = getSecondOptimizedMaxSegment(leftend, centerpiont);
     85            rightsum = getSecondOptimizedMaxSegment(centerpiont + 1, rightend);
     86        }

     87        int templeftSum = 0, lefts = 0;
     88        for (int i = centerpiont; i > leftend - 1; i--{
     89            lefts += source[i];
     90            if (lefts > templeftSum) {
     91                templeftSum = lefts;
     92                // tempstart = i;
     93                start = i;
     94            }

     95        }

     96        int temprightSum = 0, rights = 0;
     97        for (int j = centerpiont + 1; j < rightend + 1; j++{
     98            rights += source[j];
     99            if (rights > temprightSum) {
    100                temprightSum = rights;
    101                // tempend = j;
    102                end = j;
    103            }

    104        }

    105        sum = templeftSum + temprightSum;
    106        if (sum < leftsum) {
    107            start = leftend;
    108            end = centerpiont;
    109            return sum = leftsum;
    110        }
     else if (sum < rightsum) {
    111            start = centerpiont + 1;
    112            end = rightend;
    113            return sum = rightsum;
    114        }
     else {
    115            // start = tempstart ;
    116            // end = tempend;
    117            return sum;
    118        }

    119    }

    120
    121    /**
    122     * @return the sum of the max segment && use the dynamic programming
    123     *         (DP).temp[i]=max(temp[i-1]+source[i],source[i]) algorithmic
    124     *         complexity is O(n).(Not all are negative)
    125     */

    126    public static int getThrOptimizedMaxSegment() {
    127        start = -1;end = -1;sum = 0;
    128        int temp = 0;
    129        int flag=-1, count=0;
    130        for (int i = 0; i < source.length; i++{
    131            if (temp > 0){
    132                temp += source[i];
    133                flag = i;
    134                count++;
    135            }

    136            else
    137                temp = source[i];
    138            if (temp > sum){
    139                sum = temp ;
    140                start = flag-count;
    141                end = i;
    142            }

    143        }

    144        return sum;
    145    }

    146
    147    public static int getStart() {
    148        return start;
    149    }

    150
    151    public static int getEnd() {
    152        return end;
    153    }

    154}
    posted on 2007-07-31 12:45 wqwqwqwqwq 閱讀(846) 評論(0)  編輯  收藏 所屬分類: Data Structure && Algorithm
    <2007年7月>
    24252627282930
    1234567
    891011121314
    15161718192021
    22232425262728
    2930311234




    常用鏈接

    留言簿(10)

    隨筆分類(95)

    隨筆檔案(97)

    文章檔案(10)

    相冊

    J2ME技術網站

    java技術相關

    mess

    搜索

    •  

    最新評論

    閱讀排行榜

    校園夢網網絡電話,中國最優秀的網絡電話
    主站蜘蛛池模板: 亚洲av成人综合网| 7x7x7x免费在线观看| 亚洲天堂免费在线视频| 国产成人免费ā片在线观看老同学| 美女网站在线观看视频免费的| 久章草在线精品视频免费观看 | 老司机福利在线免费观看| 免费夜色污私人影院网站| 最近国语视频在线观看免费播放| 久久国产精品免费网站| 亚洲人成电影网站免费| 亚洲国产精品无码久久青草| 搜日本一区二区三区免费高清视频| 一区二区三区视频免费| 免费A级毛片在线播放| 国产大片91精品免费观看男同| 中文亚洲AV片不卡在线观看| 久久精品国产亚洲AV忘忧草18| eeuss草民免费| 中文字幕av无码无卡免费| 亚洲国产V高清在线观看| 亚洲日韩在线视频| 一级做a爰全过程免费视频毛片| 国产精品视频免费| 亚洲综合图色40p| 亚洲精品欧美综合四区| 99热精品在线免费观看| 亚洲国产成人爱av在线播放| 亚洲国产成人综合| 最新亚洲成av人免费看| 国产免费爽爽视频免费可以看| 香蕉蕉亚亚洲aav综合| 亚洲国产成人久久一区二区三区| 无码av免费一区二区三区试看| 免费a级毛片视频| 亚洲中文字幕无码中文字| 亚洲国产午夜精品理论片在线播放| 99精品视频在线观看免费| 亚洲AV无码乱码在线观看牲色| 亚洲人成77777在线观看网| 一级毛片免费不卡在线|