Skip to content

Commit 0641cb7

Browse files
committed
Added tasks 3527-3534
1 parent e725d67 commit 0641cb7

File tree

24 files changed

+1245
-0
lines changed

24 files changed

+1245
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g3501_3600.s3527_find_the_most_common_response
2+
3+
// #Medium #2025_04_27_Time_73_ms_(100.00%)_Space_243.97_MB_(50.00%)
4+
5+
class Solution {
6+
private fun compareStrings(str1: String, str2: String): Boolean {
7+
val n = str1.length
8+
val m = str2.length
9+
var i = 0
10+
var j = 0
11+
while (i < n && j < m) {
12+
if (str1[i] < str2[j]) {
13+
return true
14+
} else if (str1[i] > str2[j]) {
15+
return false
16+
}
17+
i++
18+
j++
19+
}
20+
return n < m
21+
}
22+
23+
fun findCommonResponse(responses: List<List<String>>): String {
24+
val n = responses.size
25+
val mp: MutableMap<String, IntArray> = HashMap()
26+
var ans = responses[0][0]
27+
var maxFreq = 0
28+
for (row in 0..<n) {
29+
val m = responses[row].size
30+
for (col in 0..<m) {
31+
val resp = responses[row][col]
32+
val arr = mp.getOrDefault(resp, intArrayOf(0, -1))
33+
if (arr[1] != row) {
34+
arr[0]++
35+
arr[1] = row
36+
mp.put(resp, arr)
37+
}
38+
if (arr[0] > maxFreq ||
39+
(ans != resp) && arr[0] == maxFreq && compareStrings(resp, ans)
40+
) {
41+
ans = resp
42+
maxFreq = arr[0]
43+
}
44+
}
45+
}
46+
return ans
47+
}
48+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
3527\. Find the Most Common Response
2+
3+
Medium
4+
5+
You are given a 2D string array `responses` where each `responses[i]` is an array of strings representing survey responses from the <code>i<sup>th</sup></code> day.
6+
7+
Return the **most common** response across all days after removing **duplicate** responses within each `responses[i]`. If there is a tie, return the _lexicographically smallest_ response.
8+
9+
**Example 1:**
10+
11+
**Input:** responses = [["good","ok","good","ok"],["ok","bad","good","ok","ok"],["good"],["bad"]]
12+
13+
**Output:** "good"
14+
15+
**Explanation:**
16+
17+
* After removing duplicates within each list, `responses = [["good", "ok"], ["ok", "bad", "good"], ["good"], ["bad"]]`.
18+
* `"good"` appears 3 times, `"ok"` appears 2 times, and `"bad"` appears 2 times.
19+
* Return `"good"` because it has the highest frequency.
20+
21+
**Example 2:**
22+
23+
**Input:** responses = [["good","ok","good"],["ok","bad"],["bad","notsure"],["great","good"]]
24+
25+
**Output:** "bad"
26+
27+
**Explanation:**
28+
29+
* After removing duplicates within each list we have `responses = [["good", "ok"], ["ok", "bad"], ["bad", "notsure"], ["great", "good"]]`.
30+
* `"bad"`, `"good"`, and `"ok"` each occur 2 times.
31+
* The output is `"bad"` because it is the lexicographically smallest amongst the words with the highest frequency.
32+
33+
**Constraints:**
34+
35+
* `1 <= responses.length <= 1000`
36+
* `1 <= responses[i].length <= 1000`
37+
* `1 <= responses[i][j].length <= 10`
38+
* `responses[i][j]` consists of only lowercase English letters
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package g3501_3600.s3528_unit_conversion_i
2+
3+
// #Medium #2025_04_27_Time_3_ms_(100.00%)_Space_128.33_MB_(100.00%)
4+
5+
class Solution {
6+
fun baseUnitConversions(conversions: Array<IntArray>): IntArray {
7+
val arr = IntArray(conversions.size + 1)
8+
arr[0] = 1
9+
for (conversion in conversions) {
10+
val `val` = (arr[conversion[0]].toLong() * conversion[2]) % 1000000007
11+
arr[conversion[1]] = `val`.toInt()
12+
}
13+
return arr
14+
}
15+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
3528\. Unit Conversion I
2+
3+
Medium
4+
5+
There are `n` types of units indexed from `0` to `n - 1`. You are given a 2D integer array `conversions` of length `n - 1`, where <code>conversions[i] = [sourceUnit<sub>i</sub>, targetUnit<sub>i</sub>, conversionFactor<sub>i</sub>]</code>. This indicates that a single unit of type <code>sourceUnit<sub>i</sub></code> is equivalent to <code>conversionFactor<sub>i</sub></code> units of type <code>targetUnit<sub>i</sub></code>.
6+
7+
Return an array `baseUnitConversion` of length `n`, where `baseUnitConversion[i]` is the number of units of type `i` equivalent to a single unit of type 0. Since the answer may be large, return each `baseUnitConversion[i]` **modulo** <code>10<sup>9</sup> + 7</code>.
8+
9+
**Example 1:**
10+
11+
**Input:** conversions = [[0,1,2],[1,2,3]]
12+
13+
**Output:** [1,2,6]
14+
15+
**Explanation:**
16+
17+
* Convert a single unit of type 0 into 2 units of type 1 using `conversions[0]`.
18+
* Convert a single unit of type 0 into 6 units of type 2 using `conversions[0]`, then `conversions[1]`.
19+
20+
![](https://assets.leetcode.com/uploads/2025/03/12/example1.png)
21+
22+
**Example 2:**
23+
24+
**Input:** conversions = [[0,1,2],[0,2,3],[1,3,4],[1,4,5],[2,5,2],[4,6,3],[5,7,4]]
25+
26+
**Output:** [1,2,3,8,10,6,30,24]
27+
28+
**Explanation:**
29+
30+
* Convert a single unit of type 0 into 2 units of type 1 using `conversions[0]`.
31+
* Convert a single unit of type 0 into 3 units of type 2 using `conversions[1]`.
32+
* Convert a single unit of type 0 into 8 units of type 3 using `conversions[0]`, then `conversions[2]`.
33+
* Convert a single unit of type 0 into 10 units of type 4 using `conversions[0]`, then `conversions[3]`.
34+
* Convert a single unit of type 0 into 6 units of type 5 using `conversions[1]`, then `conversions[4]`.
35+
* Convert a single unit of type 0 into 30 units of type 6 using `conversions[0]`, `conversions[3]`, then `conversions[5]`.
36+
* Convert a single unit of type 0 into 24 units of type 7 using `conversions[1]`, `conversions[4]`, then `conversions[6]`.
37+
38+
**Constraints:**
39+
40+
* <code>2 <= n <= 10<sup>5</sup></code>
41+
* `conversions.length == n - 1`
42+
* <code>0 <= sourceUnit<sub>i</sub>, targetUnit<sub>i</sub> < n</code>
43+
* <code>1 <= conversionFactor<sub>i</sub> <= 10<sup>9</sup></code>
44+
* It is guaranteed that unit 0 can be converted into any other unit through a **unique** combination of conversions without using any conversions in the opposite direction.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package g3501_3600.s3529_count_cells_in_overlapping_horizontal_and_vertical_substrings
2+
3+
// #Medium #2025_04_27_Time_51_ms_(100.00%)_Space_85.31_MB_(100.00%)
4+
5+
class Solution {
6+
fun countCells(grid: Array<CharArray>, pattern: String): Int {
7+
val k = pattern.length
8+
val lps = makeLps(pattern)
9+
val m = grid.size
10+
val n = grid[0].size
11+
val horiPats = Array(m) { IntArray(n) }
12+
val vertPats = Array(m) { IntArray(n) }
13+
var i = 0
14+
var j = 0
15+
while (i < m * n) {
16+
if (grid[i / n][i % n] == pattern[j]) {
17+
i++
18+
if (++j == k) {
19+
val d = i - j
20+
horiPats[d / n][d % n] = horiPats[d / n][d % n] + 1
21+
if (i < m * n) {
22+
horiPats[i / n][i % n] = horiPats[i / n][i % n] - 1
23+
}
24+
j = lps[j - 1]
25+
}
26+
} else if (j != 0) {
27+
j = lps[j - 1]
28+
} else {
29+
i++
30+
}
31+
}
32+
i = 0
33+
j = 0
34+
// now do vert pattern, use i = 0 to m*n -1 but instead index as grid[i % m][i/m]
35+
while (i < m * n) {
36+
if (grid[i % m][i / m] == pattern[j]) {
37+
i++
38+
if (++j == k) {
39+
val d = i - j
40+
vertPats[d % m][d / m] = vertPats[d % m][d / m] + 1
41+
if (i < m * n) {
42+
vertPats[i % m][i / m] = vertPats[i % m][i / m] - 1
43+
}
44+
j = lps[j - 1]
45+
}
46+
} else if (j != 0) {
47+
j = lps[j - 1]
48+
} else {
49+
i++
50+
}
51+
}
52+
i = 1
53+
while (i < m * n) {
54+
vertPats[i % m][i / m] += vertPats[(i - 1) % m][(i - 1) / m]
55+
horiPats[i / n][i % n] += horiPats[(i - 1) / n][(i - 1) % n]
56+
i++
57+
}
58+
var res = 0
59+
i = 0
60+
while (i < m) {
61+
j = 0
62+
while (j < n) {
63+
if (horiPats[i][j] > 0 && vertPats[i][j] > 0) {
64+
res++
65+
}
66+
j++
67+
}
68+
i++
69+
}
70+
return res
71+
}
72+
73+
private fun makeLps(pattern: String): IntArray {
74+
val n = pattern.length
75+
val lps = IntArray(n)
76+
var len = 0
77+
var i = 1
78+
lps[0] = 0
79+
while (i < n) {
80+
if (pattern[i] == pattern[len]) {
81+
lps[i++] = ++len
82+
} else if (len != 0) {
83+
len = lps[len - 1]
84+
} else {
85+
lps[i++] = 0
86+
}
87+
}
88+
return lps
89+
}
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
3529\. Count Cells in Overlapping Horizontal and Vertical Substrings
2+
3+
Medium
4+
5+
You are given an `m x n` matrix `grid` consisting of characters and a string `pattern`.
6+
7+
A **horizontal substring** is a contiguous sequence of characters read from left to right. If the end of a row is reached before the substring is complete, it wraps to the first column of the next row and continues as needed. You do **not** wrap from the bottom row back to the top.
8+
9+
A **vertical substring** is a contiguous sequence of characters read from top to bottom. If the bottom of a column is reached before the substring is complete, it wraps to the first row of the next column and continues as needed. You do **not** wrap from the last column back to the first.
10+
11+
Count the number of cells in the matrix that satisfy the following condition:
12+
13+
* The cell must be part of **at least** one horizontal substring and **at least** one vertical substring, where **both** substrings are equal to the given `pattern`.
14+
15+
Return the count of these cells.
16+
17+
**Example 1:**
18+
19+
![](https://assets.leetcode.com/uploads/2025/03/03/gridtwosubstringsdrawio.png)
20+
21+
**Input:** grid = [["a","a","c","c"],["b","b","b","c"],["a","a","b","a"],["c","a","a","c"],["a","a","c","c"]], pattern = "abaca"
22+
23+
**Output:** 1
24+
25+
**Explanation:**
26+
27+
The pattern `"abaca"` appears once as a horizontal substring (colored blue) and once as a vertical substring (colored red), intersecting at one cell (colored purple).
28+
29+
**Example 2:**
30+
31+
![](https://assets.leetcode.com/uploads/2025/03/03/gridexample2fixeddrawio.png)
32+
33+
**Input:** grid = [["c","a","a","a"],["a","a","b","a"],["b","b","a","a"],["a","a","b","a"]], pattern = "aba"
34+
35+
**Output:** 4
36+
37+
**Explanation:**
38+
39+
The cells colored above are all part of at least one horizontal and one vertical substring matching the pattern `"aba"`.
40+
41+
**Example 3:**
42+
43+
**Input:** grid = [["a"]], pattern = "a"
44+
45+
**Output:** 1
46+
47+
**Constraints:**
48+
49+
* `m == grid.length`
50+
* `n == grid[i].length`
51+
* `1 <= m, n <= 1000`
52+
* <code>1 <= m * n <= 10<sup>5</sup></code>
53+
* `1 <= pattern.length <= m * n`
54+
* `grid` and `pattern` consist of only lowercase English letters.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package g3501_3600.s3530_maximum_profit_from_valid_topological_order_in_dag
2+
3+
// #Hard #2025_04_27_Time_833_ms_(100.00%)_Space_78.65_MB_(100.00%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
private fun helper(
9+
mask: Int,
10+
pos: Int,
11+
inDegree: IntArray,
12+
adj: List<List<Int>>,
13+
score: IntArray,
14+
dp: IntArray,
15+
n: Int,
16+
): Int {
17+
if (mask == (1 shl n) - 1) {
18+
return 0
19+
}
20+
if (dp[mask] != -1) {
21+
return dp[mask]
22+
}
23+
var res = 0
24+
for (i in 0..<n) {
25+
if ((mask and (1 shl i)) == 0 && inDegree[i] == 0) {
26+
for (ng in adj[i]) {
27+
inDegree[ng]--
28+
}
29+
val `val` =
30+
(
31+
pos * score[i] +
32+
helper(mask or (1 shl i), pos + 1, inDegree, adj, score, dp, n)
33+
)
34+
res = max(res, `val`)
35+
for (ng in adj[i]) {
36+
inDegree[ng]++
37+
}
38+
}
39+
}
40+
dp[mask] = res
41+
return res
42+
}
43+
44+
fun maxProfit(n: Int, edges: Array<IntArray>, score: IntArray): Int {
45+
val adj: MutableList<MutableList<Int>> = ArrayList<MutableList<Int>>()
46+
for (i in 0..<n) {
47+
adj.add(ArrayList<Int>())
48+
}
49+
val inDegree = IntArray(n)
50+
for (e in edges) {
51+
adj[e[0]].add(e[1])
52+
inDegree[e[1]]++
53+
}
54+
val dp = IntArray(1 shl n)
55+
dp.fill(-1)
56+
return helper(0, 1, inDegree, adj, score, dp, n)
57+
}
58+
}

0 commit comments

Comments
 (0)