Skip to content

Commit ed3b6e9

Browse files
committed
Added tasks 3527-3534
1 parent b436f7c commit ed3b6e9

File tree

24 files changed

+1164
-0
lines changed

24 files changed

+1164
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package g3501_3600.s3527_find_the_most_common_response;
2+
3+
// #Medium #2025_04_27_Time_94_ms_(100.00%)_Space_211.47_MB_(22.16%)
4+
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
public class Solution {
10+
private boolean compareStrings(String str1, String str2) {
11+
int n = str1.length();
12+
int m = str2.length();
13+
int i = 0;
14+
int j = 0;
15+
while (i < n && j < m) {
16+
if (str1.charAt(i) < str2.charAt(j)) {
17+
return true;
18+
} else if (str1.charAt(i) > str2.charAt(j)) {
19+
return false;
20+
}
21+
i++;
22+
j++;
23+
}
24+
return n < m;
25+
}
26+
27+
public String findCommonResponse(List<List<String>> responses) {
28+
int n = responses.size();
29+
Map<String, int[]> mp = new HashMap<>();
30+
String ans = responses.get(0).get(0);
31+
int maxFreq = 0;
32+
for (int row = 0; row < n; row++) {
33+
int m = responses.get(row).size();
34+
for (int col = 0; col < m; col++) {
35+
String resp = responses.get(row).get(col);
36+
int[] arr = mp.getOrDefault(resp, new int[] {0, -1});
37+
if (arr[1] != row) {
38+
arr[0]++;
39+
arr[1] = row;
40+
mp.put(resp, arr);
41+
}
42+
if (arr[0] > maxFreq) {
43+
ans = resp;
44+
maxFreq = arr[0];
45+
} else if (!ans.equals(resp) && arr[0] == maxFreq && compareStrings(resp, ans)) {
46+
ans = resp;
47+
maxFreq = arr[0];
48+
}
49+
}
50+
}
51+
return ans;
52+
}
53+
}
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,15 @@
1+
package g3501_3600.s3528_unit_conversion_i;
2+
3+
// #Medium #2025_04_27_Time_3_ms_(99.90%)_Space_127.63_MB_(29.91%)
4+
5+
public class Solution {
6+
public int[] baseUnitConversions(int[][] conversions) {
7+
int[] arr = new int[conversions.length + 1];
8+
arr[0] = 1;
9+
for (int[] conversion : conversions) {
10+
long val = ((long) arr[conversion[0]] * conversion[2]) % 1000000007;
11+
arr[conversion[1]] = (int) val;
12+
}
13+
return arr;
14+
}
15+
}
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,84 @@
1+
package g3501_3600.s3529_count_cells_in_overlapping_horizontal_and_vertical_substrings;
2+
3+
// #Medium #2025_04_27_Time_32_ms_(100.00%)_Space_64.02_MB_(100.00%)
4+
5+
public class Solution {
6+
public int countCells(char[][] grid, String pattern) {
7+
int k = pattern.length();
8+
int[] lps = makeLps(pattern);
9+
int m = grid.length;
10+
int n = grid[0].length;
11+
int[][] horiPats = new int[m][n];
12+
int[][] vertPats = new int[m][n];
13+
int i = 0;
14+
int j = 0;
15+
while (i < m * n) {
16+
if (grid[i / n][i % n] == pattern.charAt(j)) {
17+
i++;
18+
if (++j == k) {
19+
int d = i - j;
20+
horiPats[d / n][d % n]++;
21+
if (i < m * n) {
22+
horiPats[i / n][i % n]--;
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.charAt(j)) {
37+
i++;
38+
if (++j == k) {
39+
int d = i - j;
40+
vertPats[d % m][d / m]++;
41+
if (i < m * n) {
42+
vertPats[i % m][i / m]--;
43+
}
44+
j = lps[j - 1];
45+
}
46+
} else if (j != 0) {
47+
j = lps[j - 1];
48+
} else {
49+
i++;
50+
}
51+
}
52+
for (i = 1; i < m * n; i++) {
53+
vertPats[i % m][i / m] += vertPats[(i - 1) % m][(i - 1) / m];
54+
horiPats[i / n][i % n] += horiPats[(i - 1) / n][(i - 1) % n];
55+
}
56+
int res = 0;
57+
for (i = 0; i < m; i++) {
58+
for (j = 0; j < n; j++) {
59+
if (horiPats[i][j] > 0 && vertPats[i][j] > 0) {
60+
res++;
61+
}
62+
}
63+
}
64+
return res;
65+
}
66+
67+
private int[] makeLps(String pattern) {
68+
int n = pattern.length();
69+
int[] lps = new int[n];
70+
int len = 0;
71+
int i = 1;
72+
lps[0] = 0;
73+
while (i < n) {
74+
if (pattern.charAt(i) == pattern.charAt(len)) {
75+
lps[i++] = ++len;
76+
} else if (len != 0) {
77+
len = lps[len - 1];
78+
} else {
79+
lps[i++] = 0;
80+
}
81+
}
82+
return lps;
83+
}
84+
}
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,56 @@
1+
package g3501_3600.s3530_maximum_profit_from_valid_topological_order_in_dag;
2+
3+
// #Hard #2025_04_27_Time_2055_ms_(100.00%)_Space_67.18_MB_(100.00%)
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
public class Solution {
10+
private int helper(
11+
int mask,
12+
int pos,
13+
int[] inDegree,
14+
List<List<Integer>> adj,
15+
int[] score,
16+
int[] dp,
17+
int n) {
18+
if (mask == (1 << n) - 1) {
19+
return 0;
20+
}
21+
if (dp[mask] != -1) {
22+
return dp[mask];
23+
}
24+
int res = 0;
25+
for (int i = 0; i < n; i++) {
26+
if ((mask & (1 << i)) == 0 && inDegree[i] == 0) {
27+
for (int ng : adj.get(i)) {
28+
inDegree[ng]--;
29+
}
30+
int val =
31+
pos * score[i]
32+
+ helper(mask | (1 << i), pos + 1, inDegree, adj, score, dp, n);
33+
res = Math.max(res, val);
34+
for (int ng : adj.get(i)) {
35+
inDegree[ng]++;
36+
}
37+
}
38+
}
39+
return dp[mask] = res;
40+
}
41+
42+
public int maxProfit(int n, int[][] edges, int[] score) {
43+
List<List<Integer>> adj = new ArrayList<>();
44+
for (int i = 0; i < n; i++) {
45+
adj.add(new ArrayList<>());
46+
}
47+
int[] inDegree = new int[n];
48+
for (int[] e : edges) {
49+
adj.get(e[0]).add(e[1]);
50+
inDegree[e[1]]++;
51+
}
52+
int[] dp = new int[1 << n];
53+
Arrays.fill(dp, -1);
54+
return helper(0, 1, inDegree, adj, score, dp, n);
55+
}
56+
}

0 commit comments

Comments
 (0)