Skip to content

Commit

Permalink
Added tasks 2968-2973
Browse files Browse the repository at this point in the history
  • Loading branch information
ThanhNIT authored Jan 16, 2024
1 parent ff2f7ea commit fc3f7e3
Show file tree
Hide file tree
Showing 15 changed files with 558 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package g2901_3000.s2968_apply_operations_to_maximize_frequency_score;

// #Hard #Array #Sorting #Binary_Search #Prefix_Sum #Sliding_Window
// #2024_01_16_Time_27_ms_(78.37%)_Space_55.8_MB_(53.47%)

import java.util.Arrays;

public class Solution {
public int maxFrequencyScore(int[] nums, long k) {
Arrays.sort(nums);
int n = nums.length;
long[] prefixSum = new long[n + 1];
for (int i = 0; i < n; i++) {
prefixSum[i + 1] = prefixSum[i] + nums[i];
}
int start = 0;
int end = 1;
int out = 1;
while (end < n) {
end++;
int mid = (start + end) / 2;
long target = nums[mid];
long cost =
(target * (mid - start) - (prefixSum[mid] - prefixSum[start]))
+ (prefixSum[end] - prefixSum[mid] - target * (end - mid));
while (start < end && cost > k) {
start++;
mid = (start + end) / 2;
target = nums[mid];
cost =
(target * (mid - start) - (prefixSum[mid] - prefixSum[start]))
+ (prefixSum[end] - prefixSum[mid] - target * (end - mid));
}
out = Math.max(out, end - start);
}
return out;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
2968\. Apply Operations to Maximize Frequency Score

Hard

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

You can perform the following operation on the array **at most** `k` times:

* Choose any index `i` from the array and **increase** or **decrease** `nums[i]` by `1`.

The score of the final array is the **frequency** of the most frequent element in the array.

Return _the **maximum** score you can achieve_.

The frequency of an element is the number of occurences of that element in the array.

**Example 1:**

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

**Output:** 3

**Explanation:** We can do the following operations on the array:
- Choose i = 0, and increase the value of nums[0] by 1. The resulting array is [2,2,6,4].
- Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,3].
- Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,2].

The element 2 is the most frequent in the final array so our score is 3. It can be shown that we cannot achieve a better score.

**Example 2:**

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

**Output:** 3

**Explanation:** We cannot apply any operations so our score will be the frequency of the most frequent element in the original array, which is 3.

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
* <code>0 <= k <= 10<sup>14</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package g2901_3000.s2970_count_the_number_of_incremovable_subarrays_i;

// #Easy #Array #Binary_Search #Two_Pointers #Enumeration
// #2024_01_16_Time_0_ms_(100.00%)_Space_42.9_MB_(87.73%)

public class Solution {
public int incremovableSubarrayCount(int[] nums) {
int n = nums.length;
int res = 0;
int left = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
int right = Integer.MAX_VALUE;
for (int j = n - 1; i <= j; j--) {
res++;
if (left >= nums[j] || nums[j] >= right) {
break;
}
right = nums[j];
}
if (left >= nums[i]) {
break;
}
left = nums[i];
}
return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
2970\. Count the Number of Incremovable Subarrays I

Easy

You are given a **0-indexed** array of **positive** integers `nums`.

A subarray of `nums` is called **incremovable** if `nums` becomes **strictly increasing** on removing the subarray. For example, the subarray `[3, 4]` is an incremovable subarray of `[5, 3, 4, 6, 7]` because removing this subarray changes the array `[5, 3, 4, 6, 7]` to `[5, 6, 7]` which is strictly increasing.

Return _the total number of **incremovable** subarrays of_ `nums`.

**Note** that an empty array is considered strictly increasing.

A **subarray** is a contiguous non-empty sequence of elements within an array.

**Example 1:**

**Input:** nums = [1,2,3,4]

**Output:** 10

**Explanation:** The 10 incremovable subarrays are: [1], [2], [3], [4], [1,2], [2,3], [3,4], [1,2,3], [2,3,4], and [1,2,3,4], because on removing any one of these subarrays nums becomes strictly increasing. Note that you cannot select an empty subarray.

**Example 2:**

**Input:** nums = [6,5,7,8]

**Output:** 7

**Explanation:** The 7 incremovable subarrays are: [5], [6], [5,7], [6,5], [5,7,8], [6,5,7] and [6,5,7,8]. It can be shown that there are only 7 incremovable subarrays in nums.

**Example 3:**

**Input:** nums = [8,7,6,6]

**Output:** 3

**Explanation:** The 3 incremovable subarrays are: [8,7,6], [7,6,6], and [8,7,6,6]. Note that [8,7] is not an incremovable subarray because after removing [8,7] nums becomes [6,6], which is sorted in ascending order but not strictly increasing.

**Constraints:**

* `1 <= nums.length <= 50`
* `1 <= nums[i] <= 50`
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package g2901_3000.s2971_find_polygon_with_the_largest_perimeter;

// #Medium #Array #Sorting #Greedy #Prefix_Sum
// #2024_01_16_Time_21_ms_(98.77%)_Space_60.9_MB_(34.24%)

import java.util.Collections;
import java.util.PriorityQueue;

public class Solution {
public long largestPerimeter(int[] nums) {
long sum = 0L;
PriorityQueue<Long> pq = new PriorityQueue<>(Collections.reverseOrder());
for (int i : nums) {
pq.add((long) i);
sum = (sum + i);
}
while (pq.size() >= 3) {
long curr = pq.poll();
if (sum - curr > curr) {
return sum;
} else {
sum = sum - curr;
}
}
return -1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
2971\. Find Polygon With the Largest Perimeter

Medium

You are given an array of **positive** integers `nums` of length `n`.

A **polygon** is a closed plane figure that has at least `3` sides. The **longest side** of a polygon is **smaller** than the sum of its other sides.

Conversely, if you have `k` (`k >= 3`) **positive** real numbers <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code> where <code>a<sub>1</sub> <= a<sub>2</sub> <= a<sub>3</sub> <= ... <= a<sub>k</sub></code> **and** <code>a<sub>1</sub> + a<sub>2</sub> + a<sub>3</sub> + ... + a<sub>k-1</sub> > a<sub>k</sub></code>, then there **always** exists a polygon with `k` sides whose lengths are <code>a<sub>1</sub></code>, <code>a<sub>2</sub></code>, <code>a<sub>3</sub></code>, ..., <code>a<sub>k</sub></code>.

The **perimeter** of a polygon is the sum of lengths of its sides.

Return _the **largest** possible **perimeter** of a **polygon** whose sides can be formed from_ `nums`, _or_ `-1` _if it is not possible to create a polygon_.

**Example 1:**

**Input:** nums = [5,5,5]

**Output:** 15

**Explanation:** The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15.

**Example 2:**

**Input:** nums = [1,12,1,2,5,50,3]

**Output:** 12

**Explanation:** The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12.

**Example 3:**

**Input:** nums = [5,5,50]

**Output:** -1

**Explanation:** There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 > 5 + 5.

**Constraints:**

* <code>3 <= n <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package g2901_3000.s2972_count_the_number_of_incremovable_subarrays_ii;

// #Hard #Array #Binary_Search #Two_Pointers #2024_01_16_Time_1_ms_(100.00%)_Space_57.8_MB_(66.47%)

public class Solution {
public long incremovableSubarrayCount(int[] nums) {
long ans = 0;
int n = nums.length;
int l = 0;
int r = n - 1;
while (l + 1 < n && nums[l] < nums[l + 1]) {
l++;
}
while (r > 0 && nums[r - 1] < nums[r]) {
r--;
}
ans = (l == n - 1) ? 0 : 1 + (n - r);
for (int i = 0; i <= l; i++) {
while (r < n && nums[r] <= nums[i]) {
r++;
}
ans += n - r + 1;
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
2972\. Count the Number of Incremovable Subarrays II

Hard

You are given a **0-indexed** array of **positive** integers `nums`.

A subarray of `nums` is called **incremovable** if `nums` becomes **strictly increasing** on removing the subarray. For example, the subarray `[3, 4]` is an incremovable subarray of `[5, 3, 4, 6, 7]` because removing this subarray changes the array `[5, 3, 4, 6, 7]` to `[5, 6, 7]` which is strictly increasing.

Return _the total number of **incremovable** subarrays of_ `nums`.

**Note** that an empty array is considered strictly increasing.

A **subarray** is a contiguous non-empty sequence of elements within an array.

**Example 1:**

**Input:** nums = [1,2,3,4]

**Output:** 10

**Explanation:** The 10 incremovable subarrays are: [1], [2], [3], [4], [1,2], [2,3], [3,4], [1,2,3], [2,3,4], and [1,2,3,4], because on removing any one of these subarrays nums becomes strictly increasing. Note that you cannot select an empty subarray.

**Example 2:**

**Input:** nums = [6,5,7,8]

**Output:** 7

**Explanation:** The 7 incremovable subarrays are: [5], [6], [5,7], [6,5], [5,7,8], [6,5,7] and [6,5,7,8]. It can be shown that there are only 7 incremovable subarrays in nums.

**Example 3:**

**Input:** nums = [8,7,6,6]

**Output:** 3

**Explanation:** The 3 incremovable subarrays are: [8,7,6], [7,6,6], and [8,7,6,6]. Note that [8,7] is not an incremovable subarray because after removing [8,7] nums becomes [6,6], which is sorted in ascending order but not strictly increasing.

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package g2901_3000.s2973_find_number_of_coins_to_place_in_tree_nodes;

// #Hard #Dynamic_Programming #Sorting #Tree #Heap_Priority_Queue #Depth_First_Search
// #2024_01_16_Time_93_ms_(72.11%)_Space_63.4_MB_(33.51%)

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;

public class Solution {
private long[] result;

public long[] placedCoins(int[][] edges, int[] cost) {
int n = cost.length;
List<List<Integer>> g = new ArrayList<>();
for (int i = 0; i < n; i++) {
g.add(new ArrayList<>());
}
for (int[] e : edges) {
g.get(e[0]).add(e[1]);
g.get(e[1]).add(e[0]);
}
result = new long[n];
dp(g, cost, 0, -1);
return result;
}

private static class PQX {
PriorityQueue<Integer> min;
PriorityQueue<Integer> max;
}

private PQX dp(List<List<Integer>> g, int[] cost, int i, int p) {
if (i >= g.size()) {
PQX pqx = new PQX();
pqx.max = new PriorityQueue<>((a, b) -> b - a);
pqx.min = new PriorityQueue<>(Comparator.comparingInt(a -> a));
return pqx;
}
List<Integer> next = g.get(i);
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
PriorityQueue<Integer> pq2 = new PriorityQueue<>(Comparator.comparingInt(a -> a));
if (cost[i] > 0) {
pq.add(cost[i]);
} else {
pq2.add(cost[i]);
}
for (int ne : next) {
if (ne != p) {
PQX r = dp(g, cost, ne, i);
while (!r.min.isEmpty()) {
int a = r.min.poll();
pq2.add(a);
}
while (!r.max.isEmpty()) {
int a = r.max.poll();
pq.add(a);
}
}
}
if (pq.size() + pq2.size() < 3) {
result[i] = 1;
} else {
int a = !pq.isEmpty() ? pq.poll() : 0;
int b = !pq.isEmpty() ? pq.poll() : 0;
int c = !pq.isEmpty() ? pq.poll() : 0;
int aa = !pq2.isEmpty() ? pq2.poll() : 0;
int bb = !pq2.isEmpty() ? pq2.poll() : 0;
result[i] = Math.max(0, (long) a * b * c);
result[i] = Math.max(result[i], Math.max(0, (long) a * aa * bb));
pq = new PriorityQueue<>((x, y) -> y - x);
pq.add(a);
pq.add(b);
pq.add(c);
pq2 = new PriorityQueue<>(Comparator.comparingInt(x -> x));
pq2.add(aa);
pq2.add(bb);
}
PQX pqx = new PQX();
pqx.min = pq2;
pqx.max = pq;
return pqx;
}
}
Loading

0 comments on commit fc3f7e3

Please sign in to comment.