740. Delete and Earn Medium

@problem@discussion
#Array#Hash Table#Dynamic Programming



1/**
2 * [740] Delete and Earn
3 *
4 * You are given an integer array nums. You want to maximize the number of points you get by performing the following operation any number of times:
5 * 
6 * 	Pick any nums[i] and delete it to earn nums[i] points. Afterwards, you must delete every element equal to nums[i] - 1 and every element equal to nums[i] + 1.
7 * 
8 * Return the maximum number of points you can earn by applying the above operation some number of times.
9 *  
10 * Example 1:
11 * 
12 * Input: nums = [3,4,2]
13 * Output: 6
14 * Explanation: You can perform the following operations:
15 * - Delete 4 to earn 4 points. Consequently, 3 is also deleted. nums = [2].
16 * - Delete 2 to earn 2 points. nums = [].
17 * You earn a total of 6 points.
18 * 
19 * Example 2:
20 * 
21 * Input: nums = [2,2,3,3,3,4]
22 * Output: 9
23 * Explanation: You can perform the following operations:
24 * - Delete a 3 to earn 3 points. All 2's and 4's are also deleted. nums = [3,3].
25 * - Delete a 3 again to earn 3 points. nums = [3].
26 * - Delete a 3 once more to earn 3 points. nums = [].
27 * You earn a total of 9 points.
28 *  
29 * Constraints:
30 * 
31 * 	1 <= nums.length <= 2 * 10^4
32 * 	1 <= nums[i] <= 10^4
33 * 
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/delete-and-earn/
38// discuss: https://leetcode.com/problems/delete-and-earn/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43    pub fn delete_and_earn(nums: Vec<i32>) -> i32 {
44        0
45    }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_740() {
56    }
57}
58


Back
© 2025 bowen.ge All Rights Reserved.