This commit is contained in:
kkunkka
2025-03-12 14:05:21 +08:00
parent 83b8c0e572
commit 51e6fff72c
3 changed files with 90 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
package com.dota.arr._2012;
class Solution {
public int sumOfBeauties(int[] nums) {
var res = 0;
var pre = new int[nums.length];
pre[0] = nums[0];
var post = new int[nums.length];
post[nums.length - 1] = nums[nums.length - 1];
for (int i = 1; i < nums.length - 1; i++) {
pre[i] = Math.max(pre[i - 1], nums[i]);
post[nums.length - i - 1] = Math.min(post[nums.length - i], nums[nums.length - i - 1]);
}
for (int i = 1; i < nums.length - 1; i++) {
if (nums[i]>pre[i-1] && nums[i]< post[i+1]) {
res += 2;
} else if (nums[i]<nums[i+1]&&nums[i]>nums[i-1]) {
res += 1;
}
}
return res;
}
}

View File

@@ -0,0 +1,27 @@
package com.dota.slidingWindow;
class Solution {
public long countSubarrays(int[] nums, int k) {
int max = 0;
long res = 0;
for (int num : nums) {
max = Math.max(max, num);
}
int left = 0;
int cnt = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == max) {
cnt++;
}
while (cnt == k) {
if (nums[left++] == max) {
cnt--;
}
}
res += left;
}
return res;
}
}

View File

@@ -0,0 +1,37 @@
package com.dota.str._3305;
import java.util.HashMap;
class Solution {
public int countOfSubstrings(String word, int k) {
return f(word, k) - f(word, k-1);
}
public int f(String word, int k) {
int res = 0;
int cnt = 0;
int left = 0;
var map = new HashMap<Character, Integer>();
for (int i = 0; i < word.length(); i++) {
if ("aeiou".indexOf(word.charAt(i)) >= 0) {
map.merge(word.charAt(i), 1, Integer::sum);
} else {
cnt++;
}
while (map.size()==5 && cnt >= k) {
char out = word.charAt(left);
if ("aeiou".indexOf(out) >= 0) {
if (map.merge(out, -1, Integer::sum) == 0) {
map.remove(out);
}
} else {
cnt--;
}
left++;
}
res += left;
}
return res;
}
}