MCPcopy Index your code
hub / github.com/neetcode-gh/leetcode / deleteAndEarn

Method deleteAndEarn

java/0740-delete-and-earn.java:2–24  ·  view source on GitHub ↗
(int[] nums)

Source from the content-addressed store, hash-verified

1class Solution {
2 public int deleteAndEarn(int[] nums) {
3 Map<Integer, Integer> counter = new HashMap<>();
4 for (int i = 0; i < nums.length; i++) {
5 counter.put(nums[i], counter.getOrDefault(nums[i], 0) + 1);
6 }
7 List<Integer> numsList = new ArrayList<>(counter.keySet());
8 Collections.sort(numsList);
9
10 int earnOne = 0;
11 int earnTwo = 0;
12 for (int i = 0; i < numsList.size(); i++) {
13 int curEarn = numsList.get(i) * counter.get(numsList.get(i));
14 if (i > 0 && numsList.get(i) == numsList.get(i - 1) + 1) {
15 int temp = earnTwo;
16 earnTwo = Math.max(earnOne + curEarn, earnTwo);
17 earnOne = temp;
18 } else {
19 earnOne = earnTwo;
20 earnTwo += curEarn;
21 }
22 }
23 return earnTwo;
24 }
25}

Callers

nothing calls this directly

Calls 3

putMethod · 0.45
sizeMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected