Help me see this simple algorithm.

Source: Internet
Author: User
poorly written, please optimize
An array of user IDs

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

Each key value of this array represents a UID
Amount array

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

The key name of this array corresponds to the key value of the $uid array.
Loop $uids This array, take out the corresponding amount $amount,
If this amount is greater than or equal to 12000 ($boundary), the total amount ($totals) plus this $amount
If this amount is less than 12000, then cycle down again, loop to these several $amount add greater than or equal to 12000, and then in the total amount ($totals) plus these several $amount and
Take up to three layers.
Finally, the value of $totals is obtained.
My current code:


  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];            Then get one layer down $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; //jump out of the main loop}}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;}

Draw $totals=47500

Reply content:

Poorly written, please optimize
An array of user IDs

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

Each key value of this array represents a UID
Amount array

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

The key name of this array corresponds to the key value of the $uid array.
Loop $uids This array, take out the corresponding amount $amount,
If this amount is greater than or equal to 12000 ($boundary), the total amount ($totals) plus this $amount
If this amount is less than 12000, then cycle down again, loop to these several $amount add greater than or equal to 12000, and then in the total amount ($totals) plus these several $amount and
Take up to three layers.
Finally, the value of $totals is obtained.
My current code:


  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];            Then get one layer down $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; //jump out of the main loop}}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;}

Draw $totals=47500

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.