This commit is contained in:
kkunkka
2025-04-07 15:56:32 +08:00
parent 471bf5c0d5
commit 4c69991f14
4 changed files with 80 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
package com.dota.arr._2874;
class Solution {
public long maximumTripletValue(int[] nums) {
long res = 0;
long max = Math.max(nums[0], nums[1]);
long num = nums[0] - nums[1];
for (int i = 2; i < nums.length; i++) {
res = Math.max(res, num * nums[2]);
num = Math.max(num, max - nums[i]);
max = Math.max(max, nums[i]);
}
return res;
}
}

View File

@@ -0,0 +1,23 @@
package com.dota.dp._416;
class Solution {
public boolean canPartition(int[] nums) {
int sum = 0;
for (int num : nums) {
sum += num;
}
if (sum % 2 != 0) return false;
int s = sum / 2;
int n = nums.length;
var dp = new boolean[n + 1][s + 1];
dp[0][0] = true;
for (int i = 0; i < n; i++) {
int x = nums[i];
for (int j = 0; j <= s; j++) {
dp[i + 1][j] = j>=x && dp[i][j-x] || dp[i][j];
}
}
return dp[n][s];
}
}

View File

@@ -0,0 +1,24 @@
package com.dota.slidingWindow._1493;
class Solution {
public int longestSubarray(int[] nums) {
int max = 0;
int l = 0, r = 0;
int n = nums.length;
int cnt = 0;
while (r < n) {
if (nums[r] == 0) {
cnt++;
}
while (cnt == 2) {
if (nums[l] == 0) {
cnt--;
}
l++;
}
max = Math.max(max, r - l);
r++;
}
return max;
}
}

View File

@@ -0,0 +1,18 @@
package com.dota.slidingWindow._3090;
class Solution {
public int maximumLengthSubstring(String s) {
var dp = new int[26];
int l = 0;
int res = 0;
for (int i = 0; i < s.length(); i++) {
dp[s.charAt(i) - 'a']++;
while (dp[s.charAt(i) - 'a'] > 2) {
dp[s.charAt(l) - 'a']--;
l++;
}
res = Math.max(res, i - l+1);
}
return res;
}
}