Skip to content

Commit a57cce3

Browse files
committed
Added tasks 3527-3534
1 parent 8600baa commit a57cce3

File tree

9 files changed

+916
-0
lines changed
  • src/main/java/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

+916
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
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/java/g3501_3600/s3534_path_existence_queries_in_a_graph_ii)| Hard | Array, Sorting, Greedy, Binary_Search, Graph | 84 | 100.00
2092+
| 3533 |[Concatenated Divisibility](src/main/java/g3501_3600/s3533_concatenated_divisibility)| Hard | Array, Dynamic_Programming, Bit_Manipulation, Bitmask | 14 | 100.00
2093+
| 3532 |[Path Existence Queries in a Graph I](src/main/java/g3501_3600/s3532_path_existence_queries_in_a_graph_i)| Medium | Array, Binary_Search, Graph, Union_Find | 3 | 100.00
2094+
| 3531 |[Count Covered Buildings](src/main/java/g3501_3600/s3531_count_covered_buildings)| Medium | Array, Hash_Table, Sorting | 12 | 100.00
2095+
| 3530 |[Maximum Profit from Valid Topological Order in DAG](src/main/java/g3501_3600/s3530_maximum_profit_from_valid_topological_order_in_dag)| Hard | Array, Dynamic_Programming, Bit_Manipulation, Graph, Bitmask, Topological_Sort | 1927 | 100.00
2096+
| 3529 |[Count Cells in Overlapping Horizontal and Vertical Substrings](src/main/java/g3501_3600/s3529_count_cells_in_overlapping_horizontal_and_vertical_substrings)| Medium | Array, String, Matrix, Hash_Function, String_Matching, Rolling_Hash | 33 | 100.00
2097+
| 3528 |[Unit Conversion I](src/main/java/g3501_3600/s3528_unit_conversion_i)| Medium | Depth_First_Search, Breadth_First_Search, Graph | 3 | 99.90
2098+
| 3527 |[Find the Most Common Response](src/main/java/g3501_3600/s3527_find_the_most_common_response)| Medium | Array, String, Hash_Table, Counting | 94 | 100.00
20912099
| 3525 |[Find X Value of Array II](src/main/java/g3501_3600/s3525_find_x_value_of_array_ii)| Hard | Array, Math, Segment_Tree | 177 | 79.87
20922100
| 3524 |[Find X Value of Array I](src/main/java/g3501_3600/s3524_find_x_value_of_array_i)| Medium | Array, Dynamic_Programming, Math | 12 | 95.54
20932101
| 3523 |[Make Array Non-decreasing](src/main/java/g3501_3600/s3523_make_array_non_decreasing)| Medium | Array, Greedy, Stack, Monotonic_Stack | 3 | 63.29
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/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+
```java
46+
import java.util.HashMap;
47+
import java.util.List;
48+
import java.util.Map;
49+
50+
public class Solution {
51+
private boolean compareStrings(String str1, String str2) {
52+
int n = str1.length();
53+
int m = str2.length();
54+
int i = 0;
55+
int j = 0;
56+
while (i < n && j < m) {
57+
if (str1.charAt(i) < str2.charAt(j)) {
58+
return true;
59+
} else if (str1.charAt(i) > str2.charAt(j)) {
60+
return false;
61+
}
62+
i++;
63+
j++;
64+
}
65+
return n < m;
66+
}
67+
68+
public String findCommonResponse(List<List<String>> responses) {
69+
int n = responses.size();
70+
Map<String, int[]> mp = new HashMap<>();
71+
String ans = responses.get(0).get(0);
72+
int maxFreq = 0;
73+
for (int row = 0; row < n; row++) {
74+
int m = responses.get(row).size();
75+
for (int col = 0; col < m; col++) {
76+
String resp = responses.get(row).get(col);
77+
int[] arr = mp.getOrDefault(resp, new int[] {0, -1});
78+
if (arr[1] != row) {
79+
arr[0]++;
80+
arr[1] = row;
81+
mp.put(resp, arr);
82+
}
83+
if (arr[0] > maxFreq
84+
|| !ans.equals(resp) && arr[0] == maxFreq && compareStrings(resp, ans)) {
85+
ans = resp;
86+
maxFreq = arr[0];
87+
}
88+
}
89+
}
90+
return ans;
91+
}
92+
}
93+
```
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/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+
```java
52+
public class Solution {
53+
public int[] baseUnitConversions(int[][] conversions) {
54+
int[] arr = new int[conversions.length + 1];
55+
arr[0] = 1;
56+
for (int[] conversion : conversions) {
57+
long val = ((long) arr[conversion[0]] * conversion[2]) % 1000000007;
58+
arr[conversion[1]] = (int) val;
59+
}
60+
return arr;
61+
}
62+
}
63+
```
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/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+
```java
62+
public class Solution {
63+
public int countCells(char[][] grid, String pattern) {
64+
int k = pattern.length();
65+
int[] lps = makeLps(pattern);
66+
int m = grid.length;
67+
int n = grid[0].length;
68+
int[][] horiPats = new int[m][n];
69+
int[][] vertPats = new int[m][n];
70+
int i = 0;
71+
int j = 0;
72+
while (i < m * n) {
73+
if (grid[i / n][i % n] == pattern.charAt(j)) {
74+
i++;
75+
if (++j == k) {
76+
int d = i - j;
77+
horiPats[d / n][d % n]++;
78+
if (i < m * n) {
79+
horiPats[i / n][i % n]--;
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.charAt(j)) {
94+
i++;
95+
if (++j == k) {
96+
int d = i - j;
97+
vertPats[d % m][d / m]++;
98+
if (i < m * n) {
99+
vertPats[i % m][i / m]--;
100+
}
101+
j = lps[j - 1];
102+
}
103+
} else if (j != 0) {
104+
j = lps[j - 1];
105+
} else {
106+
i++;
107+
}
108+
}
109+
for (i = 1; i < m * n; i++) {
110+
vertPats[i % m][i / m] += vertPats[(i - 1) % m][(i - 1) / m];
111+
horiPats[i / n][i % n] += horiPats[(i - 1) / n][(i - 1) % n];
112+
}
113+
int res = 0;
114+
for (i = 0; i < m; i++) {
115+
for (j = 0; j < n; j++) {
116+
if (horiPats[i][j] > 0 && vertPats[i][j] > 0) {
117+
res++;
118+
}
119+
}
120+
}
121+
return res;
122+
}
123+
124+
private int[] makeLps(String pattern) {
125+
int n = pattern.length();
126+
int[] lps = new int[n];
127+
int len = 0;
128+
int i = 1;
129+
lps[0] = 0;
130+
while (i < n) {
131+
if (pattern.charAt(i) == pattern.charAt(len)) {
132+
lps[i++] = ++len;
133+
} else if (len != 0) {
134+
len = lps[len - 1];
135+
} else {
136+
lps[i++] = 0;
137+
}
138+
}
139+
return lps;
140+
}
141+
}
142+
```

0 commit comments

Comments
 (0)