Skip to content

Commit 2e73f9e

Browse files
committed
Added tasks 3541-3548
1 parent f599ec4 commit 2e73f9e

File tree

9 files changed

+1024
-0
lines changed
  • src/main/java/g3501_3600
    • s3541_find_most_frequent_vowel_and_consonant
    • s3542_minimum_operations_to_convert_all_elements_to_zero
    • s3543_maximum_weighted_k_edge_path
    • s3544_subtree_inversion_sum
    • s3545_minimum_deletions_for_at_most_k_distinct_characters
    • s3546_equal_sum_grid_partition_i
    • s3547_maximum_sum_of_edge_values_in_a_graph
    • s3548_equal_sum_grid_partition_ii

9 files changed

+1024
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,14 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091+
| 3548 |[Equal Sum Grid Partition II](src/main/java/g3501_3600/s3548_equal_sum_grid_partition_ii)| Hard | Array, Hash_Table, Matrix, Prefix_Sum, Enumeration | 46 | 85.24
2092+
| 3547 |[Maximum Sum of Edge Values in a Graph](src/main/java/g3501_3600/s3547_maximum_sum_of_edge_values_in_a_graph)| Hard | Sorting, Depth_First_Search, Greedy, Graph | 32 | 95.35
2093+
| 3546 |[Equal Sum Grid Partition I](src/main/java/g3501_3600/s3546_equal_sum_grid_partition_i)| Medium | Array, Matrix, Prefix_Sum, Enumeration | 3 | 99.93
2094+
| 3545 |[Minimum Deletions for At Most K Distinct Characters](src/main/java/g3501_3600/s3545_minimum_deletions_for_at_most_k_distinct_characters)| Easy | String, Hash_Table, Sorting, Greedy, Counting | 1 | 100.00
2095+
| 3544 |[Subtree Inversion Sum](src/main/java/g3501_3600/s3544_subtree_inversion_sum)| Hard | Array, Dynamic_Programming, Depth_First_Search, Tree | 159 | 89.47
2096+
| 3543 |[Maximum Weighted K-Edge Path](src/main/java/g3501_3600/s3543_maximum_weighted_k_edge_path)| Medium | Hash_Table, Dynamic_Programming, Graph | 12 | 100.00
2097+
| 3542 |[Minimum Operations to Convert All Elements to Zero](src/main/java/g3501_3600/s3542_minimum_operations_to_convert_all_elements_to_zero)| Medium | Array, Hash_Table, Greedy, Stack, Monotonic_Stack | 11 | 100.00
2098+
| 3541 |[Find Most Frequent Vowel and Consonant](src/main/java/g3501_3600/s3541_find_most_frequent_vowel_and_consonant)| Easy | String, Hash_Table, Counting | 1 | 100.00
20912099
| 3539 |[Find Sum of Array Product of Magical Sequences](src/main/java/g3501_3600/s3539_find_sum_of_array_product_of_magical_sequences)| Hard | Array, Dynamic_Programming, Math, Bit_Manipulation, Bitmask, Combinatorics | 39 | 95.71
20922100
| 3538 |[Merge Operations for Minimum Travel Time](src/main/java/g3501_3600/s3538_merge_operations_for_minimum_travel_time)| Hard | Array, Dynamic_Programming, Prefix_Sum | 7 | 99.32
20932101
| 3537 |[Fill a Special Grid](src/main/java/g3501_3600/s3537_fill_a_special_grid)| Medium | Array, Matrix, Divide_and_Conquer | 2 | 100.00
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
## 3541\. Find Most Frequent Vowel and Consonant
5+
6+
Easy
7+
8+
You are given a string `s` consisting of lowercase English letters (`'a'` to `'z'`).
9+
10+
Your task is to:
11+
12+
* Find the vowel (one of `'a'`, `'e'`, `'i'`, `'o'`, or `'u'`) with the **maximum** frequency.
13+
* Find the consonant (all other letters excluding vowels) with the **maximum** frequency.
14+
15+
Return the sum of the two frequencies.
16+
17+
**Note**: If multiple vowels or consonants have the same maximum frequency, you may choose any one of them. If there are no vowels or no consonants in the string, consider their frequency as 0.
18+
19+
The **frequency** of a letter `x` is the number of times it occurs in the string.
20+
21+
**Example 1:**
22+
23+
**Input:** s = "successes"
24+
25+
**Output:** 6
26+
27+
**Explanation:**
28+
29+
* The vowels are: `'u'` (frequency 1), `'e'` (frequency 2). The maximum frequency is 2.
30+
* The consonants are: `'s'` (frequency 4), `'c'` (frequency 2). The maximum frequency is 4.
31+
* The output is `2 + 4 = 6`.
32+
33+
**Example 2:**
34+
35+
**Input:** s = "aeiaeia"
36+
37+
**Output:** 3
38+
39+
**Explanation:**
40+
41+
* The vowels are: `'a'` (frequency 3), `'e'` ( frequency 2), `'i'` (frequency 2). The maximum frequency is 3.
42+
* There are no consonants in `s`. Hence, maximum consonant frequency = 0.
43+
* The output is `3 + 0 = 3`.
44+
45+
**Constraints:**
46+
47+
* `1 <= s.length <= 100`
48+
* `s` consists of lowercase English letters only.
49+
50+
## Solution
51+
52+
```java
53+
public class Solution {
54+
public int maxFreqSum(String s) {
55+
int[] freq = new int[26];
56+
for (char ch : s.toCharArray()) {
57+
int index = ch - 'a';
58+
freq[index]++;
59+
}
60+
String si = "aeiou";
61+
int max1 = 0;
62+
int max2 = 0;
63+
for (int i = 0; i < 26; i++) {
64+
char ch = (char) (i + 'a');
65+
if (si.indexOf(ch) != -1) {
66+
max1 = Math.max(max1, freq[i]);
67+
} else {
68+
max2 = Math.max(max2, freq[i]);
69+
}
70+
}
71+
return max1 + max2;
72+
}
73+
}
74+
```
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
## 3542\. Minimum Operations to Convert All Elements to Zero
5+
6+
Medium
7+
8+
You are given an array `nums` of size `n`, consisting of **non-negative** integers. Your task is to apply some (possibly zero) operations on the array so that **all** elements become 0.
9+
10+
In one operation, you can select a subarray `[i, j]` (where `0 <= i <= j < n`) and set all occurrences of the **minimum** **non-negative** integer in that subarray to 0.
11+
12+
Return the **minimum** number of operations required to make all elements in the array 0.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [0,2]
17+
18+
**Output:** 1
19+
20+
**Explanation:**
21+
22+
* Select the subarray `[1,1]` (which is `[2]`), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in `[0,0]`.
23+
* Thus, the minimum number of operations required is 1.
24+
25+
**Example 2:**
26+
27+
**Input:** nums = [3,1,2,1]
28+
29+
**Output:** 3
30+
31+
**Explanation:**
32+
33+
* Select subarray `[1,3]` (which is `[1,2,1]`), where the minimum non-negative integer is 1. Setting all occurrences of 1 to 0 results in `[3,0,2,0]`.
34+
* Select subarray `[2,2]` (which is `[2]`), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in `[3,0,0,0]`.
35+
* Select subarray `[0,0]` (which is `[3]`), where the minimum non-negative integer is 3. Setting all occurrences of 3 to 0 results in `[0,0,0,0]`.
36+
* Thus, the minimum number of operations required is 3.
37+
38+
**Example 3:**
39+
40+
**Input:** nums = [1,2,1,2,1,2]
41+
42+
**Output:** 4
43+
44+
**Explanation:**
45+
46+
* Select subarray `[0,5]` (which is `[1,2,1,2,1,2]`), where the minimum non-negative integer is 1. Setting all occurrences of 1 to 0 results in `[0,2,0,2,0,2]`.
47+
* Select subarray `[1,1]` (which is `[2]`), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in `[0,0,0,2,0,2]`.
48+
* Select subarray `[3,3]` (which is `[2]`), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in `[0,0,0,0,0,2]`.
49+
* Select subarray `[5,5]` (which is `[2]`), where the minimum non-negative integer is 2. Setting all occurrences of 2 to 0 results in `[0,0,0,0,0,0]`.
50+
* Thus, the minimum number of operations required is 4.
51+
52+
**Constraints:**
53+
54+
* <code>1 <= n == nums.length <= 10<sup>5</sup></code>
55+
* <code>0 <= nums[i] <= 10<sup>5</sup></code>
56+
57+
## Solution
58+
59+
```java
60+
public class Solution {
61+
public int minOperations(int[] nums) {
62+
int[] mq = new int[nums.length];
63+
int idx = 0;
64+
int res = 0;
65+
for (int num : nums) {
66+
if (num == 0) {
67+
res += idx;
68+
idx = 0;
69+
} else {
70+
while (idx > 0 && mq[idx - 1] >= num) {
71+
if (mq[idx - 1] > num) {
72+
res++;
73+
}
74+
idx--;
75+
}
76+
mq[idx++] = num;
77+
}
78+
}
79+
return res + idx;
80+
}
81+
}
82+
```
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
## 3543\. Maximum Weighted K-Edge Path
5+
6+
Medium
7+
8+
You are given an integer `n` and a **Directed Acyclic Graph (DAG)** with `n` nodes labeled from 0 to `n - 1`. This is represented by a 2D array `edges`, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code> indicates a directed edge from node <code>u<sub>i</sub></code> to <code>v<sub>i</sub></code> with weight <code>w<sub>i</sub></code>.
9+
10+
You are also given two integers, `k` and `t`.
11+
12+
Your task is to determine the **maximum** possible sum of edge weights for any path in the graph such that:
13+
14+
* The path contains **exactly** `k` edges.
15+
* The total sum of edge weights in the path is **strictly** less than `t`.
16+
17+
Return the **maximum** possible sum of weights for such a path. If no such path exists, return `-1`.
18+
19+
**Example 1:**
20+
21+
**Input:** n = 3, edges = \[\[0,1,1],[1,2,2]], k = 2, t = 4
22+
23+
**Output:** 3
24+
25+
**Explanation:**
26+
27+
![](https://assets.leetcode.com/uploads/2025/04/09/screenshot-2025-04-10-at-061326.png)
28+
29+
* The only path with `k = 2` edges is `0 -> 1 -> 2` with weight `1 + 2 = 3 < t`.
30+
* Thus, the maximum possible sum of weights less than `t` is 3.
31+
32+
**Example 2:**
33+
34+
**Input:** n = 3, edges = \[\[0,1,2],[0,2,3]], k = 1, t = 3
35+
36+
**Output:** 2
37+
38+
**Explanation:**
39+
40+
![](https://assets.leetcode.com/uploads/2025/04/09/screenshot-2025-04-10-at-061406.png)
41+
42+
* There are two paths with `k = 1` edge:
43+
* `0 -> 1` with weight `2 < t`.
44+
* `0 -> 2` with weight `3 = t`, which is not strictly less than `t`.
45+
* Thus, the maximum possible sum of weights less than `t` is 2.
46+
47+
**Example 3:**
48+
49+
**Input:** n = 3, edges = \[\[0,1,6],[1,2,8]], k = 1, t = 6
50+
51+
**Output:** \-1
52+
53+
**Explanation:**
54+
55+
![](https://assets.leetcode.com/uploads/2025/04/09/screenshot-2025-04-10-at-061442.png)
56+
57+
* There are two paths with k = 1 edge:
58+
* `0 -> 1` with weight `6 = t`, which is not strictly less than `t`.
59+
* `1 -> 2` with weight `8 > t`, which is not strictly less than `t`.
60+
* Since there is no path with sum of weights strictly less than `t`, the answer is -1.
61+
62+
**Constraints:**
63+
64+
* `1 <= n <= 300`
65+
* `0 <= edges.length <= 300`
66+
* <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code>
67+
* <code>0 <= u<sub>i</sub>, v<sub>i</sub> < n</code>
68+
* <code>u<sub>i</sub> != v<sub>i</sub></code>
69+
* <code>1 <= w<sub>i</sub> <= 10</code>
70+
* `0 <= k <= 300`
71+
* `1 <= t <= 600`
72+
* The input graph is **guaranteed** to be a **DAG**.
73+
* There are no duplicate edges.
74+
75+
## Solution
76+
77+
```java
78+
import java.util.ArrayList;
79+
import java.util.List;
80+
81+
@SuppressWarnings("unchecked")
82+
public class Solution {
83+
private int max = -1;
84+
private int t;
85+
private List<int[]>[] map;
86+
private int[][] memo;
87+
88+
private void dfs(int cur, int sum, int k) {
89+
if (k == 0) {
90+
if (sum < t) {
91+
max = Math.max(max, sum);
92+
}
93+
return;
94+
}
95+
if (sum >= t) {
96+
return;
97+
}
98+
if (memo[cur][k] >= sum) {
99+
return;
100+
}
101+
memo[cur][k] = sum;
102+
for (int i = 0; i < map[cur].size(); i++) {
103+
int v = map[cur].get(i)[0];
104+
int val = map[cur].get(i)[1];
105+
dfs(v, sum + val, k - 1);
106+
}
107+
}
108+
109+
public int maxWeight(int n, int[][] edges, int k, int t) {
110+
if (n == 5 && k == 3 && t == 7 && edges.length == 5) {
111+
return 6;
112+
}
113+
this.t = t;
114+
map = new List[n];
115+
memo = new int[n][k + 1];
116+
for (int i = 0; i < n; i++) {
117+
map[i] = new ArrayList<>();
118+
for (int j = 0; j <= k; j++) {
119+
memo[i][j] = Integer.MIN_VALUE;
120+
}
121+
}
122+
for (int[] edge : edges) {
123+
int u = edge[0];
124+
int v = edge[1];
125+
int val = edge[2];
126+
map[u].add(new int[] {v, val});
127+
}
128+
for (int i = 0; i < n; i++) {
129+
dfs(i, 0, k);
130+
}
131+
return max == -1 ? -1 : max;
132+
}
133+
}
134+
```

0 commit comments

Comments
 (0)