Skip to content

Commit 0286efd

Browse files
committed
Added tasks 3527-3534
1 parent 9aac466 commit 0286efd

File tree

9 files changed

+924
-0
lines changed
  • src/main/kotlin/g3501_3600
    • s3527_find_the_most_common_response
    • s3528_unit_conversion_i
    • s3529_count_cells_in_overlapping_horizontal_and_vertical_substrings
    • s3530_maximum_profit_from_valid_topological_order_in_dag
    • s3531_count_covered_buildings
    • s3532_path_existence_queries_in_a_graph_i
    • s3533_concatenated_divisibility
    • s3534_path_existence_queries_in_a_graph_ii

9 files changed

+924
-0
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,14 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091+
| 3534 |[Path Existence Queries in a Graph II](src/main/kotlin/g3501_3600/s3534_path_existence_queries_in_a_graph_ii)| Hard | Array, Sorting, Greedy, Binary_Search, Graph | 152 | 100.00
2092+
| 3533 |[Concatenated Divisibility](src/main/kotlin/g3501_3600/s3533_concatenated_divisibility)| Hard | Array, Dynamic_Programming, Bit_Manipulation, Bitmask | 20 | 100.00
2093+
| 3532 |[Path Existence Queries in a Graph I](src/main/kotlin/g3501_3600/s3532_path_existence_queries_in_a_graph_i)| Medium | Array, Binary_Search, Graph, Union_Find | 5 | 90.91
2094+
| 3531 |[Count Covered Buildings](src/main/kotlin/g3501_3600/s3531_count_covered_buildings)| Medium | Array, Hash_Table, Sorting | 34 | 100.00
2095+
| 3530 |[Maximum Profit from Valid Topological Order in DAG](src/main/kotlin/g3501_3600/s3530_maximum_profit_from_valid_topological_order_in_dag)| Hard | Array, Dynamic_Programming, Bit_Manipulation, Graph, Bitmask, Topological_Sort | 833 | 100.00
2096+
| 3529 |[Count Cells in Overlapping Horizontal and Vertical Substrings](src/main/kotlin/g3501_3600/s3529_count_cells_in_overlapping_horizontal_and_vertical_substrings)| Medium | Array, String, Matrix, Hash_Function, String_Matching, Rolling_Hash | 51 | 100.00
2097+
| 3528 |[Unit Conversion I](src/main/kotlin/g3501_3600/s3528_unit_conversion_i)| Medium | Depth_First_Search, Breadth_First_Search, Graph | 3 | 100.00
2098+
| 3527 |[Find the Most Common Response](src/main/kotlin/g3501_3600/s3527_find_the_most_common_response)| Medium | Array, String, Hash_Table, Counting | 73 | 100.00
20912099
| 3525 |[Find X Value of Array II](src/main/kotlin/g3501_3600/s3525_find_x_value_of_array_ii)| Hard | Array, Math, Segment_Tree | 237 | 50.00
20922100
| 3524 |[Find X Value of Array I](src/main/kotlin/g3501_3600/s3524_find_x_value_of_array_i)| Medium | Array, Dynamic_Programming, Math | 12 | 100.00
20932101
| 3523 |[Make Array Non-decreasing](src/main/kotlin/g3501_3600/s3523_make_array_non_decreasing)| Medium | Array, Greedy, Stack, Monotonic_Stack | 4 | 75.00
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3527\. Find the Most Common Response
5+
6+
Medium
7+
8+
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.
9+
10+
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.
11+
12+
**Example 1:**
13+
14+
**Input:** responses = \[\["good","ok","good","ok"],["ok","bad","good","ok","ok"],["good"],["bad"]]
15+
16+
**Output:** "good"
17+
18+
**Explanation:**
19+
20+
* After removing duplicates within each list, `responses = \[\["good", "ok"], ["ok", "bad", "good"], ["good"], ["bad"]]`.
21+
* `"good"` appears 3 times, `"ok"` appears 2 times, and `"bad"` appears 2 times.
22+
* Return `"good"` because it has the highest frequency.
23+
24+
**Example 2:**
25+
26+
**Input:** responses = \[\["good","ok","good"],["ok","bad"],["bad","notsure"],["great","good"]]
27+
28+
**Output:** "bad"
29+
30+
**Explanation:**
31+
32+
* After removing duplicates within each list we have `responses = \[\["good", "ok"], ["ok", "bad"], ["bad", "notsure"], ["great", "good"]]`.
33+
* `"bad"`, `"good"`, and `"ok"` each occur 2 times.
34+
* The output is `"bad"` because it is the lexicographically smallest amongst the words with the highest frequency.
35+
36+
**Constraints:**
37+
38+
* `1 <= responses.length <= 1000`
39+
* `1 <= responses[i].length <= 1000`
40+
* `1 <= responses[i][j].length <= 10`
41+
* `responses[i][j]` consists of only lowercase English letters
42+
43+
## Solution
44+
45+
```kotlin
46+
class Solution {
47+
private fun compareStrings(str1: String, str2: String): Boolean {
48+
val n = str1.length
49+
val m = str2.length
50+
var i = 0
51+
var j = 0
52+
while (i < n && j < m) {
53+
if (str1[i] < str2[j]) {
54+
return true
55+
} else if (str1[i] > str2[j]) {
56+
return false
57+
}
58+
i++
59+
j++
60+
}
61+
return n < m
62+
}
63+
64+
fun findCommonResponse(responses: List<List<String>>): String {
65+
val n = responses.size
66+
val mp: MutableMap<String, IntArray> = HashMap()
67+
var ans = responses[0][0]
68+
var maxFreq = 0
69+
for (row in 0..<n) {
70+
val m = responses[row].size
71+
for (col in 0..<m) {
72+
val resp = responses[row][col]
73+
val arr = mp.getOrDefault(resp, intArrayOf(0, -1))
74+
if (arr[1] != row) {
75+
arr[0]++
76+
arr[1] = row
77+
mp.put(resp, arr)
78+
}
79+
if (arr[0] > maxFreq ||
80+
(ans != resp) && arr[0] == maxFreq && compareStrings(resp, ans)
81+
) {
82+
ans = resp
83+
maxFreq = arr[0]
84+
}
85+
}
86+
}
87+
return ans
88+
}
89+
}
90+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3528\. Unit Conversion I
5+
6+
Medium
7+
8+
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>.
9+
10+
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>.
11+
12+
**Example 1:**
13+
14+
**Input:** conversions = \[\[0,1,2],[1,2,3]]
15+
16+
**Output:** [1,2,6]
17+
18+
**Explanation:**
19+
20+
* Convert a single unit of type 0 into 2 units of type 1 using `conversions[0]`.
21+
* Convert a single unit of type 0 into 6 units of type 2 using `conversions[0]`, then `conversions[1]`.
22+
23+
![](https://assets.leetcode.com/uploads/2025/03/12/example1.png)
24+
25+
**Example 2:**
26+
27+
**Input:** conversions = \[\[0,1,2],[0,2,3],[1,3,4],[1,4,5],[2,5,2],[4,6,3],[5,7,4]]
28+
29+
**Output:** [1,2,3,8,10,6,30,24]
30+
31+
**Explanation:**
32+
33+
* Convert a single unit of type 0 into 2 units of type 1 using `conversions[0]`.
34+
* Convert a single unit of type 0 into 3 units of type 2 using `conversions[1]`.
35+
* Convert a single unit of type 0 into 8 units of type 3 using `conversions[0]`, then `conversions[2]`.
36+
* Convert a single unit of type 0 into 10 units of type 4 using `conversions[0]`, then `conversions[3]`.
37+
* Convert a single unit of type 0 into 6 units of type 5 using `conversions[1]`, then `conversions[4]`.
38+
* Convert a single unit of type 0 into 30 units of type 6 using `conversions[0]`, `conversions[3]`, then `conversions[5]`.
39+
* Convert a single unit of type 0 into 24 units of type 7 using `conversions[1]`, `conversions[4]`, then `conversions[6]`.
40+
41+
**Constraints:**
42+
43+
* <code>2 <= n <= 10<sup>5</sup></code>
44+
* `conversions.length == n - 1`
45+
* <code>0 <= sourceUnit<sub>i</sub>, targetUnit<sub>i</sub> < n</code>
46+
* <code>1 <= conversionFactor<sub>i</sub> <= 10<sup>9</sup></code>
47+
* 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.
48+
49+
## Solution
50+
51+
```kotlin
52+
class Solution {
53+
fun baseUnitConversions(conversions: Array<IntArray>): IntArray {
54+
val arr = IntArray(conversions.size + 1)
55+
arr[0] = 1
56+
for (conversion in conversions) {
57+
val `val` = (arr[conversion[0]].toLong() * conversion[2]) % 1000000007
58+
arr[conversion[1]] = `val`.toInt()
59+
}
60+
return arr
61+
}
62+
}
63+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3529\. Count Cells in Overlapping Horizontal and Vertical Substrings
5+
6+
Medium
7+
8+
You are given an `m x n` matrix `grid` consisting of characters and a string `pattern`.
9+
10+
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.
11+
12+
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.
13+
14+
Count the number of cells in the matrix that satisfy the following condition:
15+
16+
* 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`.
17+
18+
Return the count of these cells.
19+
20+
**Example 1:**
21+
22+
![](https://assets.leetcode.com/uploads/2025/03/03/gridtwosubstringsdrawio.png)
23+
24+
**Input:** grid = \[\["a","a","c","c"],["b","b","b","c"],["a","a","b","a"],["c","a","a","c"],["a","a","c","c"]], pattern = "abaca"
25+
26+
**Output:** 1
27+
28+
**Explanation:**
29+
30+
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).
31+
32+
**Example 2:**
33+
34+
![](https://assets.leetcode.com/uploads/2025/03/03/gridexample2fixeddrawio.png)
35+
36+
**Input:** grid = \[\["c","a","a","a"],["a","a","b","a"],["b","b","a","a"],["a","a","b","a"]], pattern = "aba"
37+
38+
**Output:** 4
39+
40+
**Explanation:**
41+
42+
The cells colored above are all part of at least one horizontal and one vertical substring matching the pattern `"aba"`.
43+
44+
**Example 3:**
45+
46+
**Input:** grid = \[\["a"]], pattern = "a"
47+
48+
**Output:** 1
49+
50+
**Constraints:**
51+
52+
* `m == grid.length`
53+
* `n == grid[i].length`
54+
* `1 <= m, n <= 1000`
55+
* <code>1 <= m * n <= 10<sup>5</sup></code>
56+
* `1 <= pattern.length <= m * n`
57+
* `grid` and `pattern` consist of only lowercase English letters.
58+
59+
## Solution
60+
61+
```kotlin
62+
class Solution {
63+
fun countCells(grid: Array<CharArray>, pattern: String): Int {
64+
val k = pattern.length
65+
val lps = makeLps(pattern)
66+
val m = grid.size
67+
val n = grid[0].size
68+
val horiPats = Array(m) { IntArray(n) }
69+
val vertPats = Array(m) { IntArray(n) }
70+
var i = 0
71+
var j = 0
72+
while (i < m * n) {
73+
if (grid[i / n][i % n] == pattern[j]) {
74+
i++
75+
if (++j == k) {
76+
val d = i - j
77+
horiPats[d / n][d % n] = horiPats[d / n][d % n] + 1
78+
if (i < m * n) {
79+
horiPats[i / n][i % n] = horiPats[i / n][i % n] - 1
80+
}
81+
j = lps[j - 1]
82+
}
83+
} else if (j != 0) {
84+
j = lps[j - 1]
85+
} else {
86+
i++
87+
}
88+
}
89+
i = 0
90+
j = 0
91+
// now do vert pattern, use i = 0 to m*n -1 but instead index as grid[i % m][i/m]
92+
while (i < m * n) {
93+
if (grid[i % m][i / m] == pattern[j]) {
94+
i++
95+
if (++j == k) {
96+
val d = i - j
97+
vertPats[d % m][d / m] = vertPats[d % m][d / m] + 1
98+
if (i < m * n) {
99+
vertPats[i % m][i / m] = vertPats[i % m][i / m] - 1
100+
}
101+
j = lps[j - 1]
102+
}
103+
} else if (j != 0) {
104+
j = lps[j - 1]
105+
} else {
106+
i++
107+
}
108+
}
109+
i = 1
110+
while (i < m * n) {
111+
vertPats[i % m][i / m] += vertPats[(i - 1) % m][(i - 1) / m]
112+
horiPats[i / n][i % n] += horiPats[(i - 1) / n][(i - 1) % n]
113+
i++
114+
}
115+
var res = 0
116+
i = 0
117+
while (i < m) {
118+
j = 0
119+
while (j < n) {
120+
if (horiPats[i][j] > 0 && vertPats[i][j] > 0) {
121+
res++
122+
}
123+
j++
124+
}
125+
i++
126+
}
127+
return res
128+
}
129+
130+
private fun makeLps(pattern: String): IntArray {
131+
val n = pattern.length
132+
val lps = IntArray(n)
133+
var len = 0
134+
var i = 1
135+
lps[0] = 0
136+
while (i < n) {
137+
if (pattern[i] == pattern[len]) {
138+
lps[i++] = ++len
139+
} else if (len != 0) {
140+
len = lps[len - 1]
141+
} else {
142+
lps[i++] = 0
143+
}
144+
}
145+
return lps
146+
}
147+
}
148+
```

0 commit comments

Comments
 (0)