leetcode-740. 删除并获得点数
题目
解法
class Solution {
/**
* @param Integer[] $nums
* @return Integer
*/
function deleteAndEarn($nums) {
$sum = [];
$max = 0;
foreach ($nums as $num) {
$sum[$num] += $num;
$max = max($num, $max);
}
return $this->rob($sum, $max);
}
function rob($nums, $size) {
$first = $nums[0];
$second = max($nums[1], $nums[0]);
for ($i = 2; $i <= $size; $i++) {
$tmp = $second;
$second = max($first + $nums[$i], $second);
$first = $tmp;
}
return $second;
}
}
参考
本文来自博客园,作者:吴丹阳-V,转载请注明原文链接:https://www.cnblogs.com/wudanyang/p/15778810.html