LeetCode 第426场周赛
As my job requires it, I am studing English now. Therefore, I will write the solutions to these problems in English.
3370. Smallest Number With All Set BitsEasy | 100501. 仅含置位位的最小整数
enumerate $ 2^i-1 $
1 | class Solution { |
3371. Identify the Largest Outlier in an ArrayMed. | 100444. 识别数组中的最大异常值
Traverse the array nums
in descending order and evaluta each element as a potenial answers. if $ \frac{sum(nums) - nums[i]}{2} $ exist in the array nums
(excluding nums[i]), then nums[i] is the answer.
We need a map to count the number of nums[i]. when we visit nums[i], we should delete nums[i] in the map.
note: if $ sum(nums) - nums[i] $ is odd, we should skip this case.
1 | class Solution { |
3372. Maximize the Number of Target Nodes After Connecting Trees IMed. | 100475. 连接两棵树后最大目标节点数目 I
Given $ n,m \leq 1000 $, we can use DFS to compute the number of nodes whose distance from node i
is less than or equal to $ k $.
For any node in Tree1, we can connect it to a node in Tree2. For nodes in Tree2, we can find the one node with the maximum count of nodes `is less than or equal to $ k-1 $.
Reason for $ k-1 $, the node in Tree1 and the node in Tree2 requrie one additional edge to be connected.
1 | class Solution { |
3373. Maximize the Number of Target Nodes After Connecting Trees IIHard | 100485. 连接两棵树后最大目标节点数目 II
In a list, if the distance between two index is even, both indexs either be odd or even.
In a tree, if the distance between two nodes is even, both nodes must beling to the same layer(odd or even).
For Tree1, we count the number of nodes in odd and even layers using DFS.
When connecting Tree1 to Tree2:
- If we connect to a node in an odd layer, the result will include nodes in the even layer.
- If we connect to a node in an even layer, the result will include nodes in the odd layer.
1 | class Solution { |