import java.util.*;
public class Soution {
//员工的重要性
class Employee {//创建一个员工的类
//员工的id
public int id;
//员工的重要性
public int importance;
//创建一个下属的集合
public List<Integer> subordinates;
}
//创建一个map集合
Map<Integer,Employee> map;
public int getImportance(List<Employee> employees, int id) {
//创建map的对象
map=new HashMap<>();
//将员工的属性增加到hashMap的键值对中
for(Employee e:employees){
map.put(e.id,e);
}
return def(id);
}
public int def(int id){
Employee employee=map.get(id);
int ans=employee.importance;
for(Integer sub:employee.subordinates){
ans+=def(sub);
}
return ans;
}
//猫狗收养问题
public ArrayList<Integer> asylum(int[][] ope) {
ArrayList<Integer> result=new ArrayList<>();//存放收养的顺序
ArrayList<Integer> cat_dog=new ArrayList<>();//收容的猫狗
//遍历ope这个二维数组
for(int i=0;i<ope.length;i++){
switch (ope[i][0]){
case 1://当第一个元素为1时,将元素增加到cat_dog
cat_dog.add(ope[i][1]);
break;
case 2://当第二个元素为2时,将元素增加到result
//判断第二个元素是否为0如果为零获取cat_dog的首元素.增加到result
if(!cat_dog.isEmpty()&&ope[i][1]==0){
result.add(cat_dog.get(0));
cat_dog.remove(0);
}
else if (ope[i][1]==1){//当第二个元素为1时,表示收养狗
for(int j=0;j<cat_dog.size();j++){
if(cat_dog.get(j)>0){
result.add(cat_dog.get(j));
cat_dog.remove(j);
break;
}
}
}
else if(ope[i][1]==-1){//当第二个元素为-1时,表示收养猫
for(int z=0;z<cat_dog.size();z++){
if(cat_dog.get(z)<0){
result.add(cat_dog.get(z));
cat_dog.remove(z);
break;
}
}
}
break;
}
}
return result;
}
//约瑟夫问题
public int getResult(int n) {
//首先创建一个数组集合,将所有的元素增加到集合中
LinkedList<Integer> result=new LinkedList<>();
int round=2;
int curr=0;
//将元素增加到集合中
for(int i=1;i<=n;i++){
result.add(i);
}
//循环遍历当result的长多大于1
while(result.size()>1){
int i=0;
while(result.size()>1&&i<result.size()){
curr=(curr+1)%round;
if(curr!=1){
result.remove(i);
}
else{
i++;
}
}
round++;
curr=0;
//将尾部元素移除,之后增加到集合首部;
if(result.size()>1){
int last=result.removeLast();
result.addFirst(last);
}
}
return result.pop();
}
}
import java.util.*;
//最近的请求次数
public class RecentCounter {
Queue<Integer> queue;
public RecentCounter() {
queue = new LinkedList<>();
}
public int ping(int t) {
queue.add(t);
while(queue.peek()<t-3000){
queue.poll();
}
return queue.size();
}
//法官小镇
public int findJudge(int N, int[][] trust) {
List<Integer> A=new LinkedList<>();
List<Integer> B=new LinkedList<>();
for(int i=0;i<N;i++){
A.add(trust[i][0]);
B.add(trust[i][1]);
}
for(int j=0;j<A.size();j++){
for(int i=0;i<B.size();i++){
if(A.get(j)==B.get(i)){
B.remove(i);
}
}
}
if(B.isEmpty()){
return -1;
}
int i=0;
for( i=1;i<B.size();i++) {
if (B.get(0) != B.get(i)) {
return -1;
}
}
return B.get(i);
}
//将数组分为和相等的三部分
// public boolean canThreePartsEqualSum(int[] A) {
// int num=0;
// for(int i=0;i<A.length;i++){
// num=num+A[i];
// }
// if(num%3!=0){
// return false;
// }
// Stack<Integer> stack=new Stack<>();
// for(int i=0;i<A.length;i++){
// stack.push(A[i]);
// }
// int a=0,b=0,c=0;
// if(!stack.isEmpty()) {
// if (a != num / 3) {
// a += stack.pop();
// }
// }
// if(!stack.isEmpty()) {
// if (b != num / 3) {
// b += stack.pop();
// }
// }
// if(!stack.isEmpty()) {
// if (c != num / 3) {
// c += stack.pop();
// }
// }
// return a==b&&b==c;
// }
public boolean canThreePartsEqualSum(int[] A){
int sum=0;
for(int i=0;i<A.length;i++){
sum=sum+A[i];
}
if(sum%3!=0){
return false;
}
int left = 0;
int leftSum = A[left];
int right = A.length - 1;
int rightSum = A[right];
while(left + 1 < right){
if(leftSum == sum/3 && rightSum == sum/3){
// 左右两边都等于 sum/3 ,中间也一定等于
return true;
}
if(leftSum != sum/3){
// left = 0赋予了初值,应该先left++,在leftSum += A[left];
leftSum += A[++left];
}
if(rightSum != sum/3){
// right = A.length - 1 赋予了初值,应该先right--,在rightSum += A[right];
rightSum += A[--right];
}
}
return false;
}
}