Skip to content

Commit 5d55908

Browse files
committed
Added tasks 3370-3373
1 parent 66da5bf commit 5d55908

File tree

12 files changed

+554
-0
lines changed

12 files changed

+554
-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 #Math #Bit_Manipulation #2024_12_03_Time_0_ms_(100.00%)_Space_41.1_MB_(45.50%)
4+
5+
class Solution {
6+
fun smallestNumber(n: Int): Int {
7+
var 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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g3301_3400.s3371_identify_the_largest_outlier_in_an_array
2+
3+
// #Medium #Array #Hash_Table #Counting #Enumeration
4+
// #2024_12_03_Time_5_ms_(100.00%)_Space_60.6_MB_(33.40%)
5+
6+
class Solution {
7+
fun getLargestOutlier(nums: IntArray): Int {
8+
val cnt = IntArray(2001)
9+
var sum = 0
10+
for (i in nums) {
11+
sum += i
12+
cnt[i + 1000]++
13+
}
14+
for (i in cnt.indices.reversed()) {
15+
val j = i - 1000
16+
if (cnt[i] == 0) {
17+
continue
18+
}
19+
sum -= j
20+
val csum = (sum shr 1) + 1000
21+
cnt[i]--
22+
if (sum % 2 == 0 && csum >= 0 && csum < cnt.size && cnt[csum] > 0) {
23+
return j
24+
}
25+
sum += j
26+
cnt[i]++
27+
}
28+
return 0
29+
}
30+
}
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,97 @@
1+
package g3301_3400.s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i
2+
3+
// #Medium #Tree #Depth_First_Search #Breadth_First_Search
4+
// #2024_12_03_Time_50_ms_(99.49%)_Space_75.7_MB_(5.10%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
private fun getGraph(edges: Array<IntArray>): Array<ArrayList<Int?>?> {
10+
val n = edges.size + 1
11+
val graph: Array<ArrayList<Int?>?> = arrayOfNulls<ArrayList<Int?>?>(n)
12+
for (i in 0..<n) {
13+
graph[i] = ArrayList<Int?>()
14+
}
15+
for (edge in edges) {
16+
val u = edge[0]
17+
val v = edge[1]
18+
graph[u]!!.add(v)
19+
graph[v]!!.add(u)
20+
}
21+
return graph
22+
}
23+
24+
private fun dfs(graph: Array<ArrayList<Int?>?>, u: Int, pt: Int, dp: Array<IntArray?>, k: Int) {
25+
for (v in graph[u]!!) {
26+
if (v == pt) {
27+
continue
28+
}
29+
dfs(graph, v!!, u, dp, k)
30+
for (i in 0..<k) {
31+
dp[u]!![i + 1] += dp[v]!![i]
32+
}
33+
}
34+
dp[u]!![0] = dp[u]!![0] + 1
35+
}
36+
37+
private fun dfs2(
38+
graph: Array<ArrayList<Int?>?>,
39+
u: Int,
40+
pt: Int,
41+
ptv: IntArray,
42+
fdp: Array<IntArray?>,
43+
dp: Array<IntArray?>,
44+
k: Int,
45+
) {
46+
fdp[u]!![0] = dp[u]!![0]
47+
for (i in 1..k) {
48+
fdp[u]!![i] = (dp[u]!![i] + ptv[i - 1])
49+
}
50+
for (v in graph[u]!!) {
51+
if (v == pt) {
52+
continue
53+
}
54+
val nptv = IntArray(k + 1)
55+
for (i in 0..<k) {
56+
nptv[i + 1] = dp[u]!![i + 1] - dp[v!!]!![i] + ptv[i]
57+
}
58+
nptv[0] = 1
59+
dfs2(graph, v!!, u, nptv, fdp, dp, k)
60+
}
61+
}
62+
63+
private fun get(edges: Array<IntArray>, k: Int): Array<IntArray?> {
64+
val graph = getGraph(edges)
65+
val n = graph.size
66+
val dp = Array<IntArray?>(n) { IntArray(k + 1) }
67+
val fdp = Array<IntArray?>(n) { IntArray(k + 1) }
68+
dfs(graph, 0, -1, dp, k)
69+
dfs2(graph, 0, -1, IntArray(k + 1), fdp, dp, k)
70+
for (i in 0..<n) {
71+
for (j in 1..k) {
72+
fdp[i]!![j] += fdp[i]!![j - 1]
73+
}
74+
}
75+
return fdp
76+
}
77+
78+
fun maxTargetNodes(edges1: Array<IntArray>, edges2: Array<IntArray>, k: Int): IntArray {
79+
val a = get(edges1, k)
80+
val b = get(edges2, k)
81+
val n = a.size
82+
val m = b.size
83+
val ans = IntArray(n)
84+
var max = 0
85+
run {
86+
var i = 0
87+
while (k != 0 && i < m) {
88+
max = max(max.toDouble(), b[i]!![k - 1].toDouble()).toInt()
89+
i++
90+
}
91+
}
92+
for (i in 0..<n) {
93+
ans[i] = a[i]!![k] + max
94+
}
95+
return ans
96+
}
97+
}
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,72 @@
1+
package g3301_3400.s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii
2+
3+
// #Hard #Tree #Depth_First_Search #Breadth_First_Search
4+
// #2024_12_03_Time_26_ms_(98.75%)_Space_114.7_MB_(80.00%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
fun maxTargetNodes(edges1: Array<IntArray>, edges2: Array<IntArray>): IntArray {
10+
val n = edges1.size + 1
11+
val g1 = packU(n, edges1)
12+
val m = edges2.size + 1
13+
val g2 = packU(m, edges2)
14+
val p2 = parents(g2)
15+
val eo2 = IntArray(2)
16+
for (i in 0..<m) {
17+
eo2[p2[2]!![i] % 2]++
18+
}
19+
val max = max(eo2[0], eo2[1])
20+
val p1 = parents(g1)
21+
val eo1 = IntArray(2)
22+
for (i in 0..<n) {
23+
eo1[p1[2]!![i] % 2]++
24+
}
25+
val ans = IntArray(n)
26+
for (i in 0..<n) {
27+
ans[i] = eo1[p1[2]!![i] % 2] + max
28+
}
29+
return ans
30+
}
31+
32+
private fun parents(g: Array<IntArray?>): Array<IntArray?> {
33+
val n = g.size
34+
val par = IntArray(n)
35+
par.fill(-1)
36+
val depth = IntArray(n)
37+
depth[0] = 0
38+
val q = IntArray(n)
39+
q[0] = 0
40+
var p = 0
41+
var r = 1
42+
while (p < r) {
43+
val cur = q[p]
44+
for (nex in g[cur]!!) {
45+
if (par[cur] != nex) {
46+
q[r++] = nex
47+
par[nex] = cur
48+
depth[nex] = depth[cur] + 1
49+
}
50+
}
51+
p++
52+
}
53+
return arrayOf<IntArray?>(par, q, depth)
54+
}
55+
56+
private fun packU(n: Int, ft: Array<IntArray>): Array<IntArray?> {
57+
val g = arrayOfNulls<IntArray>(n)
58+
val p = IntArray(n)
59+
for (u in ft) {
60+
p[u[0]]++
61+
p[u[1]]++
62+
}
63+
for (i in 0..<n) {
64+
g[i] = IntArray(p[i])
65+
}
66+
for (u in ft) {
67+
g[u[0]]!![--p[u[0]]] = u[1]
68+
g[u[1]]!![--p[u[1]]] = u[0]
69+
}
70+
return g
71+
}
72+
}

0 commit comments

Comments
 (0)