Skip to content

Commit 5da17ba

Browse files
committed
Added tasks 3065-3069
1 parent 8dcb3fd commit 5da17ba

File tree

15 files changed

+554
-0
lines changed

15 files changed

+554
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package g3001_3100.s3065_minimum_operations_to_exceed_threshold_value_i;
2+
3+
// #Easy #Array #2024_03_31_Time_0_ms_(100.00%)_Space_42.7_MB_(48.42%)
4+
5+
public class Solution {
6+
public int minOperations(int[] nums, int k) {
7+
int count = 0;
8+
for (int num : nums) {
9+
if (num >= k) {
10+
count++;
11+
}
12+
}
13+
return nums.length - count;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
3065\. Minimum Operations to Exceed Threshold Value I
2+
3+
Easy
4+
5+
You are given a **0-indexed** integer array `nums`, and an integer `k`.
6+
7+
In one operation, you can remove one occurrence of the smallest element of `nums`.
8+
9+
Return _the **minimum** number of operations needed so that all elements of the array are greater than or equal to_ `k`.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [2,11,10,1,3], k = 10
14+
15+
**Output:** 3
16+
17+
**Explanation:** After one operation, nums becomes equal to [2, 11, 10, 3].
18+
19+
After two operations, nums becomes equal to [11, 10, 3].
20+
21+
After three operations, nums becomes equal to [11, 10].
22+
23+
At this stage, all the elements of nums are greater than or equal to 10 so we can stop.
24+
25+
It can be shown that 3 is the minimum number of operations needed so that all elements of the array are greater than or equal to 10.
26+
27+
**Example 2:**
28+
29+
**Input:** nums = [1,1,2,4,9], k = 1
30+
31+
**Output:** 0
32+
33+
**Explanation:** All elements of the array are greater than or equal to 1 so we do not need to apply any operations on nums.
34+
35+
**Example 3:**
36+
37+
**Input:** nums = [1,1,2,4,9], k = 9
38+
39+
**Output:** 4
40+
41+
**Explanation:** only a single element of nums is greater than or equal to 9 so we need to apply the operations 4 times on nums.
42+
43+
**Constraints:**
44+
45+
* `1 <= nums.length <= 50`
46+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
47+
* <code>1 <= k <= 10<sup>9</sup></code>
48+
* The input is generated such that there is at least one index `i` such that `nums[i] >= k`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package g3001_3100.s3066_minimum_operations_to_exceed_threshold_value_ii;
2+
3+
// #Medium #Array #Heap_Priority_Queue #Simulation
4+
// #2024_03_31_Time_26_ms_(99.91%)_Space_65.7_MB_(97.28%)
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
public class Solution {
11+
public int minOperations(int[] nums, int k) {
12+
int n = nums.length;
13+
int steps = 0;
14+
Arrays.sort(nums);
15+
List<Integer> extra = new ArrayList<>();
16+
boolean increase = true;
17+
int i = 0;
18+
int j = 0;
19+
while (increase) {
20+
if ((i >= n || nums[i] >= k) && (j >= extra.size() || extra.get(j) >= k)) {
21+
increase = false;
22+
break;
23+
}
24+
int min = -1, max = -1;
25+
if (i < n && (j >= extra.size() || extra.get(j) > nums[i])) {
26+
min = nums[i++];
27+
} else {
28+
min = extra.get(j++);
29+
}
30+
if (i < n && (j >= extra.size() || extra.get(j) > nums[i])) {
31+
max = nums[i++];
32+
} else {
33+
max = extra.get(j++);
34+
}
35+
steps++;
36+
long res = min;
37+
res = 2 * res + max;
38+
if (res > Integer.MAX_VALUE) extra.add(Integer.MAX_VALUE);
39+
else extra.add((int) res);
40+
}
41+
return steps;
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
3066\. Minimum Operations to Exceed Threshold Value II
2+
3+
Medium
4+
5+
You are given a **0-indexed** integer array `nums`, and an integer `k`.
6+
7+
In one operation, you will:
8+
9+
* Take the two smallest integers `x` and `y` in `nums`.
10+
* Remove `x` and `y` from `nums`.
11+
* Add `min(x, y) * 2 + max(x, y)` anywhere in the array.
12+
13+
**Note** that you can only apply the described operation if `nums` contains at least two elements.
14+
15+
Return _the **minimum** number of operations needed so that all elements of the array are greater than or equal to_ `k`.
16+
17+
**Example 1:**
18+
19+
**Input:** nums = [2,11,10,1,3], k = 10
20+
21+
**Output:** 2
22+
23+
**Explanation:** In the first operation, we remove elements 1 and 2, then add 1 \* 2 + 2 to nums. nums becomes equal to [4, 11, 10, 3].
24+
25+
In the second operation, we remove elements 3 and 4, then add 3 \* 2 + 4 to nums. nums becomes equal to [10, 11, 10].
26+
27+
At this stage, all the elements of nums are greater than or equal to 10 so we can stop.
28+
29+
It can be shown that 2 is the minimum number of operations needed so that all elements of the array are greater than or equal to 10.
30+
31+
**Example 2:**
32+
33+
**Input:** nums = [1,1,2,4,9], k = 20
34+
35+
**Output:** 4
36+
37+
**Explanation:** After one operation, nums becomes equal to [2, 4, 9, 3].
38+
39+
After two operations, nums becomes equal to [7, 4, 9].
40+
41+
After three operations, nums becomes equal to [15, 9].
42+
43+
After four operations, nums becomes equal to [33].
44+
45+
At this stage, all the elements of nums are greater than 20 so we can stop.
46+
47+
It can be shown that 4 is the minimum number of operations needed so that all elements of the array are greater than or equal to 20.
48+
49+
**Constraints:**
50+
51+
* <code>2 <= nums.length <= 2 * 10<sup>5</sup></code>
52+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
53+
* <code>1 <= k <= 10<sup>9</sup></code>
54+
* The input is generated such that an answer always exists. That is, there exists some sequence of operations after which all elements of the array are greater than or equal to `k`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package g3001_3100.s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network;
2+
3+
// #Medium #Array #Tree #Depth_First_Search #2024_03_31_Time_69_ms_(99.83%)_Space_45.5_MB_(81.49%)
4+
5+
import java.util.ArrayList;
6+
7+
public class Solution {
8+
private ArrayList<Integer> adj[];
9+
10+
public int[] countPairsOfConnectableServers(int[][] edges, int signalSpeed) {
11+
int n = edges.length + 1;
12+
adj = new ArrayList[n];
13+
for (int i = 0; i < n; i++) {
14+
adj[i] = new ArrayList<>();
15+
}
16+
for (int edge[] : edges) {
17+
int u = edge[0], v = edge[1], w = edge[2];
18+
adj[u].add(v);
19+
adj[v].add(u);
20+
adj[u].add(w);
21+
adj[v].add(w);
22+
}
23+
int res[] = new int[n];
24+
for (int i = 0; i < n; i++) {
25+
if (adj[i].size() > 2) {
26+
ArrayList<Integer> al = new ArrayList<>();
27+
for (int j = 0; j < adj[i].size(); j += 2) {
28+
int cnt[] = new int[1];
29+
dfs(adj[i].get(j), i, adj[i].get(j + 1), cnt, signalSpeed);
30+
al.add(cnt[0]);
31+
}
32+
int sum = 0;
33+
for (int j : al) {
34+
res[i] += (sum * j);
35+
sum += j;
36+
}
37+
}
38+
}
39+
return res;
40+
}
41+
42+
void dfs(int node, int par, int sum, int cnt[], int ss) {
43+
if (sum % ss == 0) {
44+
cnt[0]++;
45+
}
46+
for (int i = 0; i < adj[node].size(); i += 2) {
47+
int child = adj[node].get(i);
48+
if (child != par) {
49+
dfs(child, node, sum + adj[node].get(i + 1), cnt, ss);
50+
}
51+
}
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
3067\. Count Pairs of Connectable Servers in a Weighted Tree Network
2+
3+
Medium
4+
5+
You are given an unrooted weighted tree with `n` vertices representing servers numbered from `0` to `n - 1`, an array `edges` where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional edge between vertices <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> of weight <code>weight<sub>i</sub></code>. You are also given an integer `signalSpeed`.
6+
7+
Two servers `a` and `b` are **connectable** through a server `c` if:
8+
9+
* `a < b`, `a != c` and `b != c`.
10+
* The distance from `c` to `a` is divisible by `signalSpeed`.
11+
* The distance from `c` to `b` is divisible by `signalSpeed`.
12+
* The path from `c` to `b` and the path from `c` to `a` do not share any edges.
13+
14+
Return _an integer array_ `count` _of length_ `n` _where_ `count[i]` _is the **number** of server pairs that are **connectable** through_ _the server_ `i`.
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2024/01/21/example22.png)
19+
20+
**Input:** edges = [[0,1,1],[1,2,5],[2,3,13],[3,4,9],[4,5,2]], signalSpeed = 1
21+
22+
**Output:** [0,4,6,6,4,0]
23+
24+
**Explanation:** Since signalSpeed is 1, count[c] is equal to the number of pairs of paths that start at c and do not share any edges.
25+
26+
In the case of the given path graph, count[c] is equal to the number of servers to the left of c multiplied by the servers to the right of c.
27+
28+
**Example 2:**
29+
30+
![](https://assets.leetcode.com/uploads/2024/01/21/example11.png)
31+
32+
**Input:** edges = [[0,6,3],[6,5,3],[0,3,1],[3,2,7],[3,1,6],[3,4,2]], signalSpeed = 3
33+
34+
**Output:** [2,0,0,0,0,0,2]
35+
36+
**Explanation:** Through server 0, there are 2 pairs of connectable servers: (4, 5) and (4, 6).
37+
38+
Through server 6, there are 2 pairs of connectable servers: (4, 5) and (0, 5).
39+
40+
It can be shown that no two servers are connectable through servers other than 0 and 6.
41+
42+
**Constraints:**
43+
44+
* `2 <= n <= 1000`
45+
* `edges.length == n - 1`
46+
* `edges[i].length == 3`
47+
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code>
48+
* <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code>
49+
* <code>1 <= weight<sub>i</sub> <= 10<sup>6</sup></code>
50+
* <code>1 <= signalSpeed <= 10<sup>6</sup></code>
51+
* The input is generated such that `edges` represents a valid tree.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3001_3100.s3068_find_the_maximum_sum_of_node_values;
2+
3+
// #Hard #Array #Dynamic_Programming #Sorting #Greedy #Tree #Bit_Manipulation
4+
// #2024_03_31_Time_1_ms_(100.00%)_Space_54.5_MB_(67.07%)
5+
6+
public class Solution {
7+
public long maximumValueSum(int[] A, int k, int[][] edges) {
8+
long res = 0;
9+
int d = 1 << 30, c = 0;
10+
for (int a : A) {
11+
int b = a ^ k;
12+
res += Math.max(a, b);
13+
c ^= a < b ? 1 : 0;
14+
d = Math.min(d, Math.abs(a - b));
15+
}
16+
return res - d * c;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
3068\. Find the Maximum Sum of Node Values
2+
3+
Hard
4+
5+
There exists an **undirected** tree with `n` nodes numbered `0` to `n - 1`. You are given a **0-indexed** 2D integer array `edges` of length `n - 1`, where <code>edges[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 tree. You are also given a **positive** integer `k`, and a **0-indexed** array of **non-negative** integers `nums` of length `n`, where `nums[i]` represents the **value** of the node numbered `i`.
6+
7+
Alice wants the sum of values of tree nodes to be **maximum**, for which Alice can perform the following operation **any** number of times (**including zero**) on the tree:
8+
9+
* Choose any edge `[u, v]` connecting the nodes `u` and `v`, and update their values as follows:
10+
* `nums[u] = nums[u] XOR k`
11+
* `nums[v] = nums[v] XOR k`
12+
13+
Return _the **maximum** possible **sum** of the **values** Alice can achieve by performing the operation **any** number of times_.
14+
15+
**Example 1:**
16+
17+
![](https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png)
18+
19+
**Input:** nums = [1,2,1], k = 3, edges = [[0,1],[0,2]]
20+
21+
**Output:** 6
22+
23+
**Explanation:** Alice can achieve the maximum sum of 6 using a single operation:
24+
25+
- Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -> [2,2,2].
26+
27+
The total sum of values is 2 + 2 + 2 = 6.
28+
29+
It can be shown that 6 is the maximum achievable sum of values.
30+
31+
**Example 2:**
32+
33+
![](https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png)
34+
35+
**Input:** nums = [2,3], k = 7, edges = [[0,1]]
36+
37+
**Output:** 9
38+
39+
**Explanation:** Alice can achieve the maximum sum of 9 using a single operation:
40+
41+
- Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -> [5,4].
42+
43+
The total sum of values is 5 + 4 = 9.
44+
45+
It can be shown that 9 is the maximum achievable sum of values.
46+
47+
**Example 3:**
48+
49+
![](https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png)
50+
51+
**Input:** nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]]
52+
53+
**Output:** 42
54+
55+
**Explanation:** The maximum achievable sum is 42 which can be achieved by Alice performing no operations.
56+
57+
**Constraints:**
58+
59+
* <code>2 <= n == nums.length <= 2 * 10<sup>4</sup></code>
60+
* <code>1 <= k <= 10<sup>9</sup></code>
61+
* <code>0 <= nums[i] <= 10<sup>9</sup></code>
62+
* `edges.length == n - 1`
63+
* `edges[i].length == 2`
64+
* `0 <= edges[i][0], edges[i][1] <= n - 1`
65+
* The input is generated such that `edges` represent a valid tree.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g3001_3100.s3069_distribute_elements_into_two_arrays_i;
2+
3+
// #Easy #Array #Simulation #2024_03_31_Time_0_ms_(100.00%)_Space_44.6_MB_(70.15%)
4+
5+
public class Solution {
6+
public int[] resultArray(int[] nums) {
7+
int s = 0, t = 1;
8+
for (int i = 2; i < nums.length; i++) {
9+
int p = i;
10+
if (nums[s] > nums[t]) {
11+
for (int q = s + 1; q < i; q++) {
12+
int temp = nums[p];
13+
nums[p] = nums[p - 1];
14+
nums[p - 1] = temp;
15+
p = p - 1;
16+
}
17+
s++;
18+
t++;
19+
} else {
20+
for (int q = t + 1; q < i; q++) {
21+
int temp = nums[p];
22+
nums[p] = nums[p - 1];
23+
nums[p - 1] = temp;
24+
p = p - 1;
25+
}
26+
t++;
27+
}
28+
}
29+
return nums;
30+
}
31+
}

0 commit comments

Comments
 (0)