Skip to content

Instantly share code, notes, and snippets.

@restart916
Last active November 29, 2017 03:21
Show Gist options
  • Save restart916/10d06e4dfa97312d9735eefdd879277c to your computer and use it in GitHub Desktop.
Save restart916/10d06e4dfa97312d9735eefdd879277c to your computer and use it in GitHub Desktop.
problem_02
<?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));
@kkokey
Copy link

kkokey commented Nov 29, 2017

이건 답이 중앙에 몰려있는경우 O(n)이 되겠네요.ㅎ
그래도 고생하셨습니다~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment