双周赛65~67
This commit is contained in:
parent
bb165af9ae
commit
fb342e0ab9
113
src/main/java/contest/y2021/m11/dw/SolutionD65.java
Normal file
113
src/main/java/contest/y2021/m11/dw/SolutionD65.java
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
package contest.y2021.m11.dw;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class SolutionD65 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SolutionD65 solution = new SolutionD65();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean checkAlmostEquivalent(String word1, String word2) {
|
||||||
|
int[] arr = new int[26];
|
||||||
|
for (int i = 0; i < word1.length(); i++) {
|
||||||
|
arr[word1.charAt(i) - 'a']++;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < word2.length(); i++) {
|
||||||
|
arr[word2.charAt(i) - 'a']--;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < 26; i++) {
|
||||||
|
if (arr[i] > 3 || arr[i] < -3) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
class Robot {
|
||||||
|
|
||||||
|
int[][] arrs;
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
int dir;
|
||||||
|
|
||||||
|
public Robot(int width, int height) {
|
||||||
|
arrs = new int[width][height];
|
||||||
|
x = 0;
|
||||||
|
y = 0;
|
||||||
|
dir = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void move(int num) {
|
||||||
|
num = num % (2 * arrs.length + 2 * arrs[0].length - 4);
|
||||||
|
switch (dir) {
|
||||||
|
case 0:
|
||||||
|
if (x + num >= arrs.length) {
|
||||||
|
dir = (dir + 1) % 4;
|
||||||
|
num -= arrs.length - 1 - x;
|
||||||
|
x = arrs.length - 1;
|
||||||
|
move(num);
|
||||||
|
} else {
|
||||||
|
x += num;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
if (y + num >= arrs[0].length) {
|
||||||
|
dir = (dir + 1) % 4;
|
||||||
|
num -= arrs[0].length - 1 - y;
|
||||||
|
y = arrs[0].length - 1;
|
||||||
|
move(num);
|
||||||
|
} else {
|
||||||
|
y += num;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
if (x - num < 0) {
|
||||||
|
dir = (dir + 1) % 4;
|
||||||
|
num -= x;
|
||||||
|
x = 0;
|
||||||
|
move(num);
|
||||||
|
} else {
|
||||||
|
x -= num;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if (y - num < 0) {
|
||||||
|
dir = (dir + 1) % 4;
|
||||||
|
num -= y;
|
||||||
|
y = 0;
|
||||||
|
move(num);
|
||||||
|
} else {
|
||||||
|
y -= num;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[] getPos() {
|
||||||
|
return new int[]{x, y};
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDir() {
|
||||||
|
String str;
|
||||||
|
switch (dir) {
|
||||||
|
case 0:
|
||||||
|
str = "East";
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
str = "North";
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
str = "West";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
str = "South";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
88
src/main/java/contest/y2021/m11/dw/SolutionD66.java
Normal file
88
src/main/java/contest/y2021/m11/dw/SolutionD66.java
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
package contest.y2021.m11.dw;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class SolutionD66 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SolutionD66 solution = new SolutionD66();
|
||||||
|
// System.out.println(solution.minCost(new int[]{1, 0}, new int[]{2, 3}, new int[]{5, 4, 3}, new int[]{8, 2, 6, 7}));
|
||||||
|
// System.out.println(solution.minCost(new int[]{2, 0}, new int[]{2, 2}, new int[]{8,5,6,12,10}, new int[]{1,8,18,11,24,16}));
|
||||||
|
}
|
||||||
|
|
||||||
|
public int countWords(String[] words1, String[] words2) {
|
||||||
|
Map<String, Integer> map = new HashMap<>();
|
||||||
|
for (String str : words1) {
|
||||||
|
map.put(str, map.getOrDefault(str, 0) + 1);
|
||||||
|
}
|
||||||
|
for (String key : map.keySet()) {
|
||||||
|
if (map.get(key) > 1) {
|
||||||
|
map.put(key, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (String str : words2) {
|
||||||
|
map.put(str, map.getOrDefault(str, 0) - 1);
|
||||||
|
}
|
||||||
|
int count = 0;
|
||||||
|
for (String key : map.keySet()) {
|
||||||
|
if (map.get(key) == 0) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int minimumBuckets(String street) {
|
||||||
|
if (!street.contains("H")) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (street.contains("HHH") || street.lastIndexOf("HH") == street.length() - 2 || street.indexOf("HH") == 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
int count = 0;
|
||||||
|
char[] chs = street.toCharArray();
|
||||||
|
for (int i = 0; i < chs.length; i++) {
|
||||||
|
if (chs[i] == '.') {
|
||||||
|
continue;
|
||||||
|
} else if (chs[i] == 'B') {
|
||||||
|
continue;
|
||||||
|
} else if (i > 0 && chs[i - 1] == 'B') {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
count++;
|
||||||
|
if (i < chs.length - 1 && chs[i + 1] == '.') {
|
||||||
|
chs[i + 1] = 'B';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int minCost(int[] startPos, int[] homePos, int[] rowCosts, int[] colCosts) {
|
||||||
|
int row = Integer.compare(homePos[0], startPos[0]);
|
||||||
|
int col = Integer.compare(homePos[1], startPos[1]);
|
||||||
|
if (row == 0 && col == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int sum = 0;
|
||||||
|
if (row == 1) {
|
||||||
|
for (int i = startPos[0] + 1; i <= homePos[0]; i++) {
|
||||||
|
sum += rowCosts[i];
|
||||||
|
}
|
||||||
|
} else if (row == -1) {
|
||||||
|
for (int i = startPos[0] - 1; i >= homePos[0]; i--) {
|
||||||
|
sum += rowCosts[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (col == 1) {
|
||||||
|
for (int i = startPos[1] + 1; i <= homePos[1]; i++) {
|
||||||
|
sum += colCosts[i];
|
||||||
|
}
|
||||||
|
} else if (col == -1) {
|
||||||
|
for (int i = startPos[1] - 1; i >= homePos[1]; i--) {
|
||||||
|
sum += colCosts[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
}
|
104
src/main/java/contest/y2021/m12/dw/SolutionD67.java
Normal file
104
src/main/java/contest/y2021/m12/dw/SolutionD67.java
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
package contest.y2021.m12.dw;
|
||||||
|
|
||||||
|
import com.code.leet.entiy.TwoArray;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class SolutionD67 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SolutionD67 solution = new SolutionD67();
|
||||||
|
// solution.maxSubsequence(new int[]{50, -75}, 2);
|
||||||
|
// solution.goodDaysToRobBank(new int[]{5, 3, 3, 3, 5, 6, 2}, 2);
|
||||||
|
TwoArray twoArray = new TwoArray("[[7,26,7],[7,18,4],[3,10,7],[17,50,1],[3,25,10],[85,23,8],[80,50,1],[58,74,1],[38,39,7],[50,51,8],[31,99,3],[53,6,5],[59,27,10],[87,78,9],[68,58,3]]", true);
|
||||||
|
System.out.println(solution.maximumDetonation(twoArray.getArr()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[] maxSubsequence(int[] nums, int k) {
|
||||||
|
int size = nums.length;
|
||||||
|
int[] indexs = Arrays.copyOf(nums, size);
|
||||||
|
int[] arr = new int[k];
|
||||||
|
Arrays.sort(indexs);
|
||||||
|
int min = indexs[size - k];
|
||||||
|
int count = 1;
|
||||||
|
for (int i = size - k + 1; i < size; i++) {
|
||||||
|
if (indexs[i] > indexs[size - k]) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
int index = 0;
|
||||||
|
for (int num : nums) {
|
||||||
|
if (num > min) {
|
||||||
|
arr[index] = num;
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
if (num == min && count > 0) {
|
||||||
|
arr[index] = num;
|
||||||
|
index++;
|
||||||
|
count--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Integer> goodDaysToRobBank(int[] security, int time) {
|
||||||
|
List<Integer> list = new ArrayList<>();
|
||||||
|
int days = security.length;
|
||||||
|
if (time == 0) {
|
||||||
|
for (int i = 0; i < days; i++) {
|
||||||
|
list.add(i);
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
if (time * 2 + 1 > days) {
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
int[] starts = new int[days];
|
||||||
|
int[] ends = new int[days];
|
||||||
|
for (int i = 1; i < days; i++) {
|
||||||
|
if (security[i] <= security[i - 1]) {
|
||||||
|
starts[i] = starts[i - 1] + 1;
|
||||||
|
}
|
||||||
|
if (security[days - 1 - i] <= security[days - i]) {
|
||||||
|
ends[days - 1 - i] = ends[days - i] + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = 0; i < days; i++) {
|
||||||
|
if (starts[i] >= time && ends[i] >= time) {
|
||||||
|
list.add(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int maximumDetonation(int[][] bombs) {
|
||||||
|
int count = bombs.length;
|
||||||
|
boolean[] uses = new boolean[count];
|
||||||
|
int max = 1;
|
||||||
|
for (int i = 0; i < count; i++) {
|
||||||
|
if (uses[i]) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
int temp = 1;
|
||||||
|
Queue<int[]> queue = new LinkedList<>();
|
||||||
|
queue.add(new int[]{bombs[i][0], bombs[i][1], bombs[i][2], i});
|
||||||
|
while (!queue.isEmpty()) {
|
||||||
|
int[] bomb = queue.poll();
|
||||||
|
for (int j = 0; j < count; j++) {
|
||||||
|
if (uses[j] || j == bomb[3]) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
long pos = ((long) bomb[0] - bombs[j][0]) * (bomb[0] - bombs[j][0]) + ((long) bomb[1] - bombs[j][1]) * (bomb[1] - bombs[j][1]);
|
||||||
|
if (pos <= (long) bomb[2] * bomb[2] || pos <= (long) bombs[j][2] * bombs[j][2]) {
|
||||||
|
temp++;
|
||||||
|
queue.add(new int[]{bombs[j][0], bombs[j][1], bombs[j][2], j});
|
||||||
|
uses[j] = true;
|
||||||
|
uses[bomb[3]] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
max = Math.max(max, temp);
|
||||||
|
}
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user