forked from super30admin/DP-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSample.java
52 lines (46 loc) · 1.43 KB
/
Sample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Time Complexity :O(NlogN)
// Space Complexity :
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this :
// Your code here along with comments explaining your approach
//Delete and Earn
class Solution {
public int deleteAndEarn(int[] nums) {
int[] count = new int[10001];
for (int x: nums) count[x]++;
int avoid = 0, using = 0, prev = -1;
for (int k = 0; k <= 10000; ++k) if (count[k] > 0) {
int m = Math.max(avoid, using);
if (k - 1 != prev) {
using = k * count[k] + m;
avoid = m;
} else {
using = k * count[k] + avoid;
avoid = m;
}
prev = k;
}
return Math.max(avoid, using);
}
}
//Minimum Falling Path Sum
class Solution {
public int minFallingPathSum(int[][] A) {
int N = A.length;
for (int r = N-2; r >= 0; --r) {
for (int c = 0; c < N; ++c) {
// best = min(A[r+1][c-1], A[r+1][c], A[r+1][c+1])
int best = A[r+1][c];
if (c > 0)
best = Math.min(best, A[r+1][c-1]);
if (c+1 < N)
best = Math.min(best, A[r+1][c+1]);
A[r][c] += best;
}
}
int ans = Integer.MAX_VALUE;
for (int x: A[0])
ans = Math.min(ans, x);
return ans;
}
}