kk
This commit is contained in:
26
src/main/java/com/dota/arr/_2012/Solution.java
Normal file
26
src/main/java/com/dota/arr/_2012/Solution.java
Normal 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;
|
||||
}
|
||||
}
|
27
src/main/java/com/dota/slidingWindow/Solution.java
Normal file
27
src/main/java/com/dota/slidingWindow/Solution.java
Normal 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;
|
||||
}
|
||||
}
|
37
src/main/java/com/dota/str/_3305/Solution.java
Normal file
37
src/main/java/com/dota/str/_3305/Solution.java
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user