5127. 数组的相对排序
给你两个数组,arr1 和 arr2,
- arr2 中的元素各不相同
- arr2 中的每个元素都出现在 arr1 中
对 arr1 中的元素进行排序,使 arr1 中项的相对顺序和 arr2 中的相对顺序相同。未在 arr2 中出现过的元素需要按照升序放在 arr1 的末尾。
示例:
输入:arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]
输出:[2,2,2,1,4,3,3,9,6,7,19]
提示:
- arr1.length, arr2.length <= 1000
- 0 <= arr1[i], arr2[i] <= 1000
- arr2 中的元素 arr2[i] 各不相同
- arr2 中的每个元素 arr2[i] 都出现在 arr1 中
代码:
public static int[] relativeSortArray(int[] arr1, int[] arr2) {
int []res=new int[arr1.length];
Map<Integer,Integer> map=new HashMap<>();
Set<Integer> set=new HashSet<>();
List<Integer> list=new ArrayList<>();
//遍历arr2,将元素放在set里面
for (int t:arr2){
set.add(t);
}
//遍历arr1,将每个元素放入map,如果set里面没有出现过的,那么就放入list
for (int t:arr1){
if(set.contains(t)){
if (!map.containsKey(t)){
map.put(t,1);
}else {
map.put(t,map.get(t)+1);
}
}else {
list.add(t);
}
}
int count=0;
//将map里面的数按arr2的顺序依次放入res里面
for (int key:arr2){
while (map.get(key)>0){
res[count++]=key;
map.put(key,map.get(key)-1);
}
}
int start=arr1.length-list.size();
//排序
Collections.sort(list);
//将list里面(arr2里面没有出现的数按升序放入剩余的res数组里面)
int cnt=0;
for (int i=start;i<arr1.length;i++){
res[i]=list.get(cnt);
cnt++;
}
return res;
}
5128. 最深叶节点的最近公共祖先
给你一个有根节点的二叉树,找到它最深的叶节点的最近公共祖先。
回想一下:
- 叶节点 是二叉树中没有子节点的节点
- 树的根节点的 深度 为 0,如果某一节点的深度为 d,那它的子节点的深度就是 d+1
- 如果我们假定 A 是一组节点 S 的 最近公共祖先,S 中的每个节点都在以 A 为根节点的子树中,且 A 的深度达到此条件下可能的最大值。
示例 1:
输入:root = [1,2,3]
输出:[1,2,3]
示例 2:
输入:root = [1,2,3,4]
输出:[4]
示例 3:
输入:root = [1,2,3,4,5]
输出:[2,4,5]
思路:
1.先找到树的最大深度
2.将该树的等于最大深度的叶子节点放到列表里面
3.对该列表里面的所有节点依次两两一起求最近公共祖先
public static ArrayList<TreeNode> listLeaf=new ArrayList<>();
public static int minLength=0;
public static TreeNode lcaDeepestLeaves(TreeNode root) {
minLength=maxDepth(root);
dfs(root,minLength);
if(listLeaf.size()==1){
return listLeaf.get(0);
}
if(listLeaf.size()==2){
TreeNode now=lowestCommonAncestor(root,listLeaf.get(0),listLeaf.get(1));
return now;
}
//大于2的时候
TreeNode l1=listLeaf.get(0);
TreeNode l2=listLeaf.get(1);
TreeNode now=lowestCommonAncestor(root,listLeaf.get(0),listLeaf.get(1));
for(int i=2;i<listLeaf.size();i++){
now=lowestCommonAncestor(root,now,listLeaf.get(i));
}
return now;
}
//最深叶节点的最近公共祖先
public static TreeNode lowestCommonAncestor(TreeNode root, TreeNode p,TreeNode q) {
if(root == null || root == p || root == q) return root;
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
if(left == null && right == null)
return null;
else if(left != null && right != null) return root;
//其中一个为null,返回不为null的那一个
else {
return left == null ? right : left;
}
}
//返回最深叶子节点
public static void dfs(TreeNode root,int length){
if (root==null) return;
length--;
dfs(root.left,length);
if (root.left==null&&root.right==null&&length==0){
//System.out.println("root:"+root.val);
listLeaf.add(root);
}
dfs(root.right,length);
}
//返回树的最大深度
public static int maxDepth(TreeNode root) {
if(root==null) return 0;
return Math.max(maxDepth(root.left)+1,maxDepth(root.right)+1);
}
5129. 表现良好的最长时间段
给你一份工作时间表 hours,上面记录着某一位员工每天的工作小时数。
我们认为当员工一天中的工作小时数大于 8 小时的时候,那么这一天就是「劳累的一天」。
所谓「表现良好的时间段」,意味在这段时间内,「劳累的天数」是严格 大于「不劳累的天数」。
请你返回「表现良好时间段」的最大长度。
示例 1:
输入:hours = [9,9,6,0,6,6,9]
输出:3
解释:最长的表现良好时间段是 [9,9,6]。
提示:
1 <= hours.length <= 10000
0 <= hours[i] <= 16
代码:
public static int longestWPI(int[] hours) {
int n = hours.length;
int[] array = new int[n];
for(int i = 0; i < n; i++){
if(hours[i] > 8)
array[i] = 1;
else
array[i] = -1;
}
int ans = 0;
for(int i = 0; i < n; i++){
int temp = 0;
for(int j = i; j < n; j++){
temp += array[j];
if(temp > 0)
ans = Math.max(ans, j-i+1);
}
}
return ans;
}
5130. 最小的必要团队
作为项目经理,你规划了一份需求的技能清单 req_skills,并打算从备选人员名单 people 中选出些人组成一个「必要团队」( 编号为 i 的备选人员 people[i] 含有一份该备选人员掌握的技能列表)。
所谓「必要团队」,就是在这个团队中,对于所需求的技能列表 req_skills 中列出的每项技能,团队中至少有一名成员已经掌握。
我们可以用每个人的编号来表示团队中的成员:例如,团队 team = [0, 1, 3] 表示掌握技能分别为 people[0],people[1],和 people[3] 的备选人员。
请你返回 任一 规模最小的必要团队,团队成员用人员编号表示。你可以按任意顺序返回答案,本题保证答案存在。
原题地址:https://leetcode-cn.com/contest/weekly-contest-145/problems/smallest-sufficient-team/
示例 1:
输入:req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
输出:[0,2]
答案引用自:luchy0120 https://leetcode-cn.com/contest/weekly-contest-145/ranking
public int[] smallestSufficientTeam(String[] sk, List<List<String>> p) {
int id = 0;
Map<String,Integer> mp = new HashMap<>();
for(String s:sk){
mp.put(s,id++);
}
int v[] =new int[1<<id];
Arrays.fill(v,Integer.MAX_VALUE/2);
v[0] = 0;
List nm[] =new List[1<<id];
for(int st = (1<<id)-1;st>=0;--st){
nm[st] = new ArrayList();
}
int pp = 0;
int g = Integer.MAX_VALUE;
int gst = -1;
for(List<String> c:p){
for(int st = (1<<id)-1;st>=0;--st){
int cur = 0;
for(String nn:c){
if(mp.containsKey(nn)){
int cc = mp.get(nn);
cur |= 1<<cc;
}
}
int nst = st|cur;
if(v[st]+1<v[nst]){
v[nst] = v[st]+1;
nm[nst] = new ArrayList();
for(Object xx:nm[st]){
nm[nst].add(xx);
}
nm[nst].add(pp);
}
}
++pp;
}
int sz =nm[(1<<id)-1].size();
int a[] =new int[sz];
for(int i=0;i<sz;++i){
a[i] = (int)nm[(1<<id)-1].get(i);
}
return a;
}