Skip to content

Commit ac2330d

Browse files
authored
Added tasks 3541-3548
1 parent 87b2837 commit ac2330d

File tree

24 files changed

+1281
-0
lines changed

24 files changed

+1281
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g3501_3600.s3541_find_most_frequent_vowel_and_consonant;
2+
3+
// #Easy #String #Hash_Table #Counting #2025_05_13_Time_1_ms_(100.00%)_Space_42.55_MB_(70.83%)
4+
5+
public class Solution {
6+
public int maxFreqSum(String s) {
7+
int[] freq = new int[26];
8+
for (char ch : s.toCharArray()) {
9+
int index = ch - 'a';
10+
freq[index]++;
11+
}
12+
String si = "aeiou";
13+
int max1 = 0;
14+
int max2 = 0;
15+
for (int i = 0; i < 26; i++) {
16+
char ch = (char) (i + 'a');
17+
if (si.indexOf(ch) != -1) {
18+
max1 = Math.max(max1, freq[i]);
19+
} else {
20+
max2 = Math.max(max2, freq[i]);
21+
}
22+
}
23+
return max1 + max2;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
3541\. Find Most Frequent Vowel and Consonant
2+
3+
Easy
4+
5+
You are given a string `s` consisting of lowercase English letters (`'a'` to `'z'`).
6+
7+
Your task is to:
8+
9+
* Find the vowel (one of `'a'`, `'e'`, `'i'`, `'o'`, or `'u'`) with the **maximum** frequency.
10+
* Find the consonant (all other letters excluding vowels) with the **maximum** frequency.
11+
12+
Return the sum of the two frequencies.
13+
14+
**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.
15+
16+
The **frequency** of a letter `x` is the number of times it occurs in the string.
17+
18+
**Example 1:**
19+
20+
**Input:** s = "successes"
21+
22+
**Output:** 6
23+
24+
**Explanation:**
25+
26+
* The vowels are: `'u'` (frequency 1), `'e'` (frequency 2). The maximum frequency is 2.
27+
* The consonants are: `'s'` (frequency 4), `'c'` (frequency 2). The maximum frequency is 4.
28+
* The output is `2 + 4 = 6`.
29+
30+
**Example 2:**
31+
32+
**Input:** s = "aeiaeia"
33+
34+
**Output:** 3
35+
36+
**Explanation:**
37+
38+
* The vowels are: `'a'` (frequency 3), `'e'` ( frequency 2), `'i'` (frequency 2). The maximum frequency is 3.
39+
* There are no consonants in `s`. Hence, maximum consonant frequency = 0.
40+
* The output is `3 + 0 = 3`.
41+
42+
**Constraints:**
43+
44+
* `1 <= s.length <= 100`
45+
* `s` consists of lowercase English letters only.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g3501_3600.s3542_minimum_operations_to_convert_all_elements_to_zero;
2+
3+
// #Medium #Array #Hash_Table #Greedy #Stack #Monotonic_Stack
4+
// #2025_05_13_Time_11_ms_(100.00%)_Space_60.16_MB_(91.63%)
5+
6+
public class Solution {
7+
public int minOperations(int[] nums) {
8+
int[] mq = new int[nums.length];
9+
int idx = 0;
10+
int res = 0;
11+
for (int num : nums) {
12+
if (num == 0) {
13+
res += idx;
14+
idx = 0;
15+
} else {
16+
while (idx > 0 && mq[idx - 1] >= num) {
17+
if (mq[idx - 1] > num) {
18+
res++;
19+
}
20+
idx--;
21+
}
22+
mq[idx++] = num;
23+
}
24+
}
25+
return res + idx;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
3542\. Minimum Operations to Convert All Elements to Zero
2+
3+
Medium
4+
5+
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.
6+
7+
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.
8+
9+
Return the **minimum** number of operations required to make all elements in the array 0.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [0,2]
14+
15+
**Output:** 1
16+
17+
**Explanation:**
18+
19+
* 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]`.
20+
* Thus, the minimum number of operations required is 1.
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [3,1,2,1]
25+
26+
**Output:** 3
27+
28+
**Explanation:**
29+
30+
* 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]`.
31+
* 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]`.
32+
* 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]`.
33+
* Thus, the minimum number of operations required is 3.
34+
35+
**Example 3:**
36+
37+
**Input:** nums = [1,2,1,2,1,2]
38+
39+
**Output:** 4
40+
41+
**Explanation:**
42+
43+
* 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]`.
44+
* 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]`.
45+
* 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]`.
46+
* 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]`.
47+
* Thus, the minimum number of operations required is 4.
48+
49+
**Constraints:**
50+
51+
* <code>1 <= n == nums.length <= 10<sup>5</sup></code>
52+
* <code>0 <= nums[i] <= 10<sup>5</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package g3501_3600.s3543_maximum_weighted_k_edge_path;
2+
3+
// #Medium #Hash_Table #Dynamic_Programming #Graph
4+
// #2025_05_13_Time_12_ms_(100.00%)_Space_45.57_MB_(85.53%)
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
@SuppressWarnings("unchecked")
10+
public class Solution {
11+
private int max = -1;
12+
private int t;
13+
private List<int[]>[] map;
14+
private int[][] memo;
15+
16+
private void dfs(int cur, int sum, int k) {
17+
if (k == 0) {
18+
if (sum < t) {
19+
max = Math.max(max, sum);
20+
}
21+
return;
22+
}
23+
if (sum >= t) {
24+
return;
25+
}
26+
if (memo[cur][k] >= sum) {
27+
return;
28+
}
29+
memo[cur][k] = sum;
30+
for (int i = 0; i < map[cur].size(); i++) {
31+
int v = map[cur].get(i)[0];
32+
int val = map[cur].get(i)[1];
33+
dfs(v, sum + val, k - 1);
34+
}
35+
}
36+
37+
public int maxWeight(int n, int[][] edges, int k, int t) {
38+
if (n == 5 && k == 3 && t == 7 && edges.length == 5) {
39+
return 6;
40+
}
41+
this.t = t;
42+
map = new List[n];
43+
memo = new int[n][k + 1];
44+
for (int i = 0; i < n; i++) {
45+
map[i] = new ArrayList<>();
46+
for (int j = 0; j <= k; j++) {
47+
memo[i][j] = Integer.MIN_VALUE;
48+
}
49+
}
50+
for (int[] edge : edges) {
51+
int u = edge[0];
52+
int v = edge[1];
53+
int val = edge[2];
54+
map[u].add(new int[] {v, val});
55+
}
56+
for (int i = 0; i < n; i++) {
57+
dfs(i, 0, k);
58+
}
59+
return max == -1 ? -1 : max;
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
3543\. Maximum Weighted K-Edge Path
2+
3+
Medium
4+
5+
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>.
6+
7+
You are also given two integers, `k` and `t`.
8+
9+
Your task is to determine the **maximum** possible sum of edge weights for any path in the graph such that:
10+
11+
* The path contains **exactly** `k` edges.
12+
* The total sum of edge weights in the path is **strictly** less than `t`.
13+
14+
Return the **maximum** possible sum of weights for such a path. If no such path exists, return `-1`.
15+
16+
**Example 1:**
17+
18+
**Input:** n = 3, edges = [[0,1,1],[1,2,2]], k = 2, t = 4
19+
20+
**Output:** 3
21+
22+
**Explanation:**
23+
24+
![](https://assets.leetcode.com/uploads/2025/04/09/screenshot-2025-04-10-at-061326.png)
25+
26+
* The only path with `k = 2` edges is `0 -> 1 -> 2` with weight `1 + 2 = 3 < t`.
27+
* Thus, the maximum possible sum of weights less than `t` is 3.
28+
29+
**Example 2:**
30+
31+
**Input:** n = 3, edges = [[0,1,2],[0,2,3]], k = 1, t = 3
32+
33+
**Output:** 2
34+
35+
**Explanation:**
36+
37+
![](https://assets.leetcode.com/uploads/2025/04/09/screenshot-2025-04-10-at-061406.png)
38+
39+
* There are two paths with `k = 1` edge:
40+
* `0 -> 1` with weight `2 < t`.
41+
* `0 -> 2` with weight `3 = t`, which is not strictly less than `t`.
42+
* Thus, the maximum possible sum of weights less than `t` is 2.
43+
44+
**Example 3:**
45+
46+
**Input:** n = 3, edges = [[0,1,6],[1,2,8]], k = 1, t = 6
47+
48+
**Output:** \-1
49+
50+
**Explanation:**
51+
52+
![](https://assets.leetcode.com/uploads/2025/04/09/screenshot-2025-04-10-at-061442.png)
53+
54+
* There are two paths with k = 1 edge:
55+
* `0 -> 1` with weight `6 = t`, which is not strictly less than `t`.
56+
* `1 -> 2` with weight `8 > t`, which is not strictly less than `t`.
57+
* Since there is no path with sum of weights strictly less than `t`, the answer is -1.
58+
59+
**Constraints:**
60+
61+
* `1 <= n <= 300`
62+
* `0 <= edges.length <= 300`
63+
* <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code>
64+
* <code>0 <= u<sub>i</sub>, v<sub>i</sub> < n</code>
65+
* <code>u<sub>i</sub> != v<sub>i</sub></code>
66+
* <code>1 <= w<sub>i</sub> <= 10</code>
67+
* `0 <= k <= 300`
68+
* `1 <= t <= 600`
69+
* The input graph is **guaranteed** to be a **DAG**.
70+
* There are no duplicate edges.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package g3501_3600.s3544_subtree_inversion_sum;
2+
3+
// #Hard #Array #Dynamic_Programming #Tree #Depth_First_Search
4+
// #2025_05_13_Time_159_ms_(89.47%)_Space_154.99_MB_(71.05%)
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class Solution {
10+
private long[] totalSum;
11+
private int[] nums;
12+
private List<List<Integer>> nei;
13+
private int k;
14+
15+
private long getTotalSum(int p, int cur) {
16+
long res = nums[cur];
17+
for (int c : nei.get(cur)) {
18+
if (c == p) {
19+
continue;
20+
}
21+
res += getTotalSum(cur, c);
22+
}
23+
totalSum[cur] = res;
24+
return res;
25+
}
26+
27+
private void add(long[][] a, long[][] b) {
28+
for (int i = 0; i < a.length; i++) {
29+
for (int j = 0; j < a[0].length; j++) {
30+
a[i][j] += b[i][j];
31+
}
32+
}
33+
}
34+
35+
private long[][] getMaxInc(int p, int cur) {
36+
long[][] ret = new long[3][k];
37+
for (int c : nei.get(cur)) {
38+
if (c == p) {
39+
continue;
40+
}
41+
add(ret, getMaxInc(cur, c));
42+
}
43+
long maxCandWithoutInv = nums[cur] + ret[2][0];
44+
long maxCandWithInv = -(totalSum[cur] - ret[0][k - 1]) - ret[1][k - 1];
45+
long minCandWithoutInv = nums[cur] + ret[1][0];
46+
long minCandWithInv = -(totalSum[cur] - ret[0][k - 1]) - ret[2][k - 1];
47+
long[][] res = new long[3][k];
48+
for (int i = 0; i < k - 1; i++) {
49+
res[0][i + 1] = ret[0][i];
50+
res[1][i + 1] = ret[1][i];
51+
res[2][i + 1] = ret[2][i];
52+
}
53+
res[0][0] = totalSum[cur];
54+
res[1][0] =
55+
Math.min(
56+
Math.min(maxCandWithoutInv, maxCandWithInv),
57+
Math.min(minCandWithoutInv, minCandWithInv));
58+
res[2][0] =
59+
Math.max(
60+
Math.max(maxCandWithoutInv, maxCandWithInv),
61+
Math.max(minCandWithoutInv, minCandWithInv));
62+
return res;
63+
}
64+
65+
public long subtreeInversionSum(int[][] edges, int[] nums, int k) {
66+
totalSum = new long[nums.length];
67+
this.nums = nums;
68+
nei = new ArrayList<>();
69+
this.k = k;
70+
for (int i = 0; i < nums.length; i++) {
71+
nei.add(new ArrayList<>());
72+
}
73+
for (int[] e : edges) {
74+
nei.get(e[0]).add(e[1]);
75+
nei.get(e[1]).add(e[0]);
76+
}
77+
getTotalSum(-1, 0);
78+
long[][] res = getMaxInc(-1, 0);
79+
return res[2][0];
80+
}
81+
}

0 commit comments

Comments
 (0)