This commit is contained in:
kkunkka
2025-04-21 15:20:05 +08:00
parent da0273ca2e
commit 717657849a
3 changed files with 61 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
package com.dota.greedy._2145;
class Solution {
public int numberOfArrays(int[] differences, int lower, int upper) {
long max = differences[0];
long min = differences[0];
long t = differences[0];
for (int i = 1; i < differences.length; i++) {
t += differences[i];
min = Math.min(min, t);
max = Math.max(max, t);
}
return (int) (Math.max(-max + min + upper - lower + 1, 0));
}
}

View File

@@ -0,0 +1,20 @@
package com.dota.slidingWindow._2401;
class Solution {
public int longestNiceSubarray(int[] nums) {
int res = 1;
int l = 0;
int sum = 0;
int cnt = 0;
for (int i = 0; i < nums.length; i++) {
while((sum & nums[i])!=0) {
sum -= nums[l++];
cnt--;
}
cnt++;
sum += nums[i];
res = Math.max(res, cnt);
}
return res;
}
}

View File

@@ -0,0 +1,25 @@
package com.dota.slidingWindow._930;
class Solution {
public int numSubarraysWithSum(int[] nums, int goal) {
int res = 0;
int n = nums.length;
int l = 0;
int ll = 0;
int sum1=0,sum2=0;
for (int i = 0; i < n; i++) {
sum1 += nums[i];
while(l<=i&&sum1>goal) {
sum1 -= nums[l++];
}
sum2 += nums[i];
while(ll<=i && sum2>=goal) {
sum2 -= nums[ll++];
}
res += (ll - l);
}
return res;
}
}