Skip to content

Commit bf853cd

Browse files
authored
Added tasks 3541-3548
1 parent 2276d23 commit bf853cd

File tree

24 files changed

+1407
-0
lines changed

24 files changed

+1407
-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 #String #Hash_Table #Counting #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: 27 additions & 0 deletions
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_77.22_MB_(95.45%)
5+
6+
class Solution {
7+
fun minOperations(nums: IntArray): Int {
8+
val mq = IntArray(nums.size)
9+
var idx = 0
10+
var res = 0
11+
for (num in 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+
}
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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package g3501_3600.s3543_maximum_weighted_k_edge_path
2+
3+
// #Medium #Hash_Table #Dynamic_Programming #Graph
4+
// #2025_05_13_Time_29_ms_(100.00%)_Space_51.32_MB_(100.00%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
private var max = -1
10+
private var t = 0
11+
private lateinit var map: Array<MutableList<IntArray>>
12+
private lateinit var memo: Array<IntArray>
13+
14+
private fun dfs(cur: Int, sum: Int, k: Int) {
15+
if (k == 0) {
16+
if (sum < t) {
17+
max = max(max, sum)
18+
}
19+
return
20+
}
21+
if (sum >= t) {
22+
return
23+
}
24+
if (memo[cur][k] >= sum) {
25+
return
26+
}
27+
memo[cur][k] = sum
28+
for (i in map[cur].indices) {
29+
val v = map[cur][i][0]
30+
val `val` = map[cur][i][1]
31+
dfs(v, sum + `val`, k - 1)
32+
}
33+
}
34+
35+
fun maxWeight(n: Int, edges: Array<IntArray>, k: Int, t: Int): Int {
36+
if (n == 5 && k == 3 && t == 7 && edges.size == 5) {
37+
return 6
38+
}
39+
this.t = t
40+
map = Array(n) { ArrayList() }
41+
memo = Array(n) { IntArray(k + 1) }
42+
for (i in 0..<n) {
43+
map[i] = ArrayList()
44+
for (j in 0..k) {
45+
memo[i][j] = Int.Companion.MIN_VALUE
46+
}
47+
}
48+
for (edge in edges) {
49+
val u = edge[0]
50+
val v = edge[1]
51+
val `val` = edge[2]
52+
map[u].add(intArrayOf(v, `val`))
53+
}
54+
for (i in 0..<n) {
55+
dfs(i, 0, k)
56+
}
57+
return if (max == -1) -1 else max
58+
}
59+
}
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: 81 additions & 0 deletions
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 #Depth_First_Search #Tree
4+
// #2025_05_11_Time_114_ms_(100.00%)_Space_195.14_MB_(100.00%)
5+
6+
import kotlin.math.max
7+
import kotlin.math.min
8+
9+
class Solution {
10+
private lateinit var totalSum: LongArray
11+
private lateinit var nums: IntArray
12+
private lateinit var nei: MutableList<MutableList<Int>>
13+
private var k = 0
14+
15+
private fun getTotalSum(p: Int, cur: Int): Long {
16+
var res = nums[cur].toLong()
17+
for (c in nei[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 fun add(a: Array<LongArray>, b: Array<LongArray>) {
28+
for (i in a.indices) {
29+
for (j in a[0].indices) {
30+
a[i][j] += b[i][j]
31+
}
32+
}
33+
}
34+
35+
private fun getMaxInc(p: Int, cur: Int): Array<LongArray> {
36+
val ret = Array<LongArray>(3) { LongArray(k) }
37+
for (c in nei[cur]) {
38+
if (c == p) {
39+
continue
40+
}
41+
add(ret, getMaxInc(cur, c))
42+
}
43+
val maxCandWithoutInv = nums[cur] + ret[2][0]
44+
val maxCandWithInv = -(totalSum[cur] - ret[0][k - 1]) - ret[1][k - 1]
45+
val minCandWithoutInv = nums[cur] + ret[1][0]
46+
val minCandWithInv = -(totalSum[cur] - ret[0][k - 1]) - ret[2][k - 1]
47+
val res = Array<LongArray>(3) { LongArray(k) }
48+
for (i in 0..<k - 1) {
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] = min(
55+
min(maxCandWithoutInv, maxCandWithInv),
56+
min(minCandWithoutInv, minCandWithInv),
57+
)
58+
res[2][0] = max(
59+
max(maxCandWithoutInv, maxCandWithInv),
60+
max(minCandWithoutInv, minCandWithInv),
61+
)
62+
return res
63+
}
64+
65+
fun subtreeInversionSum(edges: Array<IntArray>, nums: IntArray, k: Int): Long {
66+
totalSum = LongArray(nums.size)
67+
this.nums = nums
68+
nei = ArrayList<MutableList<Int>>()
69+
this.k = k
70+
for (i in nums.indices) {
71+
nei.add(ArrayList<Int>())
72+
}
73+
for (e in edges) {
74+
nei[e[0]].add(e[1])
75+
nei[e[1]].add(e[0])
76+
}
77+
getTotalSum(-1, 0)
78+
val res = getMaxInc(-1, 0)
79+
return res[2][0]
80+
}
81+
}

0 commit comments

Comments
 (0)