-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwk353.java
84 lines (73 loc) · 2.29 KB
/
wk353.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package weekly;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class wk353 {
//贪心
public int theMaximumAchievableX(int num, int t) {
return num + t * 2;
}
//dp
public int maximumJumps(int[] nums, int target) {
int[] dp = new int[nums.length];
Arrays.fill(dp, -1);
dp[0] = 0;
for (int i = 1; i < nums.length; i++) {
for (int j = 0; j < i; j++) {
if (nums[i] - nums[j] >= -target && nums[i] - nums[j] <= target && dp[j] != -1) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
}
return dp[nums.length - 1];
}
//dp
public int maxNonDecreasingLength(int[] nums1, int[] nums2) {
int ans = 1;
int[][] dp = new int[nums1.length][2];
dp[0][0] = 1;
dp[0][1] = 1;
for (int i = 1; i < nums1.length; i++) {
dp[i][0] = 1;
dp[i][1] = 1;
if (nums1[i] >= nums1[i - 1]) {
dp[i][0] = Math.max(dp[i - 1][0] + 1, dp[i][0]);
}
if (nums1[i] >= nums2[i - 1]) {
dp[i][0] = Math.max(dp[i - 1][1] + 1, dp[i][0]);
}
if (nums2[i] >= nums1[i - 1]) {
dp[i][1] = Math.max(dp[i - 1][0] + 1, dp[i][1]);
}
if (nums2[i] >= nums2[i - 1]) {
dp[i][1] = Math.max(dp[i - 1][1] + 1, dp[i][1]);
}
ans = Math.max(ans, dp[i][0]);
ans = Math.max(ans, dp[i][1]);
}
return ans;
}
//滑动窗口 本质是差分数组
static public boolean checkArray(int[] nums, int k) {
if(k==1) return true;
int sum = nums[0];
for (int i = 1; i < nums.length; i++) {
int left = i - k ;
//减去左边的贡献
if (left >= 0) {
sum -= nums[left];
}
//需要减去更多,此处已经不满足
if (sum > nums[i]) {
return false;
}
//i处需要添加的贡献
nums[i] = (nums[i] - sum);
sum+=nums[i];
}
return nums[nums.length-1]==0;
}
public static void main(String[] args) {
checkArray(new int[]{2,2,3,2,1},3);
}
}