This commit is contained in:
kkunkka
2025-04-01 13:31:40 +08:00
parent f9749da0e4
commit bdbae9d9f0
3 changed files with 113 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
package com.dota.dp._2140;
class Solution {
public long mostPoints(int[][] questions) {
int n = questions.length;
var dp = new long[n + 1];
for (int i = n - 1; i >= 0; i--) {
int j = Math.min(n, i+questions[i][1]+1);
dp[i] = Math.max(dp[i+1], dp[j] + questions[i][0]);
}
return dp[0];
}
}

View File

@@ -0,0 +1,25 @@
package com.dota.slidingWindow._2156;
class Solution {
public String subStrHash(String s, int power, int modulo, int k, int hashValue) {
long sum = 0;
int n = s.length();
int pk = 1;
for (int i = n - k; i < n; i++) {
sum = (sum + pk * (s.charAt(i) & 31)) % modulo;
pk = pk * power % modulo;
}
if (sum == hashValue) {
return s.substring(n - k, n);
}
for (int i = n - k - 1; i >= 0; i--) {
sum = (sum * power + s.charAt(i) & 31 - (long) pk * (s.charAt(i+k)&31) %modulo + modulo ) % modulo;
if (sum == hashValue) {
return s.substring(i, i + k);
}
}
return null;
}
}

View File

@@ -0,0 +1,75 @@
package com.dota.slidingWindow._2953;
import java.util.Arrays;
class Solution {
public static void main(String[] args) {
int t = new Solution().countCompleteSubstrings("jjjqq",1);
System.out.println(t);
}
public int countCompleteSubstrings(String word, int k) {
int sum = 0;
var book = new int[word.length()];
Arrays.fill(book, 100);
for (int j = 1; j < word.length(); j++) {
if (Math.abs(word.charAt(j) - word.charAt(j - 1)) <= 2) {
book[j] = 1;
}
}
for (int i = k; i <= word.length(); i += k) {
var dp = new int[26];
int cnt = 0;
for (int j = 0; j < i; j++) {
dp[word.charAt(j) - 'a']++;
if (j != 0) {
cnt += book[j];
}
}
var flag = true;
for (int j = 0; j < 26; j++) {
if (dp[j] != 0 && dp[j] != k) {
flag = false;
break;
}
}
if (flag && cnt == (i - 1)) {
sum++;
}
int l = 0;
for (int j = i; j < word.length(); j++) {
dp[word.charAt(l) - 'a']--;
l++;
cnt -= book[l];
dp[word.charAt(j) - 'a']++;
cnt += book[j];
flag = true;
for (int jj = 0; jj < 26; jj++) {
if (dp[jj] != 0 && dp[jj] != k) {
flag = false;
break;
}
}
if (flag && cnt == i-1 ) {
sum++;
}
}
}
return sum;
}
public int percentageLetter(String s, char letter) {
int cnt = 0;
for (char c : s.toCharArray()) {
if (c==letter) {
cnt++;
}
}
return cnt*100/s.length();
}
}