幫幫看看這個簡單的演算法

來源:互聯網
上載者:User
寫的不好,求大家最佳化
一個使用者ID的數組

$uids = [1,2,3,5,6,8,9,11,13,25,65];

這個數組的每一個索引值代表一個UID
金額數組

$amounts = [    1=>12000,    2=>500,    3=>11000,    5=>1000,    6=>11000,    8=>12000,    9=>12000,    11=>11000,    13=>12000,    25=>22000,    65=>123123];

此數組的鍵名和$uid數組的索引值對應。
迴圈$uids這個數組,取出對應的金額$amount,
如果這個金額大於等於12000($boundary),就在總金額($totals)加上這個$amount
如果這個金額小於12000,就再向下迴圈,迴圈到這幾個$amount相加大於等於12000,然後在總金額($totals)加上這幾個$amount的和
最多取三層。
最後得出$totals的值。
我現在的代碼:

12000,    2=>500,    3=>11000,    5=>1000,    6=>11000,    8=>12000,    9=>12000,    11=>11000,    13=>12000,    25=>22000,    65=>123123];$totals = 0;foreach($uids as $k => $uid){    $amont = $amounts[$uid];    if($amont >= $boundary){        $totals += $amont;    }else{        $next = get_next($uids ,$k+1 ,$amont);        if($next && is_array($next)){            $curKey = $next[0]; //amouts index 3            $totals+=$next[2];            //再向下擷取一層            $nextKey = $curKey+1;            if(!isset($uids[$nextKey])){                break;            }            $nextUid = $uids[$nextKey];            $nextAmount = $amounts[$nextUid];            if($nextAmount >= $boundary){                $totals+=$nextAmount;            }else{                $last = get_next($uids ,$nextKey+1 ,$nextAmount);                if($last && is_array($last)){                    $totals+=$last[2];                }            }        }        break; //跳出主迴圈    }}echo $totals;exit;function get_next($uids ,$start ,$prevAmount){    global $amounts ,$boundary;    $leaves = array_slice($uids ,$start ,count($uids),true);    if($leaves){        foreach($leaves as $k=>$uid){            $amount = $prevAmount+$amounts[$uid];            if($amount >= $boundary){                return [$k ,$uid ,$amount];                break;            }else{                return get_next($uids ,$k+1 ,$amount);            }        }    }    return 0;}

得出$totals=47500

回複內容:

寫的不好,求大家最佳化
一個使用者ID的數組

$uids = [1,2,3,5,6,8,9,11,13,25,65];

這個數組的每一個索引值代表一個UID
金額數組

$amounts = [    1=>12000,    2=>500,    3=>11000,    5=>1000,    6=>11000,    8=>12000,    9=>12000,    11=>11000,    13=>12000,    25=>22000,    65=>123123];

此數組的鍵名和$uid數組的索引值對應。
迴圈$uids這個數組,取出對應的金額$amount,
如果這個金額大於等於12000($boundary),就在總金額($totals)加上這個$amount
如果這個金額小於12000,就再向下迴圈,迴圈到這幾個$amount相加大於等於12000,然後在總金額($totals)加上這幾個$amount的和
最多取三層。
最後得出$totals的值。
我現在的代碼:

12000,    2=>500,    3=>11000,    5=>1000,    6=>11000,    8=>12000,    9=>12000,    11=>11000,    13=>12000,    25=>22000,    65=>123123];$totals = 0;foreach($uids as $k => $uid){    $amont = $amounts[$uid];    if($amont >= $boundary){        $totals += $amont;    }else{        $next = get_next($uids ,$k+1 ,$amont);        if($next && is_array($next)){            $curKey = $next[0]; //amouts index 3            $totals+=$next[2];            //再向下擷取一層            $nextKey = $curKey+1;            if(!isset($uids[$nextKey])){                break;            }            $nextUid = $uids[$nextKey];            $nextAmount = $amounts[$nextUid];            if($nextAmount >= $boundary){                $totals+=$nextAmount;            }else{                $last = get_next($uids ,$nextKey+1 ,$nextAmount);                if($last && is_array($last)){                    $totals+=$last[2];                }            }        }        break; //跳出主迴圈    }}echo $totals;exit;function get_next($uids ,$start ,$prevAmount){    global $amounts ,$boundary;    $leaves = array_slice($uids ,$start ,count($uids),true);    if($leaves){        foreach($leaves as $k=>$uid){            $amount = $prevAmount+$amounts[$uid];            if($amount >= $boundary){                return [$k ,$uid ,$amount];                break;            }else{                return get_next($uids ,$k+1 ,$amount);            }        }    }    return 0;}

得出$totals=47500

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.