leecode,rt_phm更新更新

This commit is contained in:
kevinding1125 2023-06-12 13:02:39 +08:00
parent 242d98d0c0
commit 9de573cb99
3 changed files with 172 additions and 1 deletions

View File

@ -1,5 +1,7 @@
package com.markilue.leecode.hot100.interviewHot.union_find.second;
import org.junit.Test;
import java.util.ArrayList;
/**
@ -12,6 +14,12 @@ import java.util.ArrayList;
*/
public class LC_685_FindRedundantConnection {
@Test
public void test() {
int[][] edges = {{1, 2}, {1, 3}, {2, 3}};
System.out.println(findRedundantDirectedConnection(edges));
}
int[] father;//父节点
int n;//父节点个数
@ -84,7 +92,7 @@ public class LC_685_FindRedundantConnection {
ArrayList<Integer> twoDegree = new ArrayList<>();
//判断入度为2的节点该节点一定有子节点需要删除;反向遍历,因为后面的删除优先级更高
for (int i = edges.length-1; i >=0; i--) {
for (int i = edges.length - 1; i >= 0; i--) {
if (inDegree[edges[i][1]] == 2) {
twoDegree.add(i);//这个节点需要删除
}

View File

@ -0,0 +1,109 @@
package com.markilue.leecode.hot100.interviewHot.union_find.second;
import org.junit.Test;
import java.util.ArrayList;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.hot100.interviewHot.union_find.second
*@Author: markilue
*@CreateTime: 2023-06-12 09:58
*@Description: TODO
*@Version: 1.0
*/
public class LC_685_FindRedundantConnection1 {
@Test
public void test() {
int[][] edges = {{1, 2}, {1, 3}, {2, 3}};
System.out.println(findRedundantDirectedConnection(edges));
}
int[] father;
private void init(int[] father) {
for (int i = 0; i < father.length; i++) {
father[i] = i;
}
}
private int find(int u) {
if (father[u] == u) return u;
father[u] = find(father[u]);
return father[u];
}
private void union(int u, int v) {
u = find(u);
v = find(v);
if (u == v) return;
father[v] = u;
}
private boolean same(int u, int v) {
u = find(u);
v = find(v);
return u == v;
}
private int[] removeOne(int[][] edges) {
init(father);
for (int[] edge : edges) {
if (same(edge[0], edge[1])) {
return edge;
}
union(edge[0], edge[1]);
}
return null;
}
private boolean removeIfCan(int[][] edges, int i) {
init(father);
//遇上i就跳过
for (int i1 = 0; i1 < edges.length; i1++) {
if (i1 == i) continue;
if (same(edges[i1][0], edges[i1][1])) {
return false;
}
union(edges[i1][0], edges[i1][1]);
}
return true;
}
//虽然是有向图但是一共就三种情况两种入度为2的情况可以直接判断出来最后可以转为无向图的情况
public int[] findRedundantDirectedConnection(int[][] edges) {
father = new int[1010];
//判断入度为2的情况
int[] countDegree = new int[1010];
for (int[] edge : edges) {
countDegree[edge[1]]++;
}
ArrayList<Integer> twoDegree = new ArrayList<>();//入度为2的节点
for (int i = edges.length - 1; i >= 0; i--) {
if (countDegree[edges[i][1]] > 1) twoDegree.add(i);
}
if (!twoDegree.isEmpty()) {
if (removeIfCan(edges, twoDegree.get(0))) {
return edges[twoDegree.get(0)];
} else {
return edges[twoDegree.get(1)];
}
}
//只用删除一个
return removeOne(edges);
}
}

View File

@ -0,0 +1,54 @@
package com.markilue.leecode.interview.huawei.T0412;
import java.util.Arrays;
import java.util.Scanner;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.interview.huawei.T0412
*@Author: markilue
*@CreateTime: 2023-06-12 11:38
*@Description:
* TODO 交易系统的降级策略:
* 有一个核心交易系统接口被N个上游系统调用每个上游系统的调用量R=[R1,R2.....,RN].
* 由于核心交易系统集群故障需要暂时系统降级限制调用核心交易系统能接受的最大调用量为cnt
* 设置降级规则如下
* 如果sum(R1.R2..RN)小于等于cnt则全部可以正常调用返回-1;
* 如果sum(R1.R2....RN)大于cnt设置一个阈值limit
* 如果某个上游系统发起的调用量超过limit就将该上游系统的调用量限制为limit
* 其余未达到limit的系统可以正常发起调用
* 求出这个最大的limit (limit可以为0)
* 此题目对效率有要求请选择高效的方式
*@Version: 1.0
*/
public class Question1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] nums = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int threshold = Integer.parseInt(sc.nextLine());
solve(nums, threshold);
}
//二分寻找最大值
private static void solve(int[] nums, int threshold) {
int max = Math.min(threshold,(int) 1e5);
int min = threshold / nums.length;
while (min < max) {
int mid = min + ((max - min + 1) >> 1);
if (check(nums, mid, threshold)) min = mid;
else max = mid - 1;
}
System.out.println(min);
}
private static boolean check(int[] nums, int max, int threshold) {
int result = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] < max) result += nums[i];
else result += max;
}
return result <= threshold;
}
}