Skip to content

Added tasks 3065-3069 #1715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package g3001_3100.s3065_minimum_operations_to_exceed_threshold_value_i;

// #Easy #Array #2024_03_31_Time_0_ms_(100.00%)_Space_42.7_MB_(48.42%)

public class Solution {
public int minOperations(int[] nums, int k) {
int count = 0;
for (int num : nums) {
if (num >= k) {
count++;
}
}
return nums.length - count;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
3065\. Minimum Operations to Exceed Threshold Value I

Easy

You are given a **0-indexed** integer array `nums`, and an integer `k`.

In one operation, you can remove one occurrence of the smallest element of `nums`.

Return _the **minimum** number of operations needed so that all elements of the array are greater than or equal to_ `k`.

**Example 1:**

**Input:** nums = [2,11,10,1,3], k = 10

**Output:** 3

**Explanation:** After one operation, nums becomes equal to [2, 11, 10, 3].

After two operations, nums becomes equal to [11, 10, 3].

After three operations, nums becomes equal to [11, 10].

At this stage, all the elements of nums are greater than or equal to 10 so we can stop.

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.

**Example 2:**

**Input:** nums = [1,1,2,4,9], k = 1

**Output:** 0

**Explanation:** All elements of the array are greater than or equal to 1 so we do not need to apply any operations on nums.

**Example 3:**

**Input:** nums = [1,1,2,4,9], k = 9

**Output:** 4

**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.

**Constraints:**

* `1 <= nums.length <= 50`
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
* <code>1 <= k <= 10<sup>9</sup></code>
* The input is generated such that there is at least one index `i` such that `nums[i] >= k`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package g3001_3100.s3066_minimum_operations_to_exceed_threshold_value_ii;

// #Medium #Array #Heap_Priority_Queue #Simulation
// #2024_03_31_Time_26_ms_(99.91%)_Space_65.7_MB_(97.28%)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution {
public int minOperations(int[] nums, int k) {
int n = nums.length;
int steps = 0;
Arrays.sort(nums);
List<Integer> extra = new ArrayList<>();
int i = 0;
int j = 0;
while ((i < n && nums[i] < k) || (j < extra.size() && extra.get(j) < k)) {
int min;
int max;
if (i < n && (j >= extra.size() || extra.get(j) > nums[i])) {
min = nums[i++];
} else {
min = extra.get(j++);
}
if (i < n && (j >= extra.size() || extra.get(j) > nums[i])) {
max = nums[i++];
} else {
max = extra.get(j++);
}
steps++;
long res = min;
res = 2 * res + max;
if (res > Integer.MAX_VALUE) {
extra.add(Integer.MAX_VALUE);
} else {
extra.add((int) res);
}
}
return steps;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
3066\. Minimum Operations to Exceed Threshold Value II

Medium

You are given a **0-indexed** integer array `nums`, and an integer `k`.

In one operation, you will:

* Take the two smallest integers `x` and `y` in `nums`.
* Remove `x` and `y` from `nums`.
* Add `min(x, y) * 2 + max(x, y)` anywhere in the array.

**Note** that you can only apply the described operation if `nums` contains at least two elements.

Return _the **minimum** number of operations needed so that all elements of the array are greater than or equal to_ `k`.

**Example 1:**

**Input:** nums = [2,11,10,1,3], k = 10

**Output:** 2

**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].

In the second operation, we remove elements 3 and 4, then add 3 \* 2 + 4 to nums. nums becomes equal to [10, 11, 10].

At this stage, all the elements of nums are greater than or equal to 10 so we can stop.

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.

**Example 2:**

**Input:** nums = [1,1,2,4,9], k = 20

**Output:** 4

**Explanation:** After one operation, nums becomes equal to [2, 4, 9, 3].

After two operations, nums becomes equal to [7, 4, 9].

After three operations, nums becomes equal to [15, 9].

After four operations, nums becomes equal to [33].

At this stage, all the elements of nums are greater than 20 so we can stop.

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.

**Constraints:**

* <code>2 <= nums.length <= 2 * 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
* <code>1 <= k <= 10<sup>9</sup></code>
* 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 number Diff line number Diff line change
@@ -0,0 +1,56 @@
package g3001_3100.s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network;

// #Medium #Array #Tree #Depth_First_Search #2024_03_31_Time_69_ms_(99.83%)_Space_45.5_MB_(81.49%)

import java.util.ArrayList;

@SuppressWarnings("unchecked")
public class Solution {
private ArrayList<Integer>[] adj;

public int[] countPairsOfConnectableServers(int[][] edges, int signalSpeed) {
int n = edges.length + 1;
adj = new ArrayList[n];
for (int i = 0; i < n; i++) {
adj[i] = new ArrayList<>();
}
for (int[] edge : edges) {
int u = edge[0];
int v = edge[1];
int w = edge[2];
adj[u].add(v);
adj[v].add(u);
adj[u].add(w);
adj[v].add(w);
}
int[] res = new int[n];
for (int i = 0; i < n; i++) {
if (adj[i].size() > 2) {
ArrayList<Integer> al = new ArrayList<>();
for (int j = 0; j < adj[i].size(); j += 2) {
int[] cnt = new int[1];
dfs(adj[i].get(j), i, adj[i].get(j + 1), cnt, signalSpeed);
al.add(cnt[0]);
}
int sum = 0;
for (int j : al) {
res[i] += (sum * j);
sum += j;
}
}
}
return res;
}

void dfs(int node, int par, int sum, int[] cnt, int ss) {
if (sum % ss == 0) {
cnt[0]++;
}
for (int i = 0; i < adj[node].size(); i += 2) {
int child = adj[node].get(i);
if (child != par) {
dfs(child, node, sum + adj[node].get(i + 1), cnt, ss);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
3067\. Count Pairs of Connectable Servers in a Weighted Tree Network

Medium

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`.

Two servers `a` and `b` are **connectable** through a server `c` if:

* `a < b`, `a != c` and `b != c`.
* The distance from `c` to `a` is divisible by `signalSpeed`.
* The distance from `c` to `b` is divisible by `signalSpeed`.
* The path from `c` to `b` and the path from `c` to `a` do not share any edges.

Return _an integer array_ `count` _of length_ `n` _where_ `count[i]` _is the **number** of server pairs that are **connectable** through_ _the server_ `i`.

**Example 1:**

![](https://assets.leetcode.com/uploads/2024/01/21/example22.png)

**Input:** edges = [[0,1,1],[1,2,5],[2,3,13],[3,4,9],[4,5,2]], signalSpeed = 1

**Output:** [0,4,6,6,4,0]

**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.

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.

**Example 2:**

![](https://assets.leetcode.com/uploads/2024/01/21/example11.png)

**Input:** edges = [[0,6,3],[6,5,3],[0,3,1],[3,2,7],[3,1,6],[3,4,2]], signalSpeed = 3

**Output:** [2,0,0,0,0,0,2]

**Explanation:** Through server 0, there are 2 pairs of connectable servers: (4, 5) and (4, 6).

Through server 6, there are 2 pairs of connectable servers: (4, 5) and (0, 5).

It can be shown that no two servers are connectable through servers other than 0 and 6.

**Constraints:**

* `2 <= n <= 1000`
* `edges.length == n - 1`
* `edges[i].length == 3`
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code>
* <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code>
* <code>1 <= weight<sub>i</sub> <= 10<sup>6</sup></code>
* <code>1 <= signalSpeed <= 10<sup>6</sup></code>
* The input is generated such that `edges` represents a valid tree.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package g3001_3100.s3068_find_the_maximum_sum_of_node_values;

// #Hard #Array #Dynamic_Programming #Sorting #Greedy #Tree #Bit_Manipulation
// #2024_03_31_Time_1_ms_(100.00%)_Space_54.5_MB_(67.07%)

@SuppressWarnings("java:S1172")
public class Solution {
public long maximumValueSum(int[] nums, int k, int[][] edges) {
long res = 0;
int d = 1 << 30;
int c = 0;
for (int a : nums) {
int b = a ^ k;
res += Math.max(a, b);
c ^= a < b ? 1 : 0;
d = Math.min(d, Math.abs(a - b));
}
return res - d * c;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
3068\. Find the Maximum Sum of Node Values

Hard

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`.

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:

* Choose any edge `[u, v]` connecting the nodes `u` and `v`, and update their values as follows:
* `nums[u] = nums[u] XOR k`
* `nums[v] = nums[v] XOR k`

Return _the **maximum** possible **sum** of the **values** Alice can achieve by performing the operation **any** number of times_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png)

**Input:** nums = [1,2,1], k = 3, edges = [[0,1],[0,2]]

**Output:** 6

**Explanation:** Alice can achieve the maximum sum of 6 using a single operation:

- 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].

The total sum of values is 2 + 2 + 2 = 6.

It can be shown that 6 is the maximum achievable sum of values.

**Example 2:**

![](https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png)

**Input:** nums = [2,3], k = 7, edges = [[0,1]]

**Output:** 9

**Explanation:** Alice can achieve the maximum sum of 9 using a single operation:

- 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].

The total sum of values is 5 + 4 = 9.

It can be shown that 9 is the maximum achievable sum of values.

**Example 3:**

![](https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png)

**Input:** nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]]

**Output:** 42

**Explanation:** The maximum achievable sum is 42 which can be achieved by Alice performing no operations.

**Constraints:**

* <code>2 <= n == nums.length <= 2 * 10<sup>4</sup></code>
* <code>1 <= k <= 10<sup>9</sup></code>
* <code>0 <= nums[i] <= 10<sup>9</sup></code>
* `edges.length == n - 1`
* `edges[i].length == 2`
* `0 <= edges[i][0], edges[i][1] <= n - 1`
* The input is generated such that `edges` represent a valid tree.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package g3001_3100.s3069_distribute_elements_into_two_arrays_i;

// #Easy #Array #Simulation #2024_03_31_Time_0_ms_(100.00%)_Space_44.6_MB_(70.15%)

public class Solution {
public int[] resultArray(int[] nums) {
int s = 0;
int t = 1;
for (int i = 2; i < nums.length; i++) {
int p = i;
if (nums[s] > nums[t]) {
for (int q = s + 1; q < i; q++) {
int temp = nums[p];
nums[p] = nums[p - 1];
nums[p - 1] = temp;
p = p - 1;
}
s++;
t++;
} else {
for (int q = t + 1; q < i; q++) {
int temp = nums[p];
nums[p] = nums[p - 1];
nums[p - 1] = temp;
p = p - 1;
}
t++;
}
}
return nums;
}
}
Loading
Loading