2021秋季赛
This commit is contained in:
parent
4442d04a34
commit
7904495388
@ -0,0 +1,38 @@
|
||||
package com.code.leet.season.fall2021.contest1;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @description:
|
||||
* @author: Administrator
|
||||
* @date: 2021/9/25-15:15
|
||||
*/
|
||||
public class Solution {
|
||||
|
||||
class TreeNode {
|
||||
int val;
|
||||
TreeNode left;
|
||||
TreeNode right;
|
||||
|
||||
TreeNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
}
|
||||
|
||||
Set<Integer> set = new HashSet<>();
|
||||
|
||||
public int numColor(TreeNode root) {
|
||||
dfs(root);
|
||||
return set.size();
|
||||
}
|
||||
|
||||
private void dfs(TreeNode root) {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
set.add(root.val);
|
||||
dfs(root.left);
|
||||
dfs(root.right);
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package com.code.leet.season.fall2021.contest2;
|
||||
|
||||
import com.code.leet.entiy.TwoArray;
|
||||
import javafx.util.Pair;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @description:
|
||||
* @author: Administrator
|
||||
* @date: 2021/9/25-16:40
|
||||
*/
|
||||
public class Solution {
|
||||
public static void main(String[] args) {
|
||||
Solution solution = new Solution();
|
||||
TwoArray terrain = new TwoArray("[[5,0],[0,6]]", true);
|
||||
TwoArray obstacle = new TwoArray("[[0,6],[7,0]]", true);
|
||||
solution.bicycleYard(new int[]{1, 1}, terrain.getArr(), obstacle.getArr());
|
||||
}
|
||||
|
||||
public int[][] bicycleYard(int[] position, int[][] terrain, int[][] obstacle) {
|
||||
List<int[]> result = new ArrayList<>();
|
||||
Queue<int[]> queue = new LinkedList<>();
|
||||
int[] px = new int[]{-1, 1, 0, 0};
|
||||
int[] py = new int[]{0, 0, -1, 1};
|
||||
queue.add(position);
|
||||
int[][] speeds = new int[terrain.length][terrain[0].length];
|
||||
for (int[] ints : speeds) {
|
||||
Arrays.fill(ints, -1);
|
||||
}
|
||||
speeds[position[0]][position[1]] = 1;
|
||||
while (!queue.isEmpty()) {
|
||||
int[] point = queue.poll();
|
||||
int h1 = terrain[point[0]][point[1]];
|
||||
int cur = speeds[point[0]][point[1]];
|
||||
for (int i = 0; i < 4; i++) {
|
||||
int x = point[0] + px[i];
|
||||
int y = point[1] + py[i];
|
||||
if (x < 0 || x >= terrain.length || y < 0 || y >= terrain[0].length) {
|
||||
continue;
|
||||
}
|
||||
int h2 = terrain[x][y];
|
||||
int o2 = obstacle[x][y];
|
||||
int speed = cur + h1 - h2 - o2;
|
||||
if (speed <= 0 || (speeds[x][y] > 0 && speed >= speeds[x][y])) {
|
||||
continue;
|
||||
}
|
||||
if (speed == 1) {
|
||||
result.add(new int[]{x, y});
|
||||
}
|
||||
speeds[x][y] = speed;
|
||||
queue.add(new int[]{x, y});
|
||||
}
|
||||
}
|
||||
int[][] ans = new int[result.size()][2];
|
||||
for (int i = 0; i < result.size(); i++) {
|
||||
ans[i] = result.get(i);
|
||||
}
|
||||
Arrays.sort(ans, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
|
||||
return ans;
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
package com.code.leet.season.fall2021.contest3;
|
||||
|
||||
import com.code.leet.entiy.TwoArray;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @description:
|
||||
* @author: Administrator
|
||||
* @date: 2021/9/25-15:16
|
||||
*/
|
||||
public class Solution {
|
||||
public static void main(String[] args) {
|
||||
Solution solution = new Solution();
|
||||
// TwoArray edges = new TwoArray("[[0,1],[1,2]]", true);
|
||||
// TwoArray plans = new TwoArray("[[2,1],[1,0],[3,0]]", true);
|
||||
// solution.volunteerDeployment(new int[]{1, 16}, 21, edges.getArr(), plans.getArr());
|
||||
TwoArray edges = new TwoArray("[[0,3],[1,3],[4,3],[2,3],[2,5]]", true);
|
||||
TwoArray plans = new TwoArray("[[1,1],[3,3]", true);
|
||||
solution.volunteerDeployment(new int[]{4, 13, 4, 3, 8}, 54, edges.getArr(), plans.getArr());
|
||||
}
|
||||
|
||||
public int[] volunteerDeployment(int[] finalCnt, long totalNum, int[][] edges, int[][] plans) {
|
||||
long[][] result = new long[finalCnt.length + 1][2];
|
||||
result[0][1] = 1;
|
||||
for (int i = 0; i < finalCnt.length; i++) {
|
||||
result[i + 1][0] = finalCnt[i];
|
||||
}
|
||||
Map<Integer, List<Integer>> map = new HashMap<>();
|
||||
for (int[] edge : edges) {
|
||||
List<Integer> list = map.getOrDefault(edge[0], new ArrayList<>());
|
||||
list.add(edge[1]);
|
||||
map.put(edge[0], new ArrayList<>(list));
|
||||
list = map.getOrDefault(edge[1], new ArrayList<>());
|
||||
list.add(edge[0]);
|
||||
map.put(edge[1], new ArrayList<>(list));
|
||||
}
|
||||
for (int i = plans.length - 1; i >= 0; i--) {
|
||||
int index = plans[i][1];
|
||||
if (plans[i][0] == 1) {
|
||||
result[index][0] *= 2;
|
||||
result[index][1] *= 2;
|
||||
} else if (plans[i][0] == 2) {
|
||||
List<Integer> list = map.getOrDefault(index,new ArrayList<>());
|
||||
for (int num : list) {
|
||||
result[num][0] -= result[index][0];
|
||||
result[num][1] -= result[index][1];
|
||||
}
|
||||
} else {
|
||||
List<Integer> list = map.getOrDefault(index,new ArrayList<>());
|
||||
for (int num : list) {
|
||||
result[num][0] += result[index][0];
|
||||
result[num][1] += result[index][1];
|
||||
}
|
||||
}
|
||||
}
|
||||
int count = 0;
|
||||
for (long[] longs : result) {
|
||||
totalNum -= longs[0];
|
||||
count += longs[1];
|
||||
}
|
||||
int[] nums = new int[result.length];
|
||||
int num = count == 0 ? 0 : (int) (totalNum / count);
|
||||
for (int i = 0; i < result.length; i++) {
|
||||
nums[i] = (int) (result[i][0] + result[i][1] * num);
|
||||
}
|
||||
return nums;
|
||||
}
|
||||
}
|
@ -48,9 +48,14 @@ class DeleteOperationForTwoStrings {
|
||||
int[][] dp = new int[word1.length() + 1][s2.length() + 1];
|
||||
for (int i = 0; i <= word1.length(); i++) {
|
||||
for (int j = 0; j <= s2.length(); j++) {
|
||||
if (i == 0 || j == 0) continue;
|
||||
if (word1.charAt(i - 1) == s2.charAt(j - 1)) dp[i][j] = 1 + dp[i - 1][j - 1];
|
||||
else dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
|
||||
if (i == 0 || j == 0) {
|
||||
continue;
|
||||
}
|
||||
if (word1.charAt(i - 1) == s2.charAt(j - 1)) {
|
||||
dp[i][j] = 1 + dp[i - 1][j - 1];
|
||||
} else {
|
||||
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return word1.length() + s2.length() - 2 * dp[word1.length()][s2.length()];
|
||||
|
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