Last active
November 29, 2017 03:21
-
-
Save restart916/10d06e4dfa97312d9735eefdd879277c to your computer and use it in GitHub Desktop.
problem_02
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// your code goes here | |
function find($arr, $findValue) | |
{ | |
$head = 0; | |
$tail = count($arr) - 1; | |
while(1) { | |
$headValue = $arr[$head]; | |
$tailValue = $arr[$tail]; | |
$resultValue = $headValue + $tailValue; | |
if ( $findValue > $resultValue) { | |
$head++; | |
} else if ( $findValue < $resultValue) { | |
$tail--; | |
} else { | |
return [$head, $tail]; | |
} | |
if ($head == $tail) { | |
return [-1, -1]; | |
} | |
} | |
} | |
print_r(find([0, 1, 3, 8], 9)); | |
print_r(find([1,2,7,11,15], 3)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
이건 답이 중앙에 몰려있는경우 O(n)이 되겠네요.ㅎ
그래도 고생하셨습니다~