Skip to content

Commit b154fab

Browse files
committed
Added tasks 3467-3475
1 parent a790409 commit b154fab

File tree

11 files changed

+919
-3
lines changed
  • src/main/java/g3401_3500
    • s3462_maximum_sum_with_at_most_k_elements
    • s3467_transform_array_by_parity
    • s3468_find_the_number_of_copy_arrays
    • s3469_find_minimum_cost_to_remove_array_elements
    • s3470_permutations_iv
    • s3471_find_the_largest_almost_missing_integer
    • s3472_longest_palindromic_subsequence_after_at_most_k_operations
    • s3473_sum_of_k_subarrays_with_length_at_least_m
    • s3474_lexicographically_smallest_generated_string
    • s3475_dna_pattern_recognition

11 files changed

+919
-3
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2088,10 +2088,19 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091+
| 3475 |[DNA Pattern Recognition](src/main/java/g3401_3500/s3475_dna_pattern_recognition)| Medium | Database | 362 | 83.49
2092+
| 3474 |[Lexicographically Smallest Generated String](src/main/java/g3401_3500/s3474_lexicographically_smallest_generated_string)| Hard | String, Greedy, String_Matching | 17 | 64.86
2093+
| 3473 |[Sum of K Subarrays With Length at Least M](src/main/java/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m)| Medium | Array, Dynamic_Programming, Prefix_Sum | 191 | 52.16
2094+
| 3472 |[Longest Palindromic Subsequence After at Most K Operations](src/main/java/g3401_3500/s3472_longest_palindromic_subsequence_after_at_most_k_operations)| Medium | String, Dynamic_Programming | 153 | 85.01
2095+
| 3471 |[Find the Largest Almost Missing Integer](src/main/java/g3401_3500/s3471_find_the_largest_almost_missing_integer)| Easy | Array, Hash_Table | 4 | 73.95
2096+
| 3470 |[Permutations IV](src/main/java/g3401_3500/s3470_permutations_iv)| Hard | Array, Math, Enumeration, Combinatorics | 11 | 59.56
2097+
| 3469 |[Find Minimum Cost to Remove Array Elements](src/main/java/g3401_3500/s3469_find_minimum_cost_to_remove_array_elements)| Medium | Array, Dynamic_Programming | 12 | 100.00
2098+
| 3468 |[Find the Number of Copy Arrays](src/main/java/g3401_3500/s3468_find_the_number_of_copy_arrays)| Medium | Array, Math | 2 | 100.00
2099+
| 3467 |[Transform Array by Parity](src/main/java/g3401_3500/s3467_transform_array_by_parity)| Easy | Array, Sorting, Counting | 1 | 100.00
20912100
| 3465 |[Find Products with Valid Serial Numbers](src/main/java/g3401_3500/s3465_find_products_with_valid_serial_numbers)| Easy | Database | 292 | 90.91
20922101
| 3464 |[Maximize the Distance Between Points on a Square](src/main/java/g3401_3500/s3464_maximize_the_distance_between_points_on_a_square)| Hard | Array, Greedy, Binary_Search | 17 | 98.18
20932102
| 3463 |[Check If Digits Are Equal in String After Operations II](src/main/java/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii)| Hard | String, Math, Number_Theory, Combinatorics | 43 | 99.64
2094-
| 3462 |[Maximum Sum With at Most K Elements](src/main/java/g3401_3500/s3462_maximum_sum_with_at_most_k_elements)| Medium | Array, Sorting, Greedy, Matrix, Heap_(Priority_Queue) | 62 | 99.82
2103+
| 3462 |[Maximum Sum With at Most K Elements](src/main/java/g3401_3500/s3462_maximum_sum_with_at_most_k_elements)| Medium | Array, Sorting, Greedy, Matrix, Heap_Priority_Queue | 62 | 99.82
20952104
| 3461 |[Check If Digits Are Equal in String After Operations I](src/main/java/g3401_3500/s3461_check_if_digits_are_equal_in_string_after_operations_i)| Easy | String, Math, Simulation, Number_Theory, Combinatorics | 2 | 96.71
20962105
| 3459 |[Length of Longest V-Shaped Diagonal Segment](src/main/java/g3401_3500/s3459_length_of_longest_v_shaped_diagonal_segment)| Hard | Array, Dynamic_Programming, Matrix, Memoization | 56 | 72.97
20972106
| 3458 |[Select K Disjoint Special Substrings](src/main/java/g3401_3500/s3458_select_k_disjoint_special_substrings)| Medium | String, Hash_Table, Dynamic_Programming, Sorting, Greedy | 7 | 95.31

src/main/java/g3401_3500/s3462_maximum_sum_with_at_most_k_elements/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ import java.util.Arrays;
5252
public class Solution {
5353
public long maxSum(int[][] grid, int[] limits, int k) {
5454
int l = 0;
55-
for (int i = 0; i < limits.length; i++) {
56-
l += limits[i];
55+
for (int limit : limits) {
56+
l += limit;
5757
}
5858
int[] dp = new int[l];
5959
int a = 0;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3467\. Transform Array by Parity
5+
6+
Easy
7+
8+
You are given an integer array `nums`. Transform `nums` by performing the following operations in the **exact** order specified:
9+
10+
1. Replace each even number with 0.
11+
2. Replace each odd numbers with 1.
12+
3. Sort the modified array in **non-decreasing** order.
13+
14+
Return the resulting array after performing these operations.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [4,3,2,1]
19+
20+
**Output:** [0,0,1,1]
21+
22+
**Explanation:**
23+
24+
* Replace the even numbers (4 and 2) with 0 and the odd numbers (3 and 1) with 1. Now, `nums = [0, 1, 0, 1]`.
25+
* After sorting `nums` in non-descending order, `nums = [0, 0, 1, 1]`.
26+
27+
**Example 2:**
28+
29+
**Input:** nums = [1,5,1,4,2]
30+
31+
**Output:** [0,0,1,1,1]
32+
33+
**Explanation:**
34+
35+
* Replace the even numbers (4 and 2) with 0 and the odd numbers (1, 5 and 1) with 1. Now, `nums = [1, 1, 1, 0, 0]`.
36+
* After sorting `nums` in non-descending order, `nums = [0, 0, 1, 1, 1]`.
37+
38+
**Constraints:**
39+
40+
* `1 <= nums.length <= 100`
41+
* `1 <= nums[i] <= 1000`
42+
43+
## Solution
44+
45+
```java
46+
public class Solution {
47+
public int[] transformArray(int[] nums) {
48+
int size = nums.length;
49+
int[] ans = new int[size];
50+
int countEven = 0;
51+
for (int num : nums) {
52+
if ((num & 1) == 0) {
53+
countEven++;
54+
}
55+
}
56+
for (int i = countEven; i < size; i++) {
57+
ans[i] = 1;
58+
}
59+
return ans;
60+
}
61+
}
62+
```
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3468\. Find the Number of Copy Arrays
5+
6+
Medium
7+
8+
You are given an array `original` of length `n` and a 2D array `bounds` of length `n x 2`, where <code>bounds[i] = [u<sub>i</sub>, v<sub>i</sub>]</code>.
9+
10+
You need to find the number of **possible** arrays `copy` of length `n` such that:
11+
12+
1. `(copy[i] - copy[i - 1]) == (original[i] - original[i - 1])` for `1 <= i <= n - 1`.
13+
2. <code>u<sub>i</sub> <= copy[i] <= v<sub>i</sub></code> for `0 <= i <= n - 1`.
14+
15+
Return the number of such arrays.
16+
17+
**Example 1:**
18+
19+
**Input:** original = [1,2,3,4], bounds = \[\[1,2],[2,3],[3,4],[4,5]]
20+
21+
**Output:** 2
22+
23+
**Explanation:**
24+
25+
The possible arrays are:
26+
27+
* `[1, 2, 3, 4]`
28+
* `[2, 3, 4, 5]`
29+
30+
**Example 2:**
31+
32+
**Input:** original = [1,2,3,4], bounds = \[\[1,10],[2,9],[3,8],[4,7]]
33+
34+
**Output:** 4
35+
36+
**Explanation:**
37+
38+
The possible arrays are:
39+
40+
* `[1, 2, 3, 4]`
41+
* `[2, 3, 4, 5]`
42+
* `[3, 4, 5, 6]`
43+
* `[4, 5, 6, 7]`
44+
45+
**Example 3:**
46+
47+
**Input:** original = [1,2,1,2], bounds = \[\[1,1],[2,3],[3,3],[2,3]]
48+
49+
**Output:** 0
50+
51+
**Explanation:**
52+
53+
No array is possible.
54+
55+
**Constraints:**
56+
57+
* <code>2 <= n == original.length <= 10<sup>5</sup></code>
58+
* <code>1 <= original[i] <= 10<sup>9</sup></code>
59+
* `bounds.length == n`
60+
* `bounds[i].length == 2`
61+
* <code>1 <= bounds[i][0] <= bounds[i][1] <= 10<sup>9</sup></code>
62+
63+
## Solution
64+
65+
```java
66+
public class Solution {
67+
public int countArrays(int[] original, int[][] bounds) {
68+
int low = bounds[0][0];
69+
int high = bounds[0][1];
70+
int ans = high - low + 1;
71+
for (int i = 1; i < original.length; ++i) {
72+
int diff = original[i] - original[i - 1];
73+
low = Math.max(low + diff, bounds[i][0]);
74+
high = Math.min(high + diff, bounds[i][1]);
75+
ans = Math.min(ans, high - low + 1);
76+
}
77+
return Math.max(ans, 0);
78+
}
79+
}
80+
```
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3469\. Find Minimum Cost to Remove Array Elements
5+
6+
Medium
7+
8+
You are given an integer array `nums`. Your task is to remove **all elements** from the array by performing one of the following operations at each step until `nums` is empty:
9+
10+
* Choose any two elements from the first three elements of `nums` and remove them. The cost of this operation is the **maximum** of the two elements removed.
11+
* If fewer than three elements remain in `nums`, remove all the remaining elements in a single operation. The cost of this operation is the **maximum** of the remaining elements.
12+
13+
Return the **minimum** cost required to remove all the elements.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [6,2,8,4]
18+
19+
**Output:** 12
20+
21+
**Explanation:**
22+
23+
Initially, `nums = [6, 2, 8, 4]`.
24+
25+
* In the first operation, remove `nums[0] = 6` and `nums[2] = 8` with a cost of `max(6, 8) = 8`. Now, `nums = [2, 4]`.
26+
* In the second operation, remove the remaining elements with a cost of `max(2, 4) = 4`.
27+
28+
The cost to remove all elements is `8 + 4 = 12`. This is the minimum cost to remove all elements in `nums`. Hence, the output is 12.
29+
30+
**Example 2:**
31+
32+
**Input:** nums = [2,1,3,3]
33+
34+
**Output:** 5
35+
36+
**Explanation:**
37+
38+
Initially, `nums = [2, 1, 3, 3]`.
39+
40+
* In the first operation, remove `nums[0] = 2` and `nums[1] = 1` with a cost of `max(2, 1) = 2`. Now, `nums = [3, 3]`.
41+
* In the second operation remove the remaining elements with a cost of `max(3, 3) = 3`.
42+
43+
The cost to remove all elements is `2 + 3 = 5`. This is the minimum cost to remove all elements in `nums`. Hence, the output is 5.
44+
45+
**Constraints:**
46+
47+
* `1 <= nums.length <= 1000`
48+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
49+
50+
## Solution
51+
52+
```java
53+
import java.util.Arrays;
54+
55+
public class Solution {
56+
private static final int INF = (int) 1e9;
57+
58+
public int minCost(int[] nums) {
59+
int n = nums.length;
60+
if (n % 2 == 0) {
61+
nums = Arrays.copyOf(nums, ++n);
62+
}
63+
int[] dp = new int[n];
64+
for (int j = 1; j < n - 1; j += 2) {
65+
int cost1 = INF;
66+
int cost2 = INF;
67+
int max = Math.max(nums[j], nums[j + 1]);
68+
for (int i = 0; i < j; ++i) {
69+
cost1 = Math.min(cost1, dp[i] + Math.max(nums[i], nums[j + 1]));
70+
cost2 = Math.min(cost2, dp[i] + Math.max(nums[i], nums[j]));
71+
dp[i] += max;
72+
}
73+
dp[j] = cost1;
74+
dp[j + 1] = cost2;
75+
}
76+
int result = INF;
77+
for (int i = 0; i < n; ++i) {
78+
result = Math.min(result, dp[i] + nums[i]);
79+
}
80+
return result;
81+
}
82+
}
83+
```

0 commit comments

Comments
 (0)