6451. 找出最大的可达成数字 - 力扣(LeetCode)
思路:数学
class Solution { public: int theMaximumAchievableX(int num, int t) { return num + 2 * t; } };
6899. 达到末尾下标所需的最大跳跃次数 - 力扣(LeetCode)
思路:dp
class Solution { public: int maximumJumps(vector<int>& nums, int target) { int n = nums.size(); int dp[n]; memset(dp, -1, sizeof dp); dp[0] = 0; for(int i = 1; i < n; i ++ ) { for(int j = 0; j < i; j ++ ) { if(j > 0 && dp[j] == -1) continue; if(abs(nums[i] - nums[j]) <= target) dp[i] = max(dp[i], dp[j] + 1); } } //for(int t : dp) cout << t << " "; if(dp[n - 1] == 0) return -1; else return dp[n - 1]; } };
6912. 构造最长非递减子数组 - 力扣(LeetCode)
思路:dp
class Solution { public: int maxNonDecreasingLength(vector<int>& nums1, vector<int>& nums2) { int n = nums1.size(); int num[n][2]; for(int i = 0; i < n; i ++ ) num[i][0] = nums1[i], num[i][1] = nums2[i]; int dp[n][2]; dp[0][0] = dp[0][1] = 1; int res = 1; for(int i = 1; i < n; i ++ ) for(int j = 0; j < 2; j ++ ) { dp[i][j] = 1; for(int k = 0; k < 2; k ++ ) if(num[i][j] >= num[i - 1][k]) dp[i][j] = max(dp[i][j], dp[i - 1][k] + 1); res = max(res, dp[i][j]); } return res; } };
6919. 使数组中的所有元素都等于零 - 力扣(LeetCode)
思路:差分数组
class Solution { public: bool checkArray(vector<int>& nums, int k) { int n = nums.size(), cnt = 0; vector<int> d(n + 1, 0); for(int i = 0; i < n; i ++ ) { cnt += d[i]; int x = nums[i]; x += cnt; if(x == 0) continue; if(x < 0 || i + k > n) return false; cnt -= x; d[i + k] += x; } return true; } };