Skip to content

Commit 4fd65cb

Browse files
committed
Added tasks 3370-3373
1 parent 7f09298 commit 4fd65cb

File tree

12 files changed

+504
-0
lines changed

12 files changed

+504
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package g3301_3400.s3370_smallest_number_with_all_set_bits;
2+
3+
// #Easy #2024_12_01_Time_0_ms_(100.00%)_Space_40.7_MB_(100.00%)
4+
5+
public class Solution {
6+
public int smallestNumber(int n) {
7+
int res = 1;
8+
while (res < n) {
9+
res = res * 2 + 1;
10+
}
11+
return res;
12+
}
13+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
3370\. Smallest Number With All Set Bits
2+
3+
Easy
4+
5+
You are given a _positive_ number `n`.
6+
7+
Return the **smallest** number `x` **greater than** or **equal to** `n`, such that the binary representation of `x` contains only **set** bits.
8+
9+
A **set** bit refers to a bit in the binary representation of a number that has a value of `1`.
10+
11+
**Example 1:**
12+
13+
**Input:** n = 5
14+
15+
**Output:** 7
16+
17+
**Explanation:**
18+
19+
The binary representation of 7 is `"111"`.
20+
21+
**Example 2:**
22+
23+
**Input:** n = 10
24+
25+
**Output:** 15
26+
27+
**Explanation:**
28+
29+
The binary representation of 15 is `"1111"`.
30+
31+
**Example 3:**
32+
33+
**Input:** n = 3
34+
35+
**Output:** 3
36+
37+
**Explanation:**
38+
39+
The binary representation of 3 is `"11"`.
40+
41+
**Constraints:**
42+
43+
* `1 <= n <= 1000`
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g3301_3400.s3371_identify_the_largest_outlier_in_an_array;
2+
3+
// #Medium #2024_12_01_Time_633_ms_(100.00%)_Space_64.5_MB_(100.00%)
4+
5+
import java.util.TreeMap;
6+
7+
public class Solution {
8+
public int getLargestOutlier(int[] nums) {
9+
int sum = 0;
10+
for (int num : nums) {
11+
sum += num;
12+
}
13+
TreeMap<Integer, Integer> frequencyMap = new TreeMap<>();
14+
for (int num : nums) {
15+
frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
16+
}
17+
int ans = Integer.MIN_VALUE;
18+
for (int num : nums) {
19+
if ((sum - num) % 2 == 0) {
20+
int target = (sum - num) / 2;
21+
frequencyMap.put(num, frequencyMap.get(num) - 1);
22+
if (frequencyMap.get(num) == 0) {
23+
frequencyMap.remove(num);
24+
}
25+
if (frequencyMap.containsKey(target)) {
26+
ans = Math.max(ans, num);
27+
}
28+
frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
29+
}
30+
}
31+
return ans == Integer.MIN_VALUE ? -1 : ans;
32+
}
33+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
3371\. Identify the Largest Outlier in an Array
2+
3+
Medium
4+
5+
You are given an integer array `nums`. This array contains `n` elements, where **exactly** `n - 2` elements are **special** **numbers**. One of the remaining **two** elements is the _sum_ of these **special numbers**, and the other is an **outlier**.
6+
7+
An **outlier** is defined as a number that is _neither_ one of the original special numbers _nor_ the element representing the sum of those numbers.
8+
9+
**Note** that special numbers, the sum element, and the outlier must have **distinct** indices, but _may_ share the **same** value.
10+
11+
Return the **largest** potential **outlier** in `nums`.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [2,3,5,10]
16+
17+
**Output:** 10
18+
19+
**Explanation:**
20+
21+
The special numbers could be 2 and 3, thus making their sum 5 and the outlier 10.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [-2,-1,-3,-6,4]
26+
27+
**Output:** 4
28+
29+
**Explanation:**
30+
31+
The special numbers could be -2, -1, and -3, thus making their sum -6 and the outlier 4.
32+
33+
**Example 3:**
34+
35+
**Input:** nums = [1,1,1,1,1,5,5]
36+
37+
**Output:** 5
38+
39+
**Explanation:**
40+
41+
The special numbers could be 1, 1, 1, 1, and 1, thus making their sum 5 and the other 5 as the outlier.
42+
43+
**Constraints:**
44+
45+
* <code>3 <= nums.length <= 10<sup>5</sup></code>
46+
* `-1000 <= nums[i] <= 1000`
47+
* The input is generated such that at least **one** potential outlier exists in `nums`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package g3301_3400.s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i;
2+
3+
// #Medium #2024_12_01_Time_828_ms_(100.00%)_Space_47.8_MB_(100.00%)
4+
5+
import java.util.ArrayList;
6+
import java.util.HashSet;
7+
import java.util.LinkedList;
8+
import java.util.Queue;
9+
10+
public class Solution {
11+
private void bfs(ArrayList<ArrayList<Integer>> tree, int node, int[] arr, int k) {
12+
Queue<Integer> q = new LinkedList<>();
13+
HashSet<Integer> visited = new HashSet<>();
14+
q.add(node);
15+
int count = 0;
16+
while (!q.isEmpty() && count <= k) {
17+
int size = q.size();
18+
for (int i = 0; i < size; i++) {
19+
int temp = q.poll();
20+
visited.add(temp);
21+
for (int x : tree.get(temp)) {
22+
if (!visited.contains(x)) {
23+
q.offer(x);
24+
}
25+
}
26+
}
27+
count++;
28+
}
29+
arr[node] = visited.size();
30+
}
31+
32+
public int[] maxTargetNodes(int[][] edges1, int[][] edges2, int k) {
33+
int n = edges1.length + 1;
34+
int m = edges2.length + 1;
35+
ArrayList<ArrayList<Integer>> tree1 = new ArrayList<>();
36+
ArrayList<ArrayList<Integer>> tree2 = new ArrayList<>();
37+
for (int i = 0; i < n; i++) {
38+
tree1.add(new ArrayList<>());
39+
}
40+
for (int i = 0; i < m; i++) {
41+
tree2.add(new ArrayList<>());
42+
}
43+
for (int[] e : edges1) {
44+
tree1.get(e[0]).add(e[1]);
45+
tree1.get(e[1]).add(e[0]);
46+
}
47+
for (int[] e : edges2) {
48+
tree2.get(e[0]).add(e[1]);
49+
tree2.get(e[1]).add(e[0]);
50+
}
51+
int[] tar1 = new int[n];
52+
int[] tar2 = new int[m];
53+
for (int i = 0; i < m; i++) {
54+
bfs(tree2, i, tar2, k - 1);
55+
}
56+
int max = 0;
57+
for (int i : tar2) {
58+
max = Math.max(i, max);
59+
}
60+
for (int i = 0; i < n; i++) {
61+
bfs(tree1, i, tar1, k);
62+
}
63+
for (int i = 0; i < n; i++) {
64+
tar1[i] += max;
65+
}
66+
return tar1;
67+
}
68+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
3372\. Maximize the Number of Target Nodes After Connecting Trees I
2+
3+
Medium
4+
5+
There exist two **undirected** trees with `n` and `m` nodes, with **distinct** labels in ranges `[0, n - 1]` and `[0, m - 1]`, respectively.
6+
7+
You are given two 2D integer arrays `edges1` and `edges2` of lengths `n - 1` and `m - 1`, respectively, where <code>edges1[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the first tree and <code>edges2[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the second tree. You are also given an integer `k`.
8+
9+
Node `u` is **target** to node `v` if the number of edges on the path from `u` to `v` is less than or equal to `k`. **Note** that a node is _always_ **target** to itself.
10+
11+
Return an array of `n` integers `answer`, where `answer[i]` is the **maximum** possible number of nodes **target** to node `i` of the first tree if you have to connect one node from the first tree to another node in the second tree.
12+
13+
**Note** that queries are independent from each other. That is, for every query you will remove the added edge before proceeding to the next query.
14+
15+
**Example 1:**
16+
17+
**Input:** edges1 = [[0,1],[0,2],[2,3],[2,4]], edges2 = [[0,1],[0,2],[0,3],[2,7],[1,4],[4,5],[4,6]], k = 2
18+
19+
**Output:** [9,7,9,8,8]
20+
21+
**Explanation:**
22+
23+
* For `i = 0`, connect node 0 from the first tree to node 0 from the second tree.
24+
* For `i = 1`, connect node 1 from the first tree to node 0 from the second tree.
25+
* For `i = 2`, connect node 2 from the first tree to node 4 from the second tree.
26+
* For `i = 3`, connect node 3 from the first tree to node 4 from the second tree.
27+
* For `i = 4`, connect node 4 from the first tree to node 4 from the second tree.
28+
29+
![](https://assets.leetcode.com/uploads/2024/09/24/3982-1.png)
30+
31+
**Example 2:**
32+
33+
**Input:** edges1 = [[0,1],[0,2],[0,3],[0,4]], edges2 = [[0,1],[1,2],[2,3]], k = 1
34+
35+
**Output:** [6,3,3,3,3]
36+
37+
**Explanation:**
38+
39+
For every `i`, connect node `i` of the first tree with any node of the second tree.
40+
41+
![](https://assets.leetcode.com/uploads/2024/09/24/3928-2.png)
42+
43+
**Constraints:**
44+
45+
* `2 <= n, m <= 1000`
46+
* `edges1.length == n - 1`
47+
* `edges2.length == m - 1`
48+
* `edges1[i].length == edges2[i].length == 2`
49+
* <code>edges1[i] = [a<sub>i</sub>, b<sub>i</sub>]</code>
50+
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code>
51+
* <code>edges2[i] = [u<sub>i</sub>, v<sub>i</sub>]</code>
52+
* <code>0 <= u<sub>i</sub>, v<sub>i</sub> < m</code>
53+
* The input is generated such that `edges1` and `edges2` represent valid trees.
54+
* `0 <= k <= 1000`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package g3301_3400.s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii;
2+
3+
// #Hard #2024_12_01_Time_447_ms_(100.00%)_Space_173.2_MB_(100.00%)
4+
5+
import java.util.HashMap;
6+
import java.util.HashSet;
7+
8+
public class Solution {
9+
private int dfs(int a, HashMap<Integer, HashSet<Integer>> adj, boolean[] mark, int i) {
10+
int res = 0;
11+
mark[a] = true;
12+
if (i % 2 == 0) {
13+
res++;
14+
}
15+
for (int j : adj.get(a)) {
16+
if (!mark[j]) {
17+
res += dfs(j, adj, mark, i + 1);
18+
}
19+
}
20+
return res;
21+
}
22+
23+
private void sol(
24+
int a,
25+
HashMap<Integer, HashSet<Integer>> adj,
26+
boolean[] mark,
27+
int i,
28+
int t,
29+
int n,
30+
int[] res) {
31+
mark[a] = true;
32+
if (i % 2 == 0) {
33+
res[a] = t;
34+
} else {
35+
res[a] = n - t;
36+
}
37+
for (int j : adj.get(a)) {
38+
if (!mark[j]) {
39+
sol(j, adj, mark, i + 1, t, n, res);
40+
}
41+
}
42+
}
43+
44+
public int[] maxTargetNodes(int[][] edges1, int[][] edges2) {
45+
int n = edges1.length + 1;
46+
int m = edges2.length + 1;
47+
HashMap<Integer, HashSet<Integer>> adj1 = new HashMap<>();
48+
HashMap<Integer, HashSet<Integer>> adj2 = new HashMap<>();
49+
for (int[] i : edges1) {
50+
if (adj1.get(i[0]) == null) {
51+
adj1.put(i[0], new HashSet<>());
52+
}
53+
adj1.get(i[0]).add(i[1]);
54+
if (adj1.get(i[1]) == null) {
55+
adj1.put(i[1], new HashSet<>());
56+
}
57+
adj1.get(i[1]).add(i[0]);
58+
}
59+
for (int[] i : edges2) {
60+
if (adj2.get(i[0]) == null) {
61+
adj2.put(i[0], new HashSet<>());
62+
}
63+
adj2.get(i[0]).add(i[1]);
64+
if (adj2.get(i[1]) == null) {
65+
adj2.put(i[1], new HashSet<>());
66+
}
67+
adj2.get(i[1]).add(i[0]);
68+
}
69+
boolean[] mark1 = new boolean[n];
70+
boolean[] mark2 = new boolean[m];
71+
int a = dfs(0, adj2, mark2, 0);
72+
int b = dfs(0, adj1, mark1, 0);
73+
mark1 = new boolean[n];
74+
int[] res = new int[n];
75+
sol(0, adj1, mark1, 0, b, n, res);
76+
for (int i = 0; i < res.length; i++) {
77+
if (res[i] != n) {
78+
res[i] += Math.max(a, m - a);
79+
} else {
80+
res[i] += a;
81+
}
82+
}
83+
return res;
84+
}
85+
}

0 commit comments

Comments
 (0)