Skip to content

Commit 95f4181

Browse files
authored
Added tasks 3527-3534
1 parent e725d67 commit 95f4181

File tree

24 files changed

+1219
-0
lines changed

24 files changed

+1219
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package g3501_3600.s3527_find_the_most_common_response
2+
3+
// #Medium #Array #String #Hash_Table #Counting
4+
// #2025_04_27_Time_73_ms_(100.00%)_Space_243.97_MB_(50.00%)
5+
6+
class Solution {
7+
private fun compareStrings(str1: String, str2: String): Boolean {
8+
val n = str1.length
9+
val m = str2.length
10+
var i = 0
11+
var j = 0
12+
while (i < n && j < m) {
13+
if (str1[i] < str2[j]) {
14+
return true
15+
} else if (str1[i] > str2[j]) {
16+
return false
17+
}
18+
i++
19+
j++
20+
}
21+
return n < m
22+
}
23+
24+
fun findCommonResponse(responses: List<List<String>>): String {
25+
val n = responses.size
26+
val mp: MutableMap<String, IntArray> = HashMap()
27+
var ans = responses[0][0]
28+
var maxFreq = 0
29+
for (row in 0..<n) {
30+
val m = responses[row].size
31+
for (col in 0..<m) {
32+
val resp = responses[row][col]
33+
val arr = mp.getOrDefault(resp, intArrayOf(0, -1))
34+
if (arr[1] != row) {
35+
arr[0]++
36+
arr[1] = row
37+
mp.put(resp, arr)
38+
}
39+
if (arr[0] > maxFreq ||
40+
(ans != resp) && arr[0] == maxFreq && compareStrings(resp, ans)
41+
) {
42+
ans = resp
43+
maxFreq = arr[0]
44+
}
45+
}
46+
}
47+
return ans
48+
}
49+
}
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package g3501_3600.s3528_unit_conversion_i
2+
3+
// #Medium #Depth_First_Search #Breadth_First_Search #Graph
4+
// #2025_04_27_Time_3_ms_(100.00%)_Space_128.33_MB_(100.00%)
5+
6+
class Solution {
7+
fun baseUnitConversions(conversions: Array<IntArray>): IntArray {
8+
val arr = IntArray(conversions.size + 1)
9+
arr[0] = 1
10+
for (conversion in conversions) {
11+
val `val` = (arr[conversion[0]].toLong() * conversion[2]) % 1000000007
12+
arr[conversion[1]] = `val`.toInt()
13+
}
14+
return arr
15+
}
16+
}
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,91 @@
1+
package g3501_3600.s3529_count_cells_in_overlapping_horizontal_and_vertical_substrings
2+
3+
// #Medium #Array #String #Matrix #Hash_Function #String_Matching #Rolling_Hash
4+
// #2025_04_27_Time_51_ms_(100.00%)_Space_85.31_MB_(100.00%)
5+
6+
class Solution {
7+
fun countCells(grid: Array<CharArray>, pattern: String): Int {
8+
val k = pattern.length
9+
val lps = makeLps(pattern)
10+
val m = grid.size
11+
val n = grid[0].size
12+
val horiPats = Array(m) { IntArray(n) }
13+
val vertPats = Array(m) { IntArray(n) }
14+
var i = 0
15+
var j = 0
16+
while (i < m * n) {
17+
if (grid[i / n][i % n] == pattern[j]) {
18+
i++
19+
if (++j == k) {
20+
val d = i - j
21+
horiPats[d / n][d % n] = horiPats[d / n][d % n] + 1
22+
if (i < m * n) {
23+
horiPats[i / n][i % n] = horiPats[i / n][i % n] - 1
24+
}
25+
j = lps[j - 1]
26+
}
27+
} else if (j != 0) {
28+
j = lps[j - 1]
29+
} else {
30+
i++
31+
}
32+
}
33+
i = 0
34+
j = 0
35+
// now do vert pattern, use i = 0 to m*n -1 but instead index as grid[i % m][i/m]
36+
while (i < m * n) {
37+
if (grid[i % m][i / m] == pattern[j]) {
38+
i++
39+
if (++j == k) {
40+
val d = i - j
41+
vertPats[d % m][d / m] = vertPats[d % m][d / m] + 1
42+
if (i < m * n) {
43+
vertPats[i % m][i / m] = vertPats[i % m][i / m] - 1
44+
}
45+
j = lps[j - 1]
46+
}
47+
} else if (j != 0) {
48+
j = lps[j - 1]
49+
} else {
50+
i++
51+
}
52+
}
53+
i = 1
54+
while (i < m * n) {
55+
vertPats[i % m][i / m] += vertPats[(i - 1) % m][(i - 1) / m]
56+
horiPats[i / n][i % n] += horiPats[(i - 1) / n][(i - 1) % n]
57+
i++
58+
}
59+
var res = 0
60+
i = 0
61+
while (i < m) {
62+
j = 0
63+
while (j < n) {
64+
if (horiPats[i][j] > 0 && vertPats[i][j] > 0) {
65+
res++
66+
}
67+
j++
68+
}
69+
i++
70+
}
71+
return res
72+
}
73+
74+
private fun makeLps(pattern: String): IntArray {
75+
val n = pattern.length
76+
val lps = IntArray(n)
77+
var len = 0
78+
var i = 1
79+
lps[0] = 0
80+
while (i < n) {
81+
if (pattern[i] == pattern[len]) {
82+
lps[i++] = ++len
83+
} else if (len != 0) {
84+
len = lps[len - 1]
85+
} else {
86+
lps[i++] = 0
87+
}
88+
}
89+
return lps
90+
}
91+
}
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package g3501_3600.s3530_maximum_profit_from_valid_topological_order_in_dag
2+
3+
// #Hard #Array #Dynamic_Programming #Bit_Manipulation #Graph #Bitmask #Topological_Sort
4+
// #2025_04_27_Time_833_ms_(100.00%)_Space_78.65_MB_(100.00%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
private fun helper(
10+
mask: Int,
11+
pos: Int,
12+
inDegree: IntArray,
13+
adj: List<List<Int>>,
14+
score: IntArray,
15+
dp: IntArray,
16+
n: Int,
17+
): Int {
18+
if (mask == (1 shl n) - 1) {
19+
return 0
20+
}
21+
if (dp[mask] != -1) {
22+
return dp[mask]
23+
}
24+
var res = 0
25+
for (i in 0..<n) {
26+
if ((mask and (1 shl i)) == 0 && inDegree[i] == 0) {
27+
for (ng in adj[i]) {
28+
inDegree[ng]--
29+
}
30+
val `val` =
31+
(
32+
pos * score[i] +
33+
helper(mask or (1 shl i), pos + 1, inDegree, adj, score, dp, n)
34+
)
35+
res = max(res, `val`)
36+
for (ng in adj[i]) {
37+
inDegree[ng]++
38+
}
39+
}
40+
}
41+
dp[mask] = res
42+
return res
43+
}
44+
45+
fun maxProfit(n: Int, edges: Array<IntArray>, score: IntArray): Int {
46+
val adj: MutableList<MutableList<Int>> = ArrayList<MutableList<Int>>()
47+
for (i in 0..<n) {
48+
adj.add(ArrayList<Int>())
49+
}
50+
val inDegree = IntArray(n)
51+
for (e in edges) {
52+
adj[e[0]].add(e[1])
53+
inDegree[e[1]]++
54+
}
55+
val dp = IntArray(1 shl n)
56+
dp.fill(-1)
57+
return helper(0, 1, inDegree, adj, score, dp, n)
58+
}
59+
}

0 commit comments

Comments
 (0)