Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
bcf24c39ff
118
src/main/java/contest/y2022/m4/dw/Solution76.java
Normal file
118
src/main/java/contest/y2022/m4/dw/Solution76.java
Normal file
@ -0,0 +1,118 @@
|
||||
package contest.y2022.m4.dw;
|
||||
|
||||
import com.code.leet.entiy.TwoArray;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @description:
|
||||
* @author: Administrator
|
||||
* @date: 2022/4/16 22:31
|
||||
*/
|
||||
public class Solution76 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Solution76 solution = new Solution76();
|
||||
// solution.findClosestNumber(new int[]{61488, 18221, -1321, 90249, -62158, 55128, -93476, 53905, 57644, 24630, 89599, -95795, -14891, -60298, 17690, 99022, -24006, -89156, 80135, -46303, 18812, 59924, 32024, 82924, -47519, -77086, 1763, 68618, 53629, -56957, 95485, 99630, -7977, 31164, 94481, -80239, -57749, -3319, -58231, -94841, -19292, 33200, -31446, -3528, 2229, 74241, -19992, -91852, -28073, 31453, -74484, 35491, 38870, -9499, 39838, 87369, 21123, -38616, -89277, -14541, -81586, -18569, -58242, -71216, 10816, 15086, -10519, 51080, 53257, -4912, -37142, -16723, -69795, 54937, -24920, 68970, -10010, -81717, 36203, -67939, 73877, -58258, -57183, 36637, 91518, -8492, -57476, 50523, 62462, 73152, -9511, -66761, 28333, -87163, 5187});
|
||||
int[] scores = new int[]{16, 21, 22, 2, 24, 21, 12, 17, 2, 24};
|
||||
TwoArray edges = new TwoArray("[[0,1],[1,2],[2,3],[3,4],[4,5],[5,6],[6,7],[7,8],[8,9],[9,0]]", true);
|
||||
System.out.println(solution.maximumScore(scores, edges.getArr()));
|
||||
}
|
||||
|
||||
public int findClosestNumber(int[] nums) {
|
||||
Arrays.sort(nums);
|
||||
if (nums[0] >= 0) {
|
||||
return nums[0];
|
||||
}
|
||||
if (nums[nums.length - 1] <= 0) {
|
||||
return nums[nums.length - 1];
|
||||
}
|
||||
for (int i = 1; i < nums.length; i++) {
|
||||
if (nums[i] == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (nums[i] > 0 && nums[i - 1] < 0) {
|
||||
if (nums[i] <= -nums[i - 1]) {
|
||||
return nums[i];
|
||||
} else {
|
||||
return nums[i - 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public long waysToBuyPensPencils(int total, int cost1, int cost2) {
|
||||
long count = 0;
|
||||
for (int i = 0; i <= total / cost1; i++) {
|
||||
count += (total - cost1 * i) / cost2 + 1;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
class ATM {
|
||||
|
||||
long[] counts;
|
||||
long[] cost;
|
||||
|
||||
public ATM() {
|
||||
counts = new long[5];
|
||||
cost = new long[]{20, 50, 100, 200, 500};
|
||||
}
|
||||
|
||||
public void deposit(int[] banknotesCount) {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
counts[i] += banknotesCount[i];
|
||||
}
|
||||
}
|
||||
|
||||
public int[] withdraw(int amount) {
|
||||
int[] free = new int[5];
|
||||
for (int i = 4; i >= 0; i--) {
|
||||
if (amount >= cost[i]) {
|
||||
free[i] = (int) Math.min(amount / cost[i], counts[i]);
|
||||
amount -= cost[i] * free[i];
|
||||
}
|
||||
}
|
||||
if (amount > 0) {
|
||||
return new int[]{-1};
|
||||
} else {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
counts[i] -= free[i];
|
||||
}
|
||||
return free;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int maximumScore(int[] scores, int[][] edges) {
|
||||
int length = scores.length;
|
||||
List<int[]>[] lists = new ArrayList[length];
|
||||
for (int i = 0; i < length; i++) {
|
||||
lists[i] = new ArrayList<>();
|
||||
}
|
||||
for (int[] edge : edges) {
|
||||
lists[edge[0]].add(new int[]{scores[edge[1]], edge[1]});
|
||||
lists[edge[1]].add(new int[]{scores[edge[0]], edge[0]});
|
||||
}
|
||||
for (int i = 0; i < length; i++)
|
||||
if (lists[i].size() > 3) {
|
||||
Collections.sort(lists[i], (a, b) -> (b[0] - a[0]));
|
||||
lists[i] = new ArrayList<>(lists[i].subList(0, 3));
|
||||
}
|
||||
|
||||
int ans = -1;
|
||||
for (int[] edge : edges) {
|
||||
for (int[] num1 : lists[edge[0]]) {
|
||||
if(num1[1]==edge[1]){
|
||||
continue;
|
||||
}
|
||||
for (int[] num2 : lists[edge[1]]) {
|
||||
if (num2[1] != edge[0] && num1[1] != num2[1])
|
||||
ans = Math.max(ans, num1[0] + scores[edge[0]] + scores[edge[1]] + num2[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
}
|
@ -1,8 +1,6 @@
|
||||
package contest.y2022.m4;
|
||||
package contest.y2022.m4.week;
|
||||
|
||||
import javax.management.Query;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @description:
|
98
src/main/java/contest/y2022/m4/week/Solution289.java
Normal file
98
src/main/java/contest/y2022/m4/week/Solution289.java
Normal file
@ -0,0 +1,98 @@
|
||||
package contest.y2022.m4.week;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @description:
|
||||
* @author: Administrator
|
||||
* @date: 2022/4/17 10:29
|
||||
*/
|
||||
public class Solution289 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Solution289 solution = new Solution289();
|
||||
System.out.println(solution.longestPath(new int[]{-1, 0, 0, 1, 1, 2}, "abacbe"));
|
||||
}
|
||||
|
||||
public String digitSum(String s, int k) {
|
||||
while (s.length() > k) {
|
||||
StringBuilder tmp = new StringBuilder();
|
||||
int num = 0;
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
num += (s.charAt(i) - '0');
|
||||
if (i % k == k - 1) {
|
||||
tmp.append(num);
|
||||
num = 0;
|
||||
}
|
||||
}
|
||||
if (s.length() % k != 0) {
|
||||
tmp.append(num);
|
||||
}
|
||||
s = tmp.toString();
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
public int minimumRounds(int[] tasks) {
|
||||
Map<Integer, Integer> map = new HashMap<>();
|
||||
for (int task : tasks) {
|
||||
map.put(task, map.getOrDefault(task, 0) + 1);
|
||||
}
|
||||
int count = 0;
|
||||
for (int key : map.keySet()) {
|
||||
int nums = map.get(key);
|
||||
if (nums == 1) {
|
||||
return -1;
|
||||
}
|
||||
count += 0 == nums % 3 % 2 ? nums / 3 + nums % 3 / 2 : (nums / 3 - 1) + 2;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
public int longestPath(int[] parent, String s) {
|
||||
Map<Integer, List<Integer>> map = new HashMap<>();
|
||||
for (int i = 0; i < parent.length; i++) {
|
||||
map.put(i, new ArrayList<>());
|
||||
}
|
||||
for (int i = 1; i < parent.length; i++) {
|
||||
map.get(parent[i]).add(i);
|
||||
}
|
||||
maxs = new int[parent.length];
|
||||
dfs(s, 0, map);
|
||||
int max = 1;
|
||||
for (int index : map.keySet()) {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
for (int num : map.get(index)) {
|
||||
if (s.charAt(index) != s.charAt(num)) {
|
||||
list.add(maxs[num]);
|
||||
}
|
||||
}
|
||||
int size = list.size();
|
||||
int temp = 1;
|
||||
if (size > 1) {
|
||||
Collections.sort(list);
|
||||
temp += list.get(size - 1) + list.get(size - 2);
|
||||
} else if (size == 1) {
|
||||
temp += list.get(0);
|
||||
}
|
||||
max = Math.max(max, temp);
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
int[] maxs;
|
||||
|
||||
private int dfs(String s, int index, Map<Integer, List<Integer>> map) {
|
||||
int max = 1;
|
||||
for (int num : map.get(index)) {
|
||||
if (s.charAt(index) != s.charAt(num)) {
|
||||
max = Math.max(max, 1 + dfs(s, num, map));
|
||||
} else {
|
||||
dfs(s, num, map);
|
||||
}
|
||||
}
|
||||
maxs[index] = max;
|
||||
return max;
|
||||
}
|
||||
}
|
134
src/main/java/contest/y2022/season/SolutionSpring.java
Normal file
134
src/main/java/contest/y2022/season/SolutionSpring.java
Normal file
@ -0,0 +1,134 @@
|
||||
package contest.y2022.season;
|
||||
|
||||
import com.code.leet.entiy.TreeNode;
|
||||
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: 2022/4/16 15:02
|
||||
*/
|
||||
public class SolutionSpring {
|
||||
public static void main(String[] args) {
|
||||
SolutionSpring solution = new SolutionSpring();
|
||||
|
||||
// int[] materials = new int[]{3, 2, 4, 1, 2};
|
||||
// TwoArray cookbooks = new TwoArray("[[1,1,0,1,2],[2,1,4,0,0],[3,2,4,1,0]]", true);
|
||||
// TwoArray attribute = new TwoArray("[[3,2],[2,4],[7,6]]", true);
|
||||
// System.out.println(solution.perfectMenu(materials, cookbooks.getArr(), attribute.getArr(), 5));
|
||||
|
||||
//5
|
||||
// TwoArray roads = new TwoArray("[[0,1],[0,2],[1,3],[2,3],[1,2],[2,4],[2,5]]",true);
|
||||
// int[] cost = new int[]{1,2,3,4,5,6};
|
||||
// TwoArray roads = new TwoArray("[[0,2],[2,3],[3,1]]", true);
|
||||
// int[] cost = new int[]{3, 2, 1, 4};
|
||||
// System.out.println(solution.minimumCost(cost, roads.getArr()));
|
||||
}
|
||||
|
||||
// public int giveGem(int[] gem, int[][] operations) {
|
||||
// int min = Integer.MAX_VALUE;
|
||||
// int max = Integer.MIN_VALUE;
|
||||
// for (int[] operation : operations) {
|
||||
// int num = gem[operation[0]] / 2;
|
||||
// gem[operation[0]] -= num;
|
||||
// gem[operation[1]] += num;
|
||||
// }
|
||||
// for (int j : gem) {
|
||||
// min = Math.min(min, j);
|
||||
// max = Math.max(max, j);
|
||||
// }
|
||||
// return max - min;
|
||||
// }
|
||||
//
|
||||
// public int perfectMenu(int[] materials, int[][] cookbooks, int[][] attribute, int limit) {
|
||||
// dfs(materials, cookbooks, attribute, limit, 0, 0, 0);
|
||||
// return max;
|
||||
// }
|
||||
// int max = -1;
|
||||
// private void dfs(int[] materials, int[][] cookbooks, int[][] attribute, int limit, int index, int x, int y) {
|
||||
// if (index == cookbooks.length) {
|
||||
// if (y >= limit) {
|
||||
// max = Math.max(max, x);
|
||||
// }
|
||||
// return;
|
||||
// }
|
||||
// dfs(materials, cookbooks, attribute, limit, index + 1, x, y);
|
||||
// int[] newMater = materials.clone();
|
||||
// for (int i = 0; i < 5; i++) {
|
||||
// if (newMater[i] < cookbooks[index][i]) {
|
||||
// return;
|
||||
// }
|
||||
// newMater[i] -= cookbooks[index][i];
|
||||
// }
|
||||
// dfs(newMater, cookbooks, attribute, limit, index + 1, x + attribute[index][0], y + attribute[index][1]);
|
||||
// }
|
||||
|
||||
// // 第五题(错误)
|
||||
// public long minimumCost(int[] cost, int[][] roads) {
|
||||
// Map<Integer, List<Integer>> map = new HashMap<>();
|
||||
// long sum = 0;
|
||||
// for (int i = 0; i < cost.length; i++) {
|
||||
// map.put(i, new ArrayList<>());
|
||||
// sum += cost[i];
|
||||
// }
|
||||
// for (int[] road : roads) {
|
||||
// List<Integer> list = map.get(road[0]);
|
||||
// list.add(road[1]);
|
||||
// map.put(road[0], list);
|
||||
// list = map.get(road[1]);
|
||||
// list.add(road[0]);
|
||||
// map.put(road[1], list);
|
||||
// }
|
||||
// for (int i = 0; i < cost.length; i++) {
|
||||
// List<Integer> use = new ArrayList<>();
|
||||
// use.add(i);
|
||||
// Map<Integer, List<Integer>> nmap = new HashMap<>();
|
||||
// for (int key : map.keySet()) {
|
||||
// List<Integer> list = map.get(key);
|
||||
// List<Integer> nList = new ArrayList<>();
|
||||
// for (Integer integer : list) {
|
||||
// nList.add(integer);
|
||||
// }
|
||||
// nmap.put(key, nList);
|
||||
// }
|
||||
// dfs(cost, nmap, cost[i], use, i);
|
||||
// }
|
||||
// return sum - max;
|
||||
// }
|
||||
//
|
||||
// long max = 0;
|
||||
//
|
||||
// private void dfs(int[] cost, Map<Integer, List<Integer>> map, long sum, List<Integer> use, int index) {
|
||||
// Map<Integer, List<Integer>> nmap = new HashMap<>();
|
||||
// for (int key : map.keySet()) {
|
||||
// List<Integer> list = map.get(key);
|
||||
// List<Integer> nList = new ArrayList<>();
|
||||
// for (Integer integer : list) {
|
||||
// nList.add(integer);
|
||||
// }
|
||||
// nmap.put(key, nList);
|
||||
// }
|
||||
// List<Integer> list = nmap.get(index);
|
||||
// for (int i = list.size() - 1; i >= 0; i--) {
|
||||
// if (use.contains(list.get(i))) {
|
||||
// list.remove(i);
|
||||
// }
|
||||
// }
|
||||
// if (list.size() == 0) {
|
||||
// sum -= cost[use.get(0)];
|
||||
// max = Math.max(max, sum);
|
||||
// return;
|
||||
// }
|
||||
// for (int num : list) {
|
||||
// use.add(num);
|
||||
// dfs(cost, nmap, sum + cost[num], use, num);
|
||||
// use.remove((Integer) num);
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
148
src/main/java/contest/y2022/season/Team.java
Normal file
148
src/main/java/contest/y2022/season/Team.java
Normal file
@ -0,0 +1,148 @@
|
||||
package contest.y2022.season;
|
||||
|
||||
import com.code.leet.entiy.TreeNode;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
|
||||
/**
|
||||
* @description:
|
||||
* @author: Administrator
|
||||
* @date: 2022/4/23 15:01
|
||||
*/
|
||||
public class Team {
|
||||
public static void main(String[] args) {
|
||||
Team solution = new Team();
|
||||
solution.conveyorBelt(new String[]{">>v", "v^<", "<><"}, new int[]{0, 1}, new int[]{2, 0});
|
||||
}
|
||||
|
||||
// p2
|
||||
public int conveyorBelt(String[] matrix, int[] start, int[] end) {
|
||||
int size = 0;
|
||||
for (int i = 0; i < matrix.length; i++) {
|
||||
size = Math.max(size, matrix[i].length());
|
||||
}
|
||||
use = new boolean[matrix.length][size];
|
||||
int count = 0;
|
||||
if (useNum(matrix, start, end)) {
|
||||
return count;
|
||||
}
|
||||
while (!counts.isEmpty()) {
|
||||
int nums = counts.size();
|
||||
count++;
|
||||
for (int i = 0; i < nums; i++) {
|
||||
int[] tmp = counts.poll();
|
||||
int tx = tmp[0];
|
||||
int ty = tmp[1];
|
||||
for (int j = 0; j < 4; j++) {
|
||||
int x = tx + xt[j];
|
||||
int y = ty + yt[j];
|
||||
if (x == start[0] && y == start[1]) {
|
||||
return count;
|
||||
}
|
||||
if (x >= 0 && y >= 0 && x < matrix.length && y < matrix[x].length() && !use[x][y]) {
|
||||
if (useNum(matrix, start, new int[]{x, y})) {
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int[] xt = new int[]{1, -1, 0, 0};
|
||||
int[] yt = new int[]{0, 0, 1, -1};
|
||||
Queue<int[]> counts = new LinkedList<>();
|
||||
boolean[][] use;
|
||||
|
||||
private boolean useNum(String[] matrix, int[] start, int[] end) {
|
||||
Queue<int[]> queue = new LinkedList<>();
|
||||
queue.add(end);
|
||||
counts.add(end);
|
||||
use[end[0]][end[1]] = true;
|
||||
char[] changes = new char[]{'^', 'v', '<', '>'};
|
||||
while (!queue.isEmpty()) {
|
||||
int[] tmp = queue.poll();
|
||||
int tx = tmp[0];
|
||||
int ty = tmp[1];
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
int x = tx + xt[i];
|
||||
int y = ty + yt[i];
|
||||
if (x >= 0 && y >= 0 && x < matrix.length && y < matrix[x].length() && changes[i] == matrix[x].charAt(y) && !use[x][y]) {
|
||||
if (x == start[0] && y == start[1]) {
|
||||
return true;
|
||||
}
|
||||
use[x][y] = true;
|
||||
queue.add(new int[]{x, y});
|
||||
counts.add(new int[]{x, y});
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//p6
|
||||
public int getMaxLayerSum(TreeNode root) {
|
||||
int max = root.val;
|
||||
Queue<RootV> queue = new LinkedList<>();
|
||||
if (root.left != null && root.right != null) {
|
||||
queue.add(new RootV(root, false));
|
||||
} else {
|
||||
queue.add(new RootV(root, true));
|
||||
if (root.left != null) {
|
||||
max = Math.max(max, root.left.val);
|
||||
} else {
|
||||
max = Math.max(max, root.right.val);
|
||||
}
|
||||
}
|
||||
while (!queue.isEmpty()) {
|
||||
int size = queue.size();
|
||||
int score = 0;
|
||||
int add = 0;
|
||||
for (int i = 0; i < size; i++) {
|
||||
RootV rootV = queue.poll();
|
||||
TreeNode node = rootV.node;
|
||||
int tmp = node.val;
|
||||
if (null != node.left && node.right != null) {
|
||||
if (rootV.bl) {
|
||||
int sum = node.left.val + node.right.val;
|
||||
if (sum > tmp) {
|
||||
add = Math.max(add, sum - tmp);
|
||||
}
|
||||
queue.add(new RootV(node.left, true));
|
||||
queue.add(new RootV(node.right, true));
|
||||
} else {
|
||||
queue.add(new RootV(node.left, false));
|
||||
queue.add(new RootV(node.right, false));
|
||||
}
|
||||
} else if (node.left != null) {
|
||||
if (node.left.val > tmp) {
|
||||
add = Math.max(add, node.left.val - tmp);
|
||||
}
|
||||
queue.add(new RootV(node.left, true));
|
||||
} else if (node.right != null) {
|
||||
if (node.right.val > tmp) {
|
||||
add = Math.max(add, node.right.val - tmp);
|
||||
}
|
||||
queue.add(new RootV(node.right, true));
|
||||
}
|
||||
score += tmp;
|
||||
}
|
||||
max = Math.max(max, score + add);
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
class RootV {
|
||||
TreeNode node;
|
||||
boolean bl;
|
||||
|
||||
public RootV(TreeNode node, boolean bl) {
|
||||
this.node = node;
|
||||
this.bl = bl;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user