2023-12-03 22:53:26 +08:00
|
|
|
package com.dota;
|
|
|
|
|
|
|
|
public class Solution {
|
2025-03-05 12:44:36 +08:00
|
|
|
// 1423. 可获得的最大点数
|
|
|
|
public int maxScore(int[] cardPoints, int k) {
|
|
|
|
int max;
|
|
|
|
int n = cardPoints.length;
|
|
|
|
int len = n - k;
|
|
|
|
int sum = 0;
|
|
|
|
for (int cardPoint : cardPoints) {
|
|
|
|
sum += cardPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int temp = 0;
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
temp += cardPoints[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
max = temp;
|
|
|
|
for (int i = len; i < n; i++) {
|
|
|
|
temp -= cardPoints[i - len];
|
|
|
|
temp += cardPoints[i];
|
|
|
|
if (temp < max) {
|
|
|
|
max = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sum - max;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2048. 下一个更大的数值平衡数
|
|
|
|
public int nextBeautifulNumber(int n) {
|
|
|
|
while (!ok(++n)) {
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean ok(int n) {
|
|
|
|
if (n == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int[] dp = new int[10];
|
|
|
|
while (n != 0) {
|
|
|
|
int t = n % 10;
|
|
|
|
dp[t]++;
|
|
|
|
if (dp[t] > t) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
n /= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 1; i < dp.length; i++) {
|
|
|
|
if (dp[i] != 0 && dp[i] != i) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2024-09-23 15:43:28 +08:00
|
|
|
}
|