2023-12-03 22:53:26 +08:00
|
|
|
package com.dota;
|
|
|
|
|
2025-03-31 11:31:59 +08:00
|
|
|
import org.apache.commons.collections4.list.TreeList;
|
|
|
|
|
2025-03-06 14:33:13 +08:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
2023-12-03 22:53:26 +08:00
|
|
|
public class Solution {
|
2025-03-31 11:31:59 +08:00
|
|
|
public static void main(String[] args) {
|
|
|
|
var a = new TreeList<Integer>();
|
|
|
|
a.add(1);
|
|
|
|
a.add(2);
|
|
|
|
a.add(1);
|
|
|
|
System.out.println(a.get(2));
|
|
|
|
Integer.bitCount(1231);
|
|
|
|
}
|
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;
|
|
|
|
}
|
2025-03-06 14:33:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class Solution2 {
|
|
|
|
public long beautifulSubarrays(int[] nums) {
|
|
|
|
long count = 0;
|
|
|
|
var map = new HashMap<Integer, Integer>();
|
|
|
|
map.put(0, 1);
|
|
|
|
var mask = 0;
|
|
|
|
for (int num : nums) {
|
|
|
|
mask ^= num;
|
|
|
|
count += map.getOrDefault(mask, 0);
|
|
|
|
map.put(mask, map.getOrDefault(mask, 0) + 1);
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
2024-09-23 15:43:28 +08:00
|
|
|
}
|