Skip to content

Commit ad5b061

Browse files
add 740
1 parent 0791941 commit ad5b061

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Your ideas/fixes/algorithms are more than welcome!
4747
|747|[Largest Number Greater Than Twice of Others](https://leetcode.com/problems/largest-number-greater-than-twice-of-others/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_747.java) | O(n) | O(1) | |Easy|
4848
|746|[Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_746.java) | O(n) | O(1) | |Easy|
4949
|744|[Find Smallest Letter Greater Than Target](https://leetcode.com/problems/find-smallest-letter-greater-than-target/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_744.java) | O(logn) | O(1) || Easy|
50+
|740|[Delete and Earn](https://leetcode.com/problems/delete-and-earn/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_740.java) | O(n) | O(n) | |Medium|
5051
|739|[Daily Temperatures](https://leetcode.com/problems/daily-temperatures/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_739.java) | O(n^2) | O(1) | |Medium|
5152
|738|[Monotone Increasing Digits](https://leetcode.com/problems/monotone-increasing-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_738.java) | O(n) | O(1) | |Medium|
5253
|737|[Sentence Similarity II](https://leetcode.com/problems/sentence-similarity-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_737.java) | O(nlogk + k) n is the length of max(words1, words2), k is the length of pairs| O(k) | |Medium| Union Find
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 740. Delete and Earn
5+
*
6+
* Given an array nums of integers, you can perform operations on the array.
7+
* In each operation, you pick any nums[i] and delete it to earn nums[i] points. After, you must delete every element equal to nums[i] - 1 or nums[i] + 1.
8+
* You start with 0 points. Return the maximum number of points you can earn by applying such operations.
9+
10+
Example 1:
11+
Input: nums = [3, 4, 2]
12+
Output: 6
13+
Explanation:
14+
Delete 4 to earn 4 points, consequently 3 is also deleted.
15+
Then, delete 2 to earn 2 points. 6 total points are earned.
16+
17+
Example 2:
18+
Input: nums = [2, 2, 3, 3, 3, 4]
19+
Output: 9
20+
Explanation:
21+
Delete 3 to earn 3 points, deleting both 2's and the 4.
22+
Then, delete 3 again to earn 3 points, and 3 again to earn 3 points.
23+
9 total points are earned.
24+
25+
Note:
26+
The length of nums is at most 20000.
27+
Each element nums[i] is an integer in the range [1, 10000].*/
28+
29+
public class _740 {
30+
public static class Solution1 {
31+
/**
32+
* Since the number is within range [1, 10000], we can build another array:
33+
* each number in the array denotes the total sum of this number that appears in this array
34+
* and
35+
* use the numbers themselves in the indices of another array
36+
*
37+
* credit: https://leetcode.com/problems/delete-and-earn/discuss/109895/JavaC++-Clean-Code-with-Explanation*/
38+
public int deleteAndEarn(int[] nums) {
39+
int n = 10001;
40+
int[] values = new int[n];
41+
for (int num : nums) {
42+
values[num] += num;
43+
}
44+
45+
int take = 0;
46+
int skip = 0;
47+
for (int i = 0; i < n; i++) {
48+
int takeI = skip + values[i];
49+
int skipI = Math.max(skip, take);
50+
take = takeI;
51+
skip = skipI;
52+
}
53+
return Math.max(take, skip);
54+
}
55+
}
56+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._740;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _740Test {
10+
private static _740.Solution1 solution1;
11+
private static int[] nums;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _740.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
nums = new int[] {3, 4, 2};
21+
assertEquals(6, solution1.deleteAndEarn(nums));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
nums = new int[] {2, 2, 3, 3, 3, 4};
27+
assertEquals(9, solution1.deleteAndEarn(nums));
28+
}
29+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy