<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无码专区| 久久国产色AV免费看| 91丁香亚洲综合社区| 亚洲av午夜成人片精品电影 | 国产免费内射又粗又爽密桃视频| 亚洲AV日韩AV天堂久久| 美女视频黄免费亚洲| 日韩大片在线永久免费观看网站 | 中文亚洲成a人片在线观看| 在线免费观看你懂的| 理论片在线观看免费| 亚洲美女人黄网成人女| 在线免费观看国产视频| 国产一区二区免费视频| 亚洲精品无码mⅴ在线观看| 中文字幕在线亚洲精品| 歪歪漫画在线观看官网免费阅读| 成人免费网站视频www| 亚洲中文字幕在线无码一区二区| 国产性爱在线观看亚洲黄色一级片 | 国产精品亚洲精品日韩已方| 久久免费的精品国产V∧| 怡红院亚洲红怡院在线观看| 亚洲精品在线免费看| 五月天婷亚洲天综合网精品偷| 国产在线精品免费aaa片| 亚洲精品美女网站| 亚洲AV日韩精品久久久久久久| 国产精品久免费的黄网站| 无码AV片在线观看免费| 青娱乐在线视频免费观看| 亚洲av成人一区二区三区| 亚洲AV无码第一区二区三区| 全亚洲最新黄色特级网站| 一区二区无码免费视频网站| 久久99精品视免费看| 成人国产网站v片免费观看| 亚洲色大成网站www| 亚洲国产电影在线观看| 亚洲欧洲一区二区|