Merge remote-tracking branch 'origin/master'

This commit is contained in:
轩辕龙儿 2023-03-29 16:21:04 +08:00
commit 278a7d7e94
3 changed files with 204 additions and 0 deletions

View File

@ -0,0 +1,76 @@
package contest.y2023;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class BW98 {
public static void main(String[] args) {
BW98 solution = new BW98();
System.out.println(solution.minMaxDifference(456));
}
public long[] handleQuery(int[] nums1, int[] nums2, int[][] queries) {
long sum = Arrays.stream(nums2).sum();
List<Long> list = new ArrayList<>();
for (int[] query : queries) {
if (query[0] == 1) {
for (int i = query[1]; i <= query[2]; i++) {
nums1[i] = 1 - nums1[i];
}
} else if (query[0] == 2) {
sum += (long) query[1] * Arrays.stream(nums1).sum();
} else {
list.add(sum);
}
}
long[] res = new long[list.size()];
for (int i = 0; i < list.size(); i++) {
res[i] = list.get(i);
}
return res;
}
public int minImpossibleOR(int[] nums) {
List<Integer> list = Arrays.stream(nums).boxed().collect(Collectors.toList());
int num = 1;
while (list.contains(num)) {
num *= 2;
}
return num;
}
public int minimizeSum(int[] nums) {
if (nums.length == 3) {
return 0;
}
Arrays.sort(nums);
int min = nums[nums.length - 3] - nums[0];
min = Math.min(min, nums[nums.length - 2] - nums[1]);
min = Math.min(min, nums[nums.length - 1] - nums[2]);
return min;
}
public int minMaxDifference(int num) {
String str = "" + num;
String max = "";
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) != '9') {
max = str.replace(str.substring(i, i + 1), "9");
break;
}
}
if (max.equals("")) {
max = str;
}
String min = str.replace(str.substring(0, 1), "0");
while (min.startsWith("0")) {
min = min.substring(1);
}
if (min.equals("")) {
min = "0";
}
return Integer.parseInt(max) - Integer.parseInt(min);
}
}

View File

@ -0,0 +1,57 @@
package contest.y2023;
import com.code.leet.entiy.TwoArray;
import java.util.*;
public class BW99 {
public static void main(String[] args) {
BW99 solution = new BW99();
}
public int countWays(int[][] ranges) {
Arrays.sort(ranges, (o1, o2) -> {
if (o1[0] == o2[0]) {
return o1[1] - o2[1];
}
return o1[0] - o2[0];
});
int sum = 2;
int max = ranges[0][1];
for (int i = 1; i < ranges.length; i++) {
if (ranges[i][0] > max) {
sum *= 2;
sum %= 1000000007;
max = ranges[i][1];
} else {
max = Math.max(max, ranges[i][1]);
}
}
return sum;
}
public long coloredCells(int n) {
return ((long) n - 1) * (n - 1) + (long) n * n;
}
public int splitNum(int num) {
String str = "" + num;
char[] strs = str.toCharArray();
Arrays.sort(strs);
int num1 = 0;
int num2 = 0;
int index = 0;
while (strs[index] - '0' == 0) {
index++;
}
for (int i = index; i < strs.length; i++) {
if (i % 2 == index % 2) {
num1 = num1 * 10 + (strs[i] - '0');
} else {
num2 = num2 * 10 + (strs[i] - '0');
}
}
return num1 + num2;
}
}

View File

@ -0,0 +1,71 @@
package contest.y2023;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Week333 {
public static void main(String[] args) {
Week333 solution = new Week333();
System.out.println(solution.squareFreeSubsets(new int[]{26, 6, 4, 27, 6, 18}));
}
public int squareFreeSubsets(int[] nums) {
Map<Integer, List<Integer>> map = new HashMap<>();
int[] arrs = new int[]{2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
for (int arr : arrs) {
map.put(arr, new ArrayList<>());
}
int count = 0;
for (int num : nums) {
if (num % 4 == 0 || num % 9 == 0 || num % 16 == 0) {
continue;
}
count++;
for (int arr : arrs) {
if (num % arr == 0) {
List<Integer> list = map.get(arr);
list.add(num);
map.put(arr, list);
}
}
}
return 0;
}
public int minOperations(int n) {
int count = 0;
while (n > 0) {
count++;
int num = (int) Math.sqrt(n);
if (n == Math.pow(num, 2)) {
break;
}
n = (int) Math.min((n - Math.pow(2, num)), (Math.pow(2, num + 1) - n));
}
return count;
}
public int[][] mergeArrays(int[][] nums1, int[][] nums2) {
int[] arr = new int[1000];
for (int[] nums : nums1) {
arr[nums[0] - 1] += nums[1];
}
for (int[] nums : nums2) {
arr[nums[0] - 1] += nums[1];
}
List<int[]> list = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 0) {
list.add(new int[]{i + 1, arr[i]});
}
}
int[][] res = new int[list.size()][2];
for (int i = 0; i < list.size(); i++) {
res[i] = list.get(i);
}
return res;
}
}