《LeetCode 力扣》 - 1010. 总持续时间可被 60 整除的歌曲 (哈希+取余)
《LeetCode 力扣》 - 1010. 总持续时间可被 60 整除的歌曲 (哈希+取余)
- ref
- contact
- 每日一题
方法1 暴力
class Solution {
/**
* @param Integer[] $arr
* @return Integer
*/
function peakIndexInMountainArray($arr) {
$size = count($time);
$total= 0;
for ($i=0; $i < $size - 1; $i++) {
for ($j=$i+1; $j < $size; $j++) {
if(($time[$i] + $time[$j]) % 60 == 0) {
$total++;
}
}
}
return $total;
}
}
方法2 取余
class Solution {
/**
* @param Integer[] $time
* @return Integer
*/
function numPairsDivisibleBy60($time) {
$size = count($time);
$total = 0;
$x = 60;
$map = array_fill(1, $x, 0);
for ($i=0; $i < $size; $i++) {
$total += $map[$x - $time[$i] % $x];
if($time[$i] % $x == 0) {
$map[$x]++;
} else {
$map[$time[$i] % $x]++;
}
}
return $total;
}
}
本作品采用《CC 协议》,转载必须注明作者和本文链接