美团题目

LeetCode复试
This commit is contained in:
whai 2024-03-24 18:48:26 +08:00
parent 81886a3091
commit 385e14423e
14 changed files with 1325 additions and 0 deletions

View File

@ -0,0 +1,328 @@
package cn.whaifree.interview.Meituan;
import java.util.Scanner;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/23 14:28
* @注释
*/
public class MeiTuan24_3_9 {
/**
* MT 是美团的缩写因此小美很喜欢这两个字母
* 现在小美拿到了一个仅由大写字母组成字符串她可以最多操作k次每次可以修改任意一个字符
* 小美想知道操作结束后最多共有多少个'M'''字符?
*
* 输入描述
*
* 第一行输入两个正整数n,k代表字符串长度和操作次数第二行输入一个长度为n的仅由大写字母组成的字符串1<=k<=n<=10^5输出描述
*
* 输出操作结束后最多共有多少个'M''T'字符
*/
static class Problem_1{
public static void main(String[] args) {
/**
* next()方法读取到 空白符空格回车tab 就结束l
* nextLine()读取到 回车 结束也就是\r
*/
Scanner scanner = new Scanner(System.in);
int n, k;
n = scanner.nextInt(); //读取int 自动跳过空格换行制表符
k = scanner.nextInt(); // 读取int
String s = scanner.next(); // 读取空格制表符和换行符
// nextLine读取 回车 nextLine()自动读取了被next()去掉的Enter作为它的结束符
// 解决方法在每一个 next()nextDouble() nextFloat()nextInt() 等语句之后加一个nextLine()语句将被next()去掉的Enter等结束符过滤掉
int isMT = 0;
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'M' || s.charAt(i) == 'T') {
isMT++;
}
}
System.out.println(Math.min(isMT + k, s.length()));
}
}
static class Problem_2{
/**
* 小美拿到了一个由正整数组成的数组但其中有一些元素是未知的(用0来表示)
* 现在小美想知道如果那些未知的元素在区间[l,r]范围内随机取值的话数组所有元素之和的最小值和最大值分别是多少?
* 共有q次询问
* 输入描述
* 第一行输入两个正整数n,q代表数组大小和询问次数
* 第二行输入n个整数ai其中如果输入ai的为 0那么说明ai是未知的
* 接下来的q行每行输入两个正整数r代表一次询问
*
* @param args
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int q = scanner.nextInt();
int isUnKnow = 0;
long sum = 0;
for (int i = 0; i < n; i++) {
int j = scanner.nextInt();
if (j == 0) {
isUnKnow++;// 未知个数
}
sum += j;
}
for (int i = 0; i < q; i++) {
long left = scanner.nextInt();
long right = scanner.nextInt();
System.out.println(sum + left * isUnKnow + " " +(sum + right * isUnKnow));
}
}
}
static class Problem_3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[][] ints = new int[n][n];
for (int i = 0; i < n; i++) {
String next = scanner.next();
for (int j = 0; j < n; j++) {
ints[i][j] = next.charAt(j) - 48;
}
}
for (int i = 1; i <= n; i++) {
int cnt = 0;
for (int x = 1; x <= n - i + 1; x++) {
for (int y = 1; y <= n - i + 1; y++) {
int sum = ints[x + i - 1][y + i - 1] - ints[x + i - 1][y - 1] - ints[x - 1][y + i - 1] + ints[x - 1][y - 1];
if (sum * 2 == i * i) {
cnt++;
}
}
}
System.out.println(cnt);
}
}
}
static class Problem_4 {
/**
* 区间删除
* 小美拿到了一个大小为n的数组
* 她希望删除一个区间后
* 使得剩余所有元素的乘积末尾至少有k个 0
* 小美想知道一共有多少种不同的删除方案?
* @param args
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int k = scanner.nextInt();
int[] ints = new int[n];
for (int i = 0; i < n; i++) {
int j = scanner.nextInt();
ints[i] = j;
}
staticYZ(ints, k);
}
public static void staticYZ(int[] ints,int k) {
/**
* 起始只要统计2和5的数量用双指针只要2和5的因子数量满足(2^p*5^p)>10^k p=min(i,j)
* 即p>k就满足条件并且此时指针变大一定会更大
*/
// 统计全部的因子数量和每个元素的2和5的因子数量
int[] two = new int[ints.length];
int[] five = new int[ints.length]; // 表示每个元素对应因子为5的数量
int twoESum = 0;
int fiveESum = 0;
for (int i = 0; i < ints.length; i++) {
int twoE = calcYZSL(ints[i], 2);
int fiveE = calcYZSL(ints[i], 5);
twoESum += twoE;
fiveESum += fiveE;
two[i] += twoE;
five[i] += fiveE;
}
// for (int i = 0; i < ints.length; i++) {
// int anInt = ints[i];
// while (anInt % 2 == 0) {
// anInt /= 2;
// twoESum++;
// two[i]++;
// }
// while (anInt % 5 == 0) {
// anInt /= 5;
// fiveESum++;
// five[i]++;
// }
// }
long res = 0;
int left = 0;
int right = 0;
while (right < ints.length) {
twoESum -= two[right]; // twoESum表示不在滑动窗口区间的2总因子数
fiveESum -= five[right];
while (Math.min(twoESum, fiveESum) < k && left <= right) {
// 如果右边存在的因子总数太小则左边因子很大
// 可以让Left指针右移动直到满足小的范围则此时的r-l+1就是一次res
twoESum += two[left];
fiveESum += five[left];
left++;
}
res +=(long) (right - left + 1);
right++;
}
System.out.println(res);
}
/**
* 计算num因子k的数量
* @param num
* @param k
* @return -1表示不是因子否则返回其因子 10,5返回2
*/
public static int calcYZSL(int num, int k) {
int res = 0;
while (num % k == 0) {
num /= k;
res++;
}
return res;
}
public void delete(int[] nums, int k) {
int left = 0;
int right = 0;
int pro = 1;
int res = 0;
while (right < nums.length) {
pro *= nums[right];
while (isHasKZero(pro, k)) {
res++;
pro /= nums[left];
left++;
}
right++;
}
System.out.println(res);
}
// public static void backTracking(int[] ints, int start, int sum, int k) {
//// if (isTrue(sum, k)) {
//// res++;
//// }else {
//// return;
//// }
// if (start > ints.length) {
// return;
// }
//
// for (int i = start; i < ints.length; i++) {
// backTracking(ints, i + 1, sum / ints[i], k);
// }
// }
public static boolean isHasKZero(int sum, int k) {
for (int i = 0; i < k; i++) {
if (sum % 10 == 0) {
sum /= 10;
}else {
return false;
}
}
return true;
}
}
/**
* 关键在于数组能不能通过i直达j
*/
static class Problem_5{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int numberPerson = scanner.nextInt();
int relation = scanner.nextInt();
int operation = scanner.nextInt();
boolean[][] isKnow = new boolean[numberPerson][numberPerson];
for (int i = 0; i < relation; i++) {
int j = scanner.nextInt()-1;
int e = scanner.nextInt()-1;
if (j > e) {
isKnow[e][j] = true;
}else {
isKnow[j][e] = true;
}
}
for (int i = 0; i < operation; i++) {
int opr = scanner.nextInt();//操作类型
int j = scanner.nextInt()-1;
int e = scanner.nextInt()-1;
if (opr == 1) {
if (j>e){
isKnow[e][j] = false;
}else {
isKnow[j][e] = false;
}
}else{
if (judgeIsKnow(isKnow, j, e, 0) || judgeIsKnow(isKnow, e, j, 0)) {
System.out.println("yes");
} else {
System.out.println("No");
}
}
}
}
/**
* i能不能到达j
* @param know
* @param i 出发节点
* @param j 结束节点
* @param k 记录次数防止
* @return
*/
public static boolean judgeIsKnow(boolean[][] know, int i, int j, int k) {
if (k == know.length) {
return false;
}
if (know[i][j]) {
return true;
}
for (int l = 0; l < know.length; l++) {
if (know[i][l]){
if (judgeIsKnow(know,l, j, l + 1)||judgeIsKnow(know,j, l, l + 1)) {
return true;
}
}
}
return false;
}
}
}

View File

@ -0,0 +1,64 @@
package cn.whaifree.leetCode.Array;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/23 16:13
* @注释
*/
public class LeetCode713 {
@Test
public void test() {
int[] nums = {1,2,3};
int k = 0;
int res = new Solution1().numSubarrayProductLessThanK(nums, k);
System.out.println(res);
}
class Solution {
public int numSubarrayProductLessThanK(int[] nums, int k) {
int res = 0;
int pro = 1;
for (int left = 0, right = 0; right < nums.length; right++) {
pro *= nums[right];
while (pro >= k) {
// 一旦 找到比k小的左边就往前并且pro得到最新的
pro /= nums[left++];
}
res += right - left + 1;
}
return res;
}
}
class Solution1 {
public int numSubarrayProductLessThanK(int[] nums, int k) {
if (k <= 1) return 0; //1 <= nums[i] <= 1000
int res = 0;
int pro = 1;
int left = 0;
int right = 0;
while (right < nums.length) {
pro *= nums[right];
// 直到总乘积大于k左边才左移
while (pro >= k) {
pro /= nums[left];
left++;
}
res += right - left + 1;
right++;
}
return res;
}
}
}

View File

@ -0,0 +1,135 @@
package cn.whaifree.redo.redo_24_3_24;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/24 12:16
* @注释
*/
public class LCR001 {
@Test
public void test() {
Solution solution = new Solution();
int a = -2147483648;
int b = 2;
int res = solution.divide(a, b);
System.out.println(res);
}
class Solution {
public int divide(int a, int b) {
//特殊情况1, b=1
if (b == 1){
return a;
}
//特殊情况2, b=-1
if (b == -1){
return a == Integer.MIN_VALUE ? Integer.MAX_VALUE : -a;
}
//特殊情况3, a=0
if (a == 0){
return 0;
}
/**
* 17/3
*
* 17 3<<1=6 6<<1=12 12<<1=24 2^2
* 5 3<<1=6 2^0
*
*/
//确定符号
boolean positive = (a ^ b) >= 0;
//为避免溢出, 转换为负数进行计算 -2^31 <= a, b <= 2^31 - 1
a = a < 0 ? a : -a;
b = b < 0 ? b : -b;
int res = 0;
while (a <= b) {
int i = b; // 标记被除数的变化不断x2
int tmpRes = 1; // 记录包含了多少个b
while (a - i <= i) {
i <<= 1;
tmpRes <<= 1;
}
res += tmpRes;
a = a - i;
}
return positive ? res : -res;
}
}
class Solution1 {
/**
* 没有转为负数处理会错误
* 输入
* a =-2147483648
* b =2
* 输出
* 0
* 预期结果
* -1073741824
* @param a
* @param b
* @return
*/
public int divide(int a, int b) {
//特殊情况1, b=1
if (b == 1){
return a;
}
//特殊情况2, b=-1
if (b == -1){
return a == Integer.MIN_VALUE ? Integer.MAX_VALUE : -a;
}
//特殊情况3, a=0
if (a == 0){
return 0;
}
/**
* 17/3
*
* 17 3<<1=6 6<<1=12 12<<1=24 2^2
* 5 3<<1=6 2^0
*
*/
//确定符号
boolean positive = (a ^ b) >= 0;
//为避免溢出, 转换为负数进行计算 -2^31 <= a, b <= 2^31 - 1
a = a < 0 ? -a : a;
b = b < 0 ? -b : b;
int res = 0;
while (a >= b) {
int i = b; // 标记被除数的变化不断x2
int tmpRes = 1; // 记录包含了多少个b
while (a - i >= i) {
i <<= 1;
tmpRes <<= 1;
}
res += tmpRes;
a = a - i;
}
return positive ? res : -res;
}
}
}

View File

@ -0,0 +1,54 @@
package cn.whaifree.redo.redo_24_3_24;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/24 12:55
* @注释
*/
public class LCR090 {
@Test
public void test() {
int[] nums = {1,7,9,2};
System.out.println(new Solution().rob(nums));
}
class Solution {
public int rob(int[] nums) {
if (nums.length == 0) {
return 0;
}
if (nums.length == 1) {
return nums[0];
}
if (nums.length == 2) {
return Math.max(nums[0], nums[1]);
}
//如果两间相邻的房屋在同一晚上被小偷闯入系统会自动报警
//环形街道
return Math.max(rob(nums, 0, nums.length - 2), rob(nums, 1, nums.length - 1));
}
public int rob(int[] nums, int start, int end) {
int dp[] = new int[nums.length];
// dpi表示 到i最高能偷窃的个数
// dp[start]=1
// dp[start+1]=1
// dp[i] = max(dp[i-2]+nums[i],dp[i-1])
dp[start] = nums[start];
dp[start + 1] = Math.max(nums[start], nums[start + 1]);
for (int i = start + 2; i <= end; i++) {
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
}
return dp[end];
}
}
}

View File

@ -0,0 +1,92 @@
package cn.whaifree.redo.redo_24_3_24;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/24 13:16
* @注释
*/
public class LeetCode005 {
@Test
public void test() {
String s = "babad";
String res = new Solution1().longestPalindrome(s);
System.out.println(res);
}
class Solution {
/**
* 确定中间节点不断向外扩展判断有几个回文
* - 中间为1个字符
* - 中间为2个字符
* @param s
* @return
*/
public String longestPalindrome(String s) {
char[] chars = s.toCharArray();
String res = "";
for (int i = 0; i < chars.length; i++) {
String index1 = getHuiWenByIndex(chars, i, i);
if (index1.length() > res.length()) {
res = index1;
}
String index2 = getHuiWenByIndex(chars, i, i + 1);
if (index2.length() > res.length()) {
res = index2;
}
}
return res;
}
public String getHuiWenByIndex(char[] chars, int left,int right) {
while (left >= 0 && right < chars.length && chars[left] == chars[right]) {
left--;
right++;
}
return new String(chars, left + 1, right - left - 1);
}
}
class Solution1 {
/**
* 动态规划
* @param s
* @return
*/
public String longestPalindrome(String s) {
/**
* dp[i][j] 表示i-j是否为回文串
* dp[i][j] =
* s[i] == s[j]
* 1. i=j true
* 2. i=j+1 true
* 3. dp[i+1][j-1] 确定了遍历顺序 从下往上从左往右
*/
int length = s.length();
boolean[][] dp = new boolean[length][length];
int left = 0;
int right = 0;
for (int i = length - 1; i >= 0; i--) {
for (int j = i; j < length; j++) {
if (s.charAt(i) == s.charAt(j)) {
if (i == j || i == j - 1 || dp[i + 1][j - 1]) {
if (right - left <= j - i) {
left = i;
right = j;
}
dp[i][j] = true;
}
}
}
}
return s.substring(left, right + 1);
}
}
}

View File

@ -0,0 +1,79 @@
package cn.whaifree.redo.redo_24_3_24;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/24 15:09
* @注释
*/
public class LeetCode1049 {
@Test
public void test()
{
Solution solution = new Solution();
int[] stones = new int[]{31,26,33,21,40};
int i = solution.lastStoneWeightII(stones);
System.out.println(i);
}
class Solution {
/**
* 背包问题
*
* dp[i][j] 表示0-i物品里选择 放入j背包
*
* 石头的总重量的一半为目标找到几块石头可以尽可能满足half
*
*
*
* @param stones
* @return
*/
public int lastStoneWeightII(int[] stones) {
int sum = 0;
for (int stone : stones) {
sum += stone;
}
int half = sum / 2; //背包容量
int[][] dp = new int[stones.length][half + 1];
/**
* 2 3 4 5 3
*
* 0 1 2 3 4
* 0 0 0 2 2 2
* 1
* 2
* 3
* 4
*
* 1. 放入物品i dp[i][j] = dp[i][j-weight[i]]+values[i]
* 2. 不放物品i = dp[i-1][j]
*
*/
for (int i = stones[0]; i <= half; i++) {
dp[0][i] = stones[0];
}
for (int i = 1; i < stones.length; i++) {
for (int j = 1; j <= half; j++) {
if (stones[i] > j) {
// 放不下
dp[i][j] = dp[i - 1][j];
}else {
dp[i][j] = Math.max(dp[i - 1][j - stones[i]] + stones[i], dp[i - 1][j]);
}
}
}
return sum - 2 * dp[stones.length - 1][half];
}
}
}

View File

@ -0,0 +1,52 @@
package cn.whaifree.redo.redo_24_3_24;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/24 17:09
* @注释
*/
public class LeetCode343 {
@Test
public void test()
{
Solution solution = new Solution();
int i = solution.integerBreak(10);
System.out.println(i);
}
class Solution {
/**
* dp[j] 表示 j拆分出来的的最大乘积的最大值
* - dp[j]
* j * i
* dp[j-i]+dp[i] 就是对j再进行拆分
*
* 10
* dp
* 0 1 2 3 4 5 6 7 8 9 10
* 9 10 21 24 25 25
* @param n
* @return
*/
public int integerBreak(int n) {
int[] dp = new int[n + 1];
dp[0] = 0;
dp[1] = 1;
dp[2] = 1;
for (int i = 3; i <= n; i++) {
for (int j = 1; j < i; j++) {
int max = Math.max((i - j) * j, dp[i - j] * j);
dp[i] = Math.max(dp[i], max);
}
}
return dp[n];
}
}
}

View File

@ -0,0 +1,100 @@
package cn.whaifree.redo.redo_24_3_24;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/24 18:22
* @注释
*/
public class LeetCode416 {
@Test
public void test() {
int[] nums = {1,6,5};
boolean canPartition = new Solution().canPartition(nums);
System.out.println(canPartition);
}
class Solution {
/**
* 把n个数放入到背包中
* dp[j] 为0-i中任取放入容量为j的背包的最大价值让价值等于nums总数的一半
*
* @param nums
* @return
*/
public boolean canPartition(int[] nums) {
int sum = 0;
for (int num : nums) {
sum += num;
}
if (sum % 2 == 1) {
return false;
}
int half = sum / 2;
int[] dp = new int[half + 1];
/**
* 内层循环从容量的一半开始逐渐减小直到小于当前物品的重量
*/
for (int i = 0; i < nums.length; i++) {
for (int j = half; j >= nums[i]; j--) {
dp[j] = Math.max(dp[j], dp[j - nums[i]] + nums[i]);
if(dp[half] == half) return true;
}
}
return dp[half] == half;
}
}
class Solution1 {
/**
* 把n个数放入到背包中
* dp[i][j] 为0-i中任取放入容量为j的背包的最大价值让价值等于nums总数的一半
*
* @param nums
* @return
*/
public boolean canPartition(int[] nums) {
int sum = 0;
for (int num : nums) {
sum += num;
}
if (sum % 2 == 1) {
return false;
}
int half = sum / 2;
int[][] dp = new int[nums.length][half + 1];
for (int i = nums[0]; i < half + 1; i++) {
dp[0][i] = nums[0];
}
for (int i = 1; i < nums.length; i++) {
for (int j = 1; j <= half; j++) {
if (j < nums[i]) {
dp[i][j] = dp[i - 1][j];
}else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - nums[i]] + nums[i]);
}
}
}
return dp[nums.length - 1][half] == half;
}
}
}

View File

@ -0,0 +1,48 @@
package cn.whaifree.redo.redo_24_3_24;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/24 16:14
* @注释
*/
public class LeetCode45 {
@Test
public void test()
{
Solution solution = new Solution();
int[] nums = {2,3,1,1,4};
int jump = solution.jump(nums);
System.out.println(jump);
}
class Solution {
public int jump(int[] nums) {
if (nums.length == 1) {
return 0;
}
int maxCover = 0;
int curCover = 0; // 当前区间
int jump = 0;
for (int i = 0; i < nums.length; i++) {
maxCover = Math.max(maxCover, i + nums[i]);
// 当遍历到达当前覆盖范围的边界时更新当前覆盖范围的起始位置并增加一次跳跃次数
if (i == curCover) {
curCover = maxCover;
jump++;
}
// 如果当前覆盖范围已经到达或超过数组最后一个位置则返回跳跃次数
if (curCover >= nums.length - 1) {
return jump;
}
}
return jump;
}
}
}

View File

@ -0,0 +1,84 @@
package cn.whaifree.redo.redo_24_3_24;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/24 14:02
* @注释
*/
public class LeetCode647 {
@Test
public void test()
{
System.out.println(new Solution1().countSubstrings("aaa"));
}
class Solution {
public int countSubstrings(String s) {
char[] chars = s.toCharArray();
int res = 0;
for (int i = 0; i < chars.length; i++) {
res += getHuiWenCount(chars, i, i);
res += getHuiWenCount(chars, i, i + 1);
}
return res;
}
/**
*
* @param chars
* @param left
* @param right
* @return left right 为中心的回文串的个数
*/
public int getHuiWenCount(char[] chars, int left, int right) {
int res = 0;
while (left >= 0 && right < chars.length && chars[left] == chars[right]) {
res++;
left--;
right++;
}
return res;
}
}
class Solution1 {
/**
* dp[i][j] 表示i-j内回文的个数
*
* s[i]==s[j]
* 1. i=j 1
* 2. i=j-1 2
* 3. dp[i+1][j-1] + 1 从下往上左到右遍历
*
* @param s
* @return
*/
public int countSubstrings(String s) {
char[] chars = s.toCharArray();
int length = chars.length;
boolean[][] dp = new boolean[chars.length][chars.length];
int res = 0;
for (int i = length - 1; i >= 0; i--) {
for (int j = i; j < length; j++) {
if (chars[i] == chars[j]) {
if (j - i == 1 || i == j || dp[i + 1][j - 1]) {
dp[i][j] = true;
res++;
}
}
}
}
return res;
}
}
}

View File

@ -0,0 +1,45 @@
package cn.whaifree.redo.redo_24_3_24;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/24 14:33
* @注释
*/
public class LeetCode713 {
@Test
public void test()
{
int[] nums = {10,5,2,6};
int k = 100;
int res = new Solution().numSubarrayProductLessThanK(nums,k);
System.out.println(res);
}
class Solution {
public int numSubarrayProductLessThanK(int[] nums, int k) {
if (k == 0) {
return 0;
}
int left = 0;
int right = 0;
int pro = 1;
int res = 0;
while (right < nums.length) {
pro *= nums[right];
while (pro >= k && left <= right) { // 111 1的时候但也可以修改 if (k <= 1) return 0; 因为 1 <= nums[i] <= 1000
pro /= nums[left];
left++;
}
res += right - left + 1;
right++;
}
return res;
}
}
}

View File

@ -0,0 +1,44 @@
package cn.whaifree.redo.redo_24_3_24;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/24 15:43
* @注释
*/
public class LeetCode738 {
@Test
public void test() {
int n =10;
Solution solution = new Solution();
System.out.println(solution.monotoneIncreasingDigits(n));
}
class Solution {
public int monotoneIncreasingDigits(int n) {
String s = String.valueOf(n);
char[] chars = s.toCharArray();
/**
* 98
* 89
*/
int index = chars.length;
for (int i = chars.length - 1; i > 0; i--) {
if (chars[i] < chars[i - 1]) {
chars[i - 1] -= 1;
index = i-1;
}
}
for (int i = index + 1; i < chars.length; i++) {
chars[i] = '9';
}
return Integer.parseInt(new String(chars));
}
}
}

View File

@ -0,0 +1,62 @@
package cn.whaifree.redo.redo_24_3_24;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/24 16:57
* @注释
*/
public class LeetCode96 {
@Test
public void test()
{
Solution solution = new Solution();
int i = solution.numTrees(3);
System.out.println(i);
}
class Solution {
public int numTrees(int n) {
if (n <= 2) {
return n;
}
/**
*
* 二叉搜索树
*
* dp[i] 表示i颗树有的可能性
* dp[0] = 1
* dp[1] = 2
*
* n = 3
* 以1为头 以2为头 以3为头
* dp[0] * dp[2] +
* dp[1] * dp[1] +
* dp[2] * dp[0]
*
* n
* dp[i] * dp[n-i-1]
* ....
* dp[n-i-1] * dp[i]
*/
int[] dp = new int[n + 1];
dp[0] = 1;
dp[1] = 1;
dp[2] = 2;
for (int i = 3; i < dp.length; i++) {
int sum = 0;
for (int j = 0; j < i; j++) {
sum += (dp[i - j - 1] * dp[j]);
}
dp[i] = sum;
}
return dp[n];
}
}
}

View File

@ -0,0 +1,138 @@
package cn.whaifree.test;
import java.util.HashSet;
import java.util.Scanner;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/23 11:09
* @注释
*/
public class MT {
// public static void main(String[] args) {
//
// Scanner in = new Scanner(System.in);
//
// String[] s = in.nextLine().split(" ");
//
// int a = Integer.parseInt(s[0]);
// int b = Integer.parseInt(s[1]);
// int[][] ints = new int[a][b];
// for(int i = 0 ; i<a;i++){
// String[] split = in.nextLine().split("");
// int index = 0;
// for(;index<b;index++){
// ints[i][index] = Integer.parseInt(split[index]);
// }
// }
//
// int res = 0;
// for (int i = 0; i < a - 1; i++) {
// for (int j = 0; j < b - 1; j++) {
// if (compute(ints, i, j)) {
// res++;
// }
// }
// }
// System.out.println(res);
// }
//
// public static boolean compute(int[][] ints ,int start,int startB){
//
// int one = 0;
// int zero = 0;
// for(int i = start; i<start+2;i++){
// for(int j = startB; j<startB+2;j++){
// if(ints[i][j]==1){
// one++;
// }else{
// zero++;
// }
// }
// }
// return one == zero;
//
// }
// public static void main(String[] args) {
// Scanner in = new Scanner(System.in);
// int n = in.nextInt();
// String s = in.nextLine();
//
// char[] chars = s.toCharArray();
// int res = 0;
// for (int i = 0; i < s.length(); i++) {
// for (int j = 0; j < s.length(); j++) {
// if (isHuiWenAndOS(s, i, j)) {
// s = new String()
// }
// }
// }
//
//
// }
//
// public static boolean isHuiWenAndOS(String s,int start,int end){
//
// while (start < end) {
// if (s.charAt(start) != s.charAt(end)) {
// return false;
// }
// start++;
// end--;
// }
//
// return (end - start) % 2 == 0;
// }
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] split = in.nextLine().split(" ");
int n = Integer.parseInt(split[0]);
int k = Integer.parseInt(split[1]);
String[] s = in.nextLine().split("");
StringBuilder sb = new StringBuilder();
for(int i= 0;i<s.length;i+=4){
int number = Integer.parseInt(s[i+2]);
for(;number>0;number--){
sb.append(s[i]);
}
}
System.out.println(res(sb.toString().toCharArray(), k, 0));
}
public static int res(char[] chars,int k,int start){
if(start>=chars.length){
return 0;
}
int weight = 0;
HashSet<Character> set = new HashSet<>();
int i;
for(i = start ; weight<k ;i++ ){
set.add(chars[i]);
weight = set.size() * (i - start + 1);
}
return weight < k ? 0 : res(chars, k, i) + 1;
}
}