leecode更新
This commit is contained in:
parent
38f94367ee
commit
cb04a53dbc
|
|
@ -0,0 +1,81 @@
|
||||||
|
package meituan;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*@BelongsProject: Leecode
|
||||||
|
*@BelongsPackage: meituan
|
||||||
|
*@Author: markilue
|
||||||
|
*@CreateTime: 2023-03-18 10:02
|
||||||
|
*@Description: TODO
|
||||||
|
*@Version: 1.0
|
||||||
|
*/
|
||||||
|
public class Question1 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
String[] s = sc.nextLine().split(" ");
|
||||||
|
int num = Integer.parseInt(s[0]);
|
||||||
|
int a = Integer.parseInt(s[1]);
|
||||||
|
int b = Integer.parseInt(s[2]);
|
||||||
|
|
||||||
|
List<int[]> result = new ArrayList<>();
|
||||||
|
while (num-- > 0) {
|
||||||
|
String[] s1 = sc.nextLine().split(" ");
|
||||||
|
result.add(new int[]{Integer.parseInt(s1[0]), Integer.parseInt(s1[1])});
|
||||||
|
}
|
||||||
|
|
||||||
|
new Question1().sovle(a, b, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
|
||||||
|
int[] l1 = {1, 1};
|
||||||
|
int[] l2 = {1, 2};
|
||||||
|
int[] l3 = {1, 3};
|
||||||
|
List<int[]> result = new ArrayList<>(Arrays.asList(l1, l2, l3));
|
||||||
|
sovle(1,1,result);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//暂时会超时(可以考虑是否要把以前的减掉,在加上现在的)
|
||||||
|
public void sovle(int maxX, int maxY, List<int[]> list) {
|
||||||
|
|
||||||
|
int maxResult = 0;
|
||||||
|
int cur = 0;
|
||||||
|
|
||||||
|
int[][] count = new int[1000][1000];
|
||||||
|
int xMin = Integer.MAX_VALUE;
|
||||||
|
int xMax = 0;
|
||||||
|
int yMin = Integer.MAX_VALUE;
|
||||||
|
int yMax = 0;
|
||||||
|
|
||||||
|
for (int[] ints : list) {
|
||||||
|
count[ints[0]][ints[1]]++;
|
||||||
|
if (xMin > ints[0]) xMin = ints[0];
|
||||||
|
if (xMax < ints[0]) xMax = ints[0];
|
||||||
|
if (yMin > ints[1]) yMin = ints[1];
|
||||||
|
if (yMax < ints[1]) yMax = ints[1];
|
||||||
|
}//记录每个位置的个数
|
||||||
|
|
||||||
|
for (int i = xMin; i <= xMax; i++) {
|
||||||
|
for (int j = yMin; j <= yMax; j++) {
|
||||||
|
//计算这个位置范围内的数量
|
||||||
|
cur = 0;
|
||||||
|
for (int k = 0; k <= maxX; k++) {
|
||||||
|
for (int l = 0; l <= maxY; l++) {
|
||||||
|
cur += count[i + k][j + l];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (maxResult < cur) maxResult = cur;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println(maxResult);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,81 @@
|
||||||
|
package meituan;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*@BelongsProject: Leecode
|
||||||
|
*@BelongsPackage: meituan
|
||||||
|
*@Author: markilue
|
||||||
|
*@CreateTime: 2023-03-18 10:02
|
||||||
|
*@Description: TODO
|
||||||
|
*@Version: 1.0
|
||||||
|
*/
|
||||||
|
public class Question2 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
String[] s = sc.nextLine().split(" ");
|
||||||
|
|
||||||
|
int n = Integer.parseInt(s[0]);//总长度
|
||||||
|
int k = Integer.parseInt(s[1]);//总色彩数
|
||||||
|
|
||||||
|
String[] s1 = sc.nextLine().split(" ");
|
||||||
|
|
||||||
|
int[] nums = Arrays.stream(s1).mapToInt(Integer::parseInt).toArray();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
|
||||||
|
int k=3;
|
||||||
|
int[] nums={1 ,2 ,3 ,2 ,1 ,4, 5, 1};
|
||||||
|
sovle(k,nums);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//滑动窗口?
|
||||||
|
public void sovle(int k, int[] nums) {
|
||||||
|
//先构造一个窗口
|
||||||
|
int left = 0;
|
||||||
|
|
||||||
|
int right = 0;
|
||||||
|
|
||||||
|
int result=0;
|
||||||
|
HashMap<Integer, Integer> map = new HashMap<>();//<nums,count>
|
||||||
|
|
||||||
|
while (right < nums.length) {
|
||||||
|
while (right < nums.length&&map.size() <= k) {
|
||||||
|
|
||||||
|
if (!map.isEmpty() && map.containsKey(nums[right])) {
|
||||||
|
Integer count = map.getOrDefault(nums[right], 0);
|
||||||
|
map.put(nums[right], count+1);
|
||||||
|
} else {
|
||||||
|
map.put(nums[right], 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
right++;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (left<=right&&map.size() > k) {
|
||||||
|
//缩小窗口
|
||||||
|
if(right-left-1>result){
|
||||||
|
result=right-left-1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!map.isEmpty() && map.containsKey(nums[left])) {
|
||||||
|
Integer count = map.getOrDefault(nums[left], 0);
|
||||||
|
map.put(nums[left], count-1);
|
||||||
|
if(count==1)map.remove(nums[left]);
|
||||||
|
}
|
||||||
|
left++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(result);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,93 @@
|
||||||
|
package meituan;
|
||||||
|
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.omg.CORBA.PUBLIC_MEMBER;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*@BelongsProject: Leecode
|
||||||
|
*@BelongsPackage: meituan
|
||||||
|
*@Author: markilue
|
||||||
|
*@CreateTime: 2023-03-18 10:58
|
||||||
|
*@Description: TODO
|
||||||
|
*@Version: 1.0
|
||||||
|
*/
|
||||||
|
public class Question3 {
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
String s = sc.nextLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
String s = "acca";
|
||||||
|
sovle(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//首先需要让s是回文
|
||||||
|
public void sovle(String s) {
|
||||||
|
char[] chars = s.toCharArray();
|
||||||
|
|
||||||
|
int count = isTrue(chars);
|
||||||
|
int length = chars.length;
|
||||||
|
|
||||||
|
//判断改了几次
|
||||||
|
if (count == 0) {
|
||||||
|
//没改过
|
||||||
|
if (length % 2 == 0) {
|
||||||
|
change(chars);
|
||||||
|
} else {
|
||||||
|
chars[length / 2] = 'a';
|
||||||
|
}
|
||||||
|
} else if (count == 1) {
|
||||||
|
//最多只能该中间那个
|
||||||
|
if (length % 2 == 1) {
|
||||||
|
chars[length / 2] = 'a';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(new String(chars));
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public int isTrue(char[] chars) {
|
||||||
|
int left = 0;
|
||||||
|
int right = chars.length - 1;
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
while (left < right) {
|
||||||
|
if (chars[left] != chars[right]) {
|
||||||
|
//不等就判断那个更小
|
||||||
|
if (chars[left] < chars[right]) chars[right] = chars[left];
|
||||||
|
else chars[left] = chars[right];
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
left++;
|
||||||
|
right--;
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void change(char[] chars) {
|
||||||
|
int left = 0;
|
||||||
|
int right = chars.length-1;
|
||||||
|
|
||||||
|
while (left < chars.length) {
|
||||||
|
//找到第一个不为a的改为a
|
||||||
|
if (chars[left] != 'a') {
|
||||||
|
chars[left] = 'a';
|
||||||
|
chars[right] = 'a';
|
||||||
|
}
|
||||||
|
left++;
|
||||||
|
right--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,51 @@
|
||||||
|
package meituan;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*@BelongsProject: Leecode
|
||||||
|
*@BelongsPackage: meituan
|
||||||
|
*@Author: markilue
|
||||||
|
*@CreateTime: 2023-03-18 11:20
|
||||||
|
*@Description: TODO
|
||||||
|
*@Version: 1.0
|
||||||
|
*/
|
||||||
|
public class Question4 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
String[] s = sc.nextLine().split(" ");
|
||||||
|
|
||||||
|
int x = Integer.parseInt(s[1]);
|
||||||
|
int n = Integer.parseInt(s[0]);
|
||||||
|
int y = Integer.parseInt(s[2]);
|
||||||
|
int[][] prices = new int[n][];
|
||||||
|
int index = 0;
|
||||||
|
while (n-- > 0) {
|
||||||
|
String[] l1 = sc.nextLine().split(" ");
|
||||||
|
prices[index++] = Arrays.stream(l1).mapToInt(Integer::parseInt).toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 动态规划:
|
||||||
|
* TODO DP五部曲:
|
||||||
|
* 1.dp定义: dp[i][j][k]表示花费不超过i时前j件商品使用了k个折扣卷最多购买数量
|
||||||
|
* 2.dp状态转移方程:
|
||||||
|
* 1.dp[i][j][k] 用折扣卷买,不用折扣卷买,不买
|
||||||
|
*
|
||||||
|
* 3.dp初始化:
|
||||||
|
* @param X
|
||||||
|
* @param Y
|
||||||
|
* @param prices
|
||||||
|
*/
|
||||||
|
public void sovle(int X, int Y, int[][] prices) {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,84 @@
|
||||||
|
package meituan;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*@BelongsProject: Leecode
|
||||||
|
*@BelongsPackage: meituan
|
||||||
|
*@Author: markilue
|
||||||
|
*@CreateTime: 2023-03-18 11:20
|
||||||
|
*@Description: TODO
|
||||||
|
*@Version: 1.0
|
||||||
|
*/
|
||||||
|
public class Question5 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
|
|
||||||
|
int n = sc.nextInt();
|
||||||
|
|
||||||
|
int[] energies = new int[n];
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
energies[i] = sc.nextInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
TreeNode[] nodes = new TreeNode[n];
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
nodes[i] = new TreeNode(i, energies[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < n - 1; i++) {
|
||||||
|
int u = sc.nextInt() - 1;
|
||||||
|
int v = sc.nextInt() - 1;
|
||||||
|
nodes[u].childList.add(nodes[v]);
|
||||||
|
nodes[v].childList.add(nodes[u]);
|
||||||
|
}
|
||||||
|
|
||||||
|
dfs(nodes[0], null, 0);
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
System.out.print(nodes[i].energy + " ");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void dfs(TreeNode node, TreeNode parent, int dist) {
|
||||||
|
|
||||||
|
for (TreeNode child : node.childList) {
|
||||||
|
if (child != parent) {
|
||||||
|
dfs(child, node, dist + 1);
|
||||||
|
node.energy += child.energy;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (dist <= node.energy) {
|
||||||
|
node.energy = dist;
|
||||||
|
} else {
|
||||||
|
node.energy = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class TreeNode {
|
||||||
|
|
||||||
|
int value;
|
||||||
|
int energy;
|
||||||
|
List<TreeNode> childList = new ArrayList<TreeNode>();
|
||||||
|
TreeNode father;
|
||||||
|
|
||||||
|
public TreeNode() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public TreeNode(int value, int energy) {
|
||||||
|
this.value = value;
|
||||||
|
this.energy = energy;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue