Skip to content

Instantly share code, notes, and snippets.

@bunlongheng
Last active March 1, 2019 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bunlongheng/13784cb964ead48a2f2b to your computer and use it in GitHub Desktop.
Save bunlongheng/13784cb964ead48a2f2b to your computer and use it in GitHub Desktop.
10/1/2015
function containsKey($key_para, $array)
{
foreach ($array as $key => $value) {
if ($key_para === $key) {
return true;
}
}
return false;
}
function findRank($perm)
{
$a = array();
$rank = 1;
$suffix = 1;
$length = strlen($perm);
$characters = str_split($perm);
for ($i = $length - 1; $i > -1; $i--) {
$char = $characters[$i];
$count = 1;
if ($this->containsKey($char, $a)) {
$count = $a[$char];
$count++;
$a[$char] = $count;
} else {
$a[$char] = $count;
}
foreach ($a as $key => $value) {
if (ord($key) < ord($characters[$i])) {
$rank = $rank + (($suffix * $value) / $count);
}
}
$suffix = $suffix * ($length - $i);
$suffix = ($suffix / $count);
}
return $rank;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment