An indexed array of PHP, the values in the array are integers from 1 to 100 (not duplicated), and the values may be intermittent, that is, there may be 7,9 but No 8. And the order is scrambled, that is, not from 1 to 100.
Now suppose $a=50, what's the quickest way to take out = = $a or the nearest two value to $ A?
The values in the array are not necessarily equal to $ A.
Reply content:
An indexed array of PHP, the values in the array are integers from 1 to 100 (not duplicated), and the values may be intermittent, that is, there may be 7,9 but No 8. And the order is scrambled, that is, not from 1 to 100.
Now suppose $a=50, what's the quickest way to take out = = $a or the nearest two value to $ A?
The values in the array are not necessarily equal to $ A.
array_search
-Searches for the given value in the array and returns the corresponding key name if successful
To get the key name, $arr[$key-1]
$arr[$key+1]
you can
Upstairs is already very simple, but need is if not found this number, find the closest, and the original array order is scrambled, so the top and bottom two is not necessarily the closest, of course, binary search is also a way of thinking, I provide myself with the idea of the algorithm, My idea is to sort the buckets first (the quickest way I know of sorting in a positive integer now)
//这个是要求的数组$arr = [1,2,3...];//填充一个1-100范围的数组$search_arr = array_fill(0 , 99 , 0);//遍历数组foreach($search_arr as $k=>$v){ if(in_array($k , $arr)){ ++ $v; }}//此时search_arr数组里面值是1的就是要找的,同时已经排序好了foreach($search_arr as $k=>$v){ if($v >= 1){ $new_arr[] = $k; }}//此时的new_arr就是一个键从0自增,同时值是排序的数组,然后再结合楼上的写法,便可求出。
I don't know how efficient this is.
$arr = range(1, 100); // 要求的数组$target = 10; // 目标值shuffle($arr); // 打乱顺序$val_key = array_search($target, $arr);// 测试 $target 不存在的情况去掉以下注释// array_splice($arr, $val_key, 1);// $val_key = '';if ($val_key) { echo "这个值是:{$arr[$val_key]}";} else { sort($arr); foreach ($arr as $key => $value) { if (($value < $target) && ($arr[$key+1] > $target)) {
"; echo "右边:{$arr[$key+1]}"; exit; } }}
Static query with no modifications
Sort it (ascending), complexity Nlogn (one order)
Then two-point fast positioning, complexity Logn (one query)
// 在有序数组$arr中得到大于等于$val的第一个下标// 如果想要获得离$val最近的值,通过返回值判断// 如果大于最大的值,返回数组的长度function binary_search($arr, $val){ $n = count($arr) - 1; $ans = $n + 1; $l = 0; $r = $n; while($l <= $r){ $mid = ($l + $r) >> 1; if($arr[$mid] >= $val){ $ans = $mid; $r = $mid -1; } else $l = $mid + 1; } return $ans;}$arr = [1,5,9,3,8,7,10,12];sort($arr);foreach($arr as $key => $val){ printf("%d ", $val);}printf("\n");$search_num = 6;printf("%d\n", binary_search($arr, $search_num));
Dynamic queries with add operations only, without changing the order
1-100 There are 100 numbers, and its value is also 1-100, if asked 69 location subscript, can be 69 as the center, two points to find the next point of the subscript, if a position exists number, then marked 1, otherwise marked 0, then 69 as the center, to the left two to find the longest interval and 0, To the right two to find the longest interval and 0, fast interval and can use a tree-like array, update query complexity is LOGN, add the number of complexity is LOGN.
Requirements and Purpose:
The tree array holds the interval flags and (whether or not an interval value appears), Update and query complexity Logn
Finds the nearest value from a value, then returns its subscript, Logn, complexity
The mapping of the subscript is saved with the space change time.
You can add a number at the end of the array without requiring it to be added sequentially
The following code addresses the following issues
Suppose there is an array [5,9,3,8,7,10,12]
Ask for nearest coordinates from 12, return 6
Ask for nearest coordinates from 2, return 2
Add a non-repeating number 15
Add a non-repeating number 18
Add a non-repeating number 16
Add a non-repeating number 13
Ask for nearest coordinates from 13, return 10
Ask for nearest coordinates from 17, return 9
The tree array is initialized with a length of 106, the assignment is 0$arr_bit = Array (), for ($i = 0; $i <=, $i + +) {$arr _bit[$i] = 0;} Query between 1-$x and function query ($x) {global $arr _bit; $sum = 0; while ($x > 0) {$sum + = $arr _bit[$x]; $x-= $x &-$x; } return $sum;} Update the $X flag function Add ($x, $val) {global $arr _bit; while ($x <=) {$arr _bit[$x] + = $val; $x + = $x &-$x; }} $arr = [5,9,3,8,7,10,12]; $arr _tmp = Array (); foreach ($arr as $key = = $val) {$arr _tmp[$val] = $key; printf ("%d", $val); Add ($val, 1);} printf ("\ n");//Find the subscript nearest to a value//find the left and then find the right, if not, return -1function Find_val_pos ($val) {if ($val < 1 | | $val > 100) { return-1; } Global $arr _tmp; $n = count ($arr); $l = 1; $r = $val; $ans _l =-1; Get $val to the left nearest while ($l <= $r) {$mid = ($l + $r) >> 1; Get $val to $mid and $mid _val = query ($val)-Query ($mid-1); If the flag interval and greater than 1, record the answer, l move to the right to continue checking if ($mid _val >= 1) {$ans _l = $miD $l = $mid + 1; } else $r = $mid-1; } $l = $val; $r = 101; $ans _r =-1; Get $val to the right of the nearest while ($l <= $r) {$mid = ($l + $r) >> 1; Get $mid to $val and $mid _val = query ($mid)-Query ($val-1); if ($mid _val >= 1) {$ans _r = $mid; $r = $mid-1; } else $l = $mid + 1; } if ($ans _l = =-1) return $arr _tmp[$ans _r]; ElseIf ($ans _r = =-1) return $arr _tmp[$ans _l]; else {if ($val-$ans _l > $ans _r-$val) return $arr _tmp[$ans _r]; else return $arr _tmp[$ans _l]; }}function Add_num ($val) {if ($val < 1 | | $val >) return false; Global $arr _tmp; if (Isset ($arr _tmp[$val])) {return false; } else {global $arr; $arr _n = count ($arr); $arr _tmp[$val] = $arr _n; $arr [$arr _n] = $val; Add ($val, 1); return true; }}//Query 12 Nearest coordinates printf ("%d\n", Find_val_pos (12)); The result is 6//Query 2 nearest coordinatesprintf ("%d\n", Find_val_pos (2)); The result is 2add_num (15); 15 out of 7add_num (18); 18 Out of 8add_num (16); 16 Out of 9add_num (13); 13 out of 10//query 13 nearest coordinates printf ("%d\n", Find_val_pos (13)); The result is 10//query 17 nearest coordinates printf ("%d\n", Find_val_pos (17)); The result is 9//query 15 nearest coordinates printf ("%d\n", Find_val_pos (15)); The result is 7printf ("hh\n");//Query 100 nearest coordinates printf ("%d\n", Find_val_pos (100)); The result is 8 because the 8th position is 18, which is the maximum number
Dynamic Query with Add delete operation (larger number)
You need to maintain an additional subscript occupancy interval value, then set a balanced binary tree, query complexity logn, add delete complexity Logn.