This commit is contained in:
kkunkka
2023-12-16 14:19:36 +08:00
parent 781a17e31e
commit 4653a6e847
3 changed files with 62 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
package com.dota.arr._2656;
/**
* 2656. K 个元素的最大和
*/
class Solution {
public int maximizeSum(int[] nums, int k) {
var max = nums[0];
for (int num : nums) {
if (num>max) {
max = num;
}
}
return max * k + k*(k-1)/2;
}
}

View File

@@ -0,0 +1,5 @@
package com.dota.rank;
public class Solution {
}

View File

@@ -0,0 +1,41 @@
package com.dota.treeMap;
import java.util.Map;
import java.util.TreeMap;
public class Solution {
}
/**
* 2276. 统计区间中的整数数目
*/
class CountIntervals {
TreeMap<Integer, Integer> map = new TreeMap<>();
int cnt = 0;
public CountIntervals() {
}
public void add(int left, int right) {
Map.Entry<Integer, Integer> inte = map.floorEntry(right);
while (inte != null && inte.getValue() >= left) {
int l = inte.getKey();
int r = inte.getValue();
left = Math.min(l, left);
right = Math.max(r, right);
cnt -= r - l + 1;
map.remove(l);
inte = map.floorEntry(right);
}
cnt += (right - left + 1);
map.put(left, right);
}
public int count() {
return cnt;
}
}