Skip to content

Commit 1780239

Browse files
committed
Added tasks 3541-3548
1 parent 2276d23 commit 1780239

File tree

24 files changed

+1226
-0
lines changed

24 files changed

+1226
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g3501_3600.s3541_find_most_frequent_vowel_and_consonant
2+
3+
// #Easy #2025_05_11_Time_8_ms_(100.00%)_Space_43.92_MB_(100.00%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
fun maxFreqSum(s: String): Int {
9+
val freq = IntArray(26)
10+
for (ch in s.toCharArray()) {
11+
val index = ch.code - 'a'.code
12+
freq[index]++
13+
}
14+
val si = "aeiou"
15+
var max1 = 0
16+
var max2 = 0
17+
for (i in 0..25) {
18+
val ch = (i + 'a'.code).toChar()
19+
if (si.indexOf(ch) != -1) {
20+
max1 = max(max1, freq[i])
21+
} else {
22+
max2 = max(max2, freq[i])
23+
}
24+
}
25+
return max1 + max2
26+
}
27+
}
Lines changed: 45 additions & 0 deletions
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.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g3501_3600.s3542_minimum_operations_to_convert_all_elements_to_zero
2+
3+
// #Medium #2025_05_11_Time_48_ms_(100.00%)_Space_78.24_MB_(100.00%)
4+
5+
import java.util.ArrayDeque
6+
import java.util.Deque
7+
8+
class Solution {
9+
fun minOperations(nums: IntArray): Int {
10+
val stack: Deque<Int> = ArrayDeque<Int>()
11+
stack.push(0)
12+
var res = 0
13+
for (a in nums) {
14+
while (!stack.isEmpty() && stack.peek()!! > a) {
15+
stack.pop()
16+
}
17+
if (stack.isEmpty() || stack.peek()!! < a) {
18+
res++
19+
stack.push(a)
20+
}
21+
}
22+
return res
23+
}
24+
}
Lines changed: 52 additions & 0 deletions
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>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package g3501_3600.s3543_maximum_weighted_k_edge_path
2+
3+
// #Medium #2025_05_11_Time_680_ms_(100.00%)_Space_308.89_MB_(100.00%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
private lateinit var dp: Array<Array<IntArray>>
9+
10+
private class Pair(var node: Int, var wt: Int)
11+
12+
fun maxWeight(n: Int, edges: Array<IntArray>, k: Int, t: Int): Int {
13+
if (k == 0) {
14+
return 0
15+
}
16+
dp = Array<Array<IntArray>>(n) { Array<IntArray>(k + 1) { IntArray(t + 1) } }
17+
for (i in 0..<n) {
18+
for (j in 0..k) {
19+
dp[i][j].fill(Int.Companion.MIN_VALUE)
20+
}
21+
}
22+
val adj: MutableList<MutableList<Pair>> = ArrayList<MutableList<Pair>>()
23+
for (i in 0..<n) {
24+
adj.add(ArrayList<Pair>())
25+
}
26+
for (edge in edges) {
27+
adj.get(edge[0]).add(Pair(edge[1], edge[2]))
28+
}
29+
var ans = -1
30+
for (start in 0..<n) {
31+
val res = dfs(adj, start, k, t, 0)
32+
ans = max(ans, res)
33+
}
34+
return ans
35+
}
36+
37+
private fun dfs(adj: MutableList<MutableList<Pair>>, u: Int, stepsRemaining: Int, t: Int, currentSum: Int): Int {
38+
if (currentSum >= t) {
39+
return -1
40+
}
41+
if (stepsRemaining == 0) {
42+
return currentSum
43+
}
44+
val memo = dp[u][stepsRemaining][currentSum]
45+
if (memo != Int.Companion.MIN_VALUE) {
46+
return memo
47+
}
48+
var best = -1
49+
for (p in adj.get(u)) {
50+
val res = dfs(adj, p.node, stepsRemaining - 1, t, currentSum + p.wt)
51+
best = max(best, res)
52+
}
53+
dp[u][stepsRemaining][currentSum] = best
54+
return best
55+
}
56+
}
Lines changed: 70 additions & 0 deletions
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.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package g3501_3600.s3544_subtree_inversion_sum
2+
3+
// #Hard #2025_05_11_Time_114_ms_(100.00%)_Space_195.14_MB_(100.00%)
4+
5+
import kotlin.math.max
6+
import kotlin.math.min
7+
8+
class Solution {
9+
private lateinit var totalSum: LongArray
10+
private lateinit var nums: IntArray
11+
private lateinit var nei: MutableList<MutableList<Int>>
12+
private var k = 0
13+
14+
private fun getTotalSum(p: Int, cur: Int): Long {
15+
var res = nums[cur].toLong()
16+
for (c in nei[cur]) {
17+
if (c == p) {
18+
continue
19+
}
20+
res += getTotalSum(cur, c)
21+
}
22+
totalSum[cur] = res
23+
return res
24+
}
25+
26+
private fun add(a: Array<LongArray>, b: Array<LongArray>) {
27+
for (i in a.indices) {
28+
for (j in a[0].indices) {
29+
a[i][j] += b[i][j]
30+
}
31+
}
32+
}
33+
34+
private fun getMaxInc(p: Int, cur: Int): Array<LongArray> {
35+
val ret = Array<LongArray>(3) { LongArray(k) }
36+
for (c in nei[cur]) {
37+
if (c == p) {
38+
continue
39+
}
40+
add(ret, getMaxInc(cur, c))
41+
}
42+
val maxCandWithoutInv = nums[cur] + ret[2][0]
43+
val maxCandWithInv = -(totalSum[cur] - ret[0][k - 1]) - ret[1][k - 1]
44+
val minCandWithoutInv = nums[cur] + ret[1][0]
45+
val minCandWithInv = -(totalSum[cur] - ret[0][k - 1]) - ret[2][k - 1]
46+
val res = Array<LongArray>(3) { LongArray(k) }
47+
for (i in 0..<k - 1) {
48+
res[0][i + 1] = ret[0][i]
49+
res[1][i + 1] = ret[1][i]
50+
res[2][i + 1] = ret[2][i]
51+
}
52+
res[0][0] = totalSum[cur]
53+
res[1][0] = min(
54+
min(maxCandWithoutInv, maxCandWithInv),
55+
min(minCandWithoutInv, minCandWithInv)
56+
)
57+
res[2][0] = max(
58+
max(maxCandWithoutInv, maxCandWithInv),
59+
max(minCandWithoutInv, minCandWithInv)
60+
)
61+
return res
62+
}
63+
64+
fun subtreeInversionSum(edges: Array<IntArray>, nums: IntArray, k: Int): Long {
65+
totalSum = LongArray(nums.size)
66+
this.nums = nums
67+
nei = ArrayList<MutableList<Int>>()
68+
this.k = k
69+
for (i in nums.indices) {
70+
nei.add(ArrayList<Int>())
71+
}
72+
for (e in edges) {
73+
nei.get(e[0]).add(e[1])
74+
nei.get(e[1]).add(e[0])
75+
}
76+
getTotalSum(-1, 0)
77+
val res = getMaxInc(-1, 0)
78+
return res[2][0]
79+
}
80+
}

0 commit comments

Comments
 (0)