[LeetCode] 740. Delete and Earn

2024-09-06 04:48
文章标签 leetcode delete earn 740

本文主要是介绍[LeetCode] 740. Delete and Earn,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题:https://leetcode.com/problems/delete-and-earn/

题目大意

对于数组nums ,选取一个数 num,那么nums数组中 num - 1 与 num + 1 都会被删除,重复多次直到 nums 数组为空。求选取 num 的最大和。

解题思路

方法一 treeMap

将nums 中所有元素进行reduce操作,得到 TreeMap,其中 key = num ,val = num*k (k 为 nums 中num 出现的次数)

然后 从 小到大遍历 treeMap,这里用到的是

iterator it = treeMap.entrySet().iterator();
Map.Entry<Integer,Integer> entry = (Map.Entry<Integern,Integer>)it.next();

这里用到了动态规划,
int dp[i] : 选了 treeMap中的第i个数时,得到的最大收益是多少。
这里分两种情况,
若 上个数 == 当前num -1,那么 两者数值上连续 dp[i] = Math.max(dp[i-3], dp[i-2]) + entry.getValue();前一个数一定不能取得。

当 若上个数 != 当前num -1,那么dp[i] = Math.max(dp[i-2], dp[i-1]) + entry.getValue();
可以取当前数。

由于 只用到了 i -3,i-2,i-1,i ,可以使用 4个变量代替,动态规划 数组。

但这个方法 太麻烦了。

class Solution {public int deleteAndEarn(int[] nums) {TreeMap<Integer,Integer>  treeMap = new TreeMap<>();for(int num : nums){treeMap.put(num,treeMap.getOrDefault(num,0)+num);}int[] sum = new int[4];int lastKey = 0;Iterator it  = treeMap.entrySet().iterator();if(it.hasNext()){Map.Entry<Integer,Integer> entry = (Map.Entry<Integer, Integer>) it.next();sum[3] = entry.getValue();sum[2] = sum[3];lastKey = entry.getKey();}while (it.hasNext()){Map.Entry<Integer,Integer> entry = (Map.Entry<Integer, Integer>) it.next();if(lastKey == entry.getKey() -1){sum[3] = Math.max(sum[0], sum[1]) + entry.getValue();sum[0] = sum[1];sum[1] = sum[2];sum[2] = sum[3];}else {sum[3] = Math.max(sum[1],sum[2]) + entry.getValue();sum[0] = Math.max(sum[1],sum[2]);sum[1] = sum[0];sum[2] = sum[3];}lastKey = entry.getKey();}return Math.max(sum[1],sum[2]);}
}

方法二 转化为 LeetCode 198. House Robber

可以将该问题转化为 House Robber 为题。
新 numsArr[num]中,index 为元素num数值,val 为 nums中 num的和。

然后 , 动态规划

int dp[num]:遍历到num数时,能得到的最大 收益。(num不一定需要选取)

状态转移方程:
dp[num] = Math.max(dp[num-2] + numsArr[num], dp[num-1])。 当前最大收益为 dp[num-2] + 选num的收益 与 不选当前num的收益从而获得 dp[num-1] 的收益 中的 较大值。
由于只用到 i-2,i-1,i 可以使用 a,b,c 代替。

class Solution {public int deleteAndEarn(int[] nums) {int maxNum = 0 ;for(int num : nums)maxNum = Math.max(maxNum,num);int[] numsArr = new int[maxNum+1];for(int num:nums)numsArr[num] += num;int a = 0,b = 0,c = 0;for(int i = 0; i <= maxNum;i++){c = Math.max(a + numsArr[i],b);a = b;b = c;}return c;}
}

这篇关于[LeetCode] 740. Delete and Earn的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1141046

相关文章

MySQL中的DELETE删除数据及注意事项

《MySQL中的DELETE删除数据及注意事项》MySQL的DELETE语句是数据库操作中不可或缺的一部分,通过合理使用索引、批量删除、避免全表删除、使用TRUNCATE、使用ORDERBY和LIMI... 目录1. 基本语法单表删除2. 高级用法使用子查询删除删除多表3. 性能优化策略使用索引批量删除避免

Oracle 数据库数据操作如何精通 INSERT, UPDATE, DELETE

《Oracle数据库数据操作如何精通INSERT,UPDATE,DELETE》在Oracle数据库中,对表内数据进行增加、修改和删除操作是通过数据操作语言来完成的,下面给大家介绍Oracle数... 目录思维导图一、插入数据 (INSERT)1.1 插入单行数据,指定所有列的值语法:1.2 插入单行数据,指

哈希leetcode-1

目录 1前言 2.例题  2.1两数之和 2.2判断是否互为字符重排 2.3存在重复元素1 2.4存在重复元素2 2.5字母异位词分组 1前言 哈希表主要是适合于快速查找某个元素(O(1)) 当我们要频繁的查找某个元素,第一哈希表O(1),第二,二分O(log n) 一般可以分为语言自带的容器哈希和用数组模拟的简易哈希。 最简单的比如数组模拟字符存储,只要开26个c

leetcode-24Swap Nodes in Pairs

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode swapPairs(L

leetcode-23Merge k Sorted Lists

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode mergeKLists

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟) 题目描述 给定一个链表,链表中的每个节点代表一个整数。链表中的整数由 0 分隔开,表示不同的区间。链表的开始和结束节点的值都为 0。任务是将每两个相邻的 0 之间的所有节点合并成一个节点,新节点的值为原区间内所有节点值的和。合并后,需要移除所有的 0,并返回修改后的链表头节点。 思路分析 初始化:创建一个虚拟头节点

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

【JavaScript】LeetCode:16-20

文章目录 16 无重复字符的最长字串17 找到字符串中所有字母异位词18 和为K的子数组19 滑动窗口最大值20 最小覆盖字串 16 无重复字符的最长字串 滑动窗口 + 哈希表这里用哈希集合Set()实现。左指针i,右指针j,从头遍历数组,若j指针指向的元素不在set中,则加入该元素,否则更新结果res,删除集合中i指针指向的元素,进入下一轮循环。 /*** @param

LeetCode:64. 最大正方形 动态规划 时间复杂度O(nm)

64. 最大正方形 题目链接 题目描述 给定一个由 0 和 1 组成的二维矩阵,找出只包含 1 的最大正方形,并返回其面积。 示例1: 输入: 1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0输出: 4 示例2: 输入: 0 1 1 0 01 1 1 1 11 1 1 1 11 1 1 1 1输出: 9 解题思路 这道题的思路是使用动态规划