Skip to content

Commit ca90bbc

Browse files
committed
Added tasks 3142-3149
1 parent b6e09cb commit ca90bbc

File tree

24 files changed

+788
-0
lines changed

24 files changed

+788
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g3101_3200.s3142_check_if_grid_satisfies_conditions;
2+
3+
// #Easy #Array #Matrix #2024_05_15_Time_1_ms_(95.76%)_Space_44.4_MB_(59.70%)
4+
5+
public class Solution {
6+
public boolean satisfiesConditions(int[][] grid) {
7+
int m = grid.length;
8+
int n = grid[0].length;
9+
for (int i = 0; i < m - 1; i++) {
10+
if (n > 1) {
11+
for (int j = 0; j < n - 1; j++) {
12+
if ((grid[i][j] != grid[i + 1][j]) || (grid[i][j] == grid[i][j + 1])) {
13+
return false;
14+
}
15+
}
16+
} else {
17+
if (grid[i][0] != grid[i + 1][0]) {
18+
return false;
19+
}
20+
}
21+
}
22+
for (int j = 0; j < n - 1; j++) {
23+
if (grid[m - 1][j] == grid[m - 1][j + 1]) {
24+
return false;
25+
}
26+
}
27+
return true;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
3142\. Check if Grid Satisfies Conditions
2+
3+
Easy
4+
5+
You are given a 2D matrix `grid` of size `m x n`. You need to check if each cell `grid[i][j]` is:
6+
7+
* Equal to the cell below it, i.e. `grid[i][j] == grid[i + 1][j]` (if it exists).
8+
* Different from the cell to its right, i.e. `grid[i][j] != grid[i][j + 1]` (if it exists).
9+
10+
Return `true` if **all** the cells satisfy these conditions, otherwise, return `false`.
11+
12+
**Example 1:**
13+
14+
**Input:** grid = [[1,0,2],[1,0,2]]
15+
16+
**Output:** true
17+
18+
**Explanation:**
19+
20+
**![](https://assets.leetcode.com/uploads/2024/04/15/examplechanged.png)**
21+
22+
All the cells in the grid satisfy the conditions.
23+
24+
**Example 2:**
25+
26+
**Input:** grid = [[1,1,1],[0,0,0]]
27+
28+
**Output:** false
29+
30+
**Explanation:**
31+
32+
**![](https://assets.leetcode.com/uploads/2024/03/27/example21.png)**
33+
34+
All cells in the first row are equal.
35+
36+
**Example 3:**
37+
38+
**Input:** grid = [[1],[2],[3]]
39+
40+
**Output:** false
41+
42+
**Explanation:**
43+
44+
![](https://assets.leetcode.com/uploads/2024/03/31/changed.png)
45+
46+
Cells in the first column have different values.
47+
48+
**Constraints:**
49+
50+
* `1 <= n, m <= 10`
51+
* `0 <= grid[i][j] <= 9`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g3101_3200.s3143_maximum_points_inside_the_square;
2+
3+
// #Medium #Array #String #Hash_Table #Sorting #Binary_Search
4+
// #2024_05_15_Time_2_ms_(100.00%)_Space_100.1_MB_(61.27%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
public int maxPointsInsideSquare(int[][] points, String s) {
10+
int[] tags = new int[26];
11+
Arrays.fill(tags, Integer.MAX_VALUE);
12+
int secondMin = Integer.MAX_VALUE;
13+
for (int i = 0; i < s.length(); i++) {
14+
int dist = Math.max(Math.abs(points[i][0]), Math.abs(points[i][1]));
15+
char c = s.charAt(i);
16+
if (tags[c - 'a'] == Integer.MAX_VALUE) {
17+
tags[c - 'a'] = dist;
18+
} else if (dist < tags[c - 'a']) {
19+
secondMin = Math.min(secondMin, tags[c - 'a']);
20+
tags[c - 'a'] = dist;
21+
} else {
22+
secondMin = Math.min(secondMin, dist);
23+
}
24+
}
25+
int count = 0;
26+
for (int dist : tags) {
27+
if (dist < secondMin) {
28+
count++;
29+
}
30+
}
31+
return count;
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
3143\. Maximum Points Inside the Square
2+
3+
Medium
4+
5+
You are given a 2D array `points` and a string `s` where, `points[i]` represents the coordinates of point `i`, and `s[i]` represents the **tag** of point `i`.
6+
7+
A **valid** square is a square centered at the origin `(0, 0)`, has edges parallel to the axes, and **does not** contain two points with the same tag.
8+
9+
Return the **maximum** number of points contained in a **valid** square.
10+
11+
Note:
12+
13+
* A point is considered to be inside the square if it lies on or within the square's boundaries.
14+
* The side length of the square can be zero.
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2024/03/29/3708-tc1.png)
19+
20+
**Input:** points = [[2,2],[-1,-2],[-4,4],[-3,1],[3,-3]], s = "abdca"
21+
22+
**Output:** 2
23+
24+
**Explanation:**
25+
26+
The square of side length 4 covers two points `points[0]` and `points[1]`.
27+
28+
**Example 2:**
29+
30+
![](https://assets.leetcode.com/uploads/2024/03/29/3708-tc2.png)
31+
32+
**Input:** points = [[1,1],[-2,-2],[-2,2]], s = "abb"
33+
34+
**Output:** 1
35+
36+
**Explanation:**
37+
38+
The square of side length 2 covers one point, which is `points[0]`.
39+
40+
**Example 3:**
41+
42+
**Input:** points = [[1,1],[-1,-1],[2,-2]], s = "ccd"
43+
44+
**Output:** 0
45+
46+
**Explanation:**
47+
48+
It's impossible to make any valid squares centered at the origin such that it covers only one point among `points[0]` and `points[1]`.
49+
50+
**Constraints:**
51+
52+
* <code>1 <= s.length, points.length <= 10<sup>5</sup></code>
53+
* `points[i].length == 2`
54+
* <code>-10<sup>9</sup> <= points[i][0], points[i][1] <= 10<sup>9</sup></code>
55+
* `s.length == points.length`
56+
* `points` consists of distinct coordinates.
57+
* `s` consists only of lowercase English letters.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g3101_3200.s3144_minimum_substring_partition_of_equal_character_frequency;
2+
3+
// #Medium #String #Hash_Table #Dynamic_Programming #Counting
4+
// #2024_05_15_Time_37_ms_(100.00%)_Space_44.9_MB_(72.95%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
public int minimumSubstringsInPartition(String s) {
10+
char[] cs = s.toCharArray();
11+
int n = cs.length;
12+
int[] dp = new int[n + 1];
13+
Arrays.fill(dp, n);
14+
dp[0] = 0;
15+
for (int i = 1; i <= n; ++i) {
16+
int[] count = new int[26];
17+
int distinct = 0;
18+
int maxCount = 0;
19+
for (int j = i - 1; j >= 0; --j) {
20+
int index = cs[j] - 'a';
21+
if (++count[index] == 1) {
22+
distinct++;
23+
}
24+
if (count[index] > maxCount) {
25+
maxCount = count[index];
26+
}
27+
if (maxCount * distinct == i - j) {
28+
dp[i] = Math.min(dp[i], dp[j] + 1);
29+
}
30+
}
31+
}
32+
return dp[n];
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
3144\. Minimum Substring Partition of Equal Character Frequency
2+
3+
Medium
4+
5+
Given a string `s`, you need to partition it into one or more **balanced** substrings. For example, if `s == "ababcc"` then `("abab", "c", "c")`, `("ab", "abc", "c")`, and `("ababcc")` are all valid partitions, but <code>("a", **"bab"**, "cc")</code>, <code>(**"aba"**, "bc", "c")</code>, and <code>("ab", **"abcc"**)</code> are not. The unbalanced substrings are bolded.
6+
7+
Return the **minimum** number of substrings that you can partition `s` into.
8+
9+
**Note:** A **balanced** string is a string where each character in the string occurs the same number of times.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "fabccddg"
14+
15+
**Output:** 3
16+
17+
**Explanation:**
18+
19+
We can partition the string `s` into 3 substrings in one of the following ways: `("fab, "ccdd", "g")`, or `("fabc", "cd", "dg")`.
20+
21+
**Example 2:**
22+
23+
**Input:** s = "abababaccddb"
24+
25+
**Output:** 2
26+
27+
**Explanation:**
28+
29+
We can partition the string `s` into 2 substrings like so: `("abab", "abaccddb")`.
30+
31+
**Constraints:**
32+
33+
* `1 <= s.length <= 1000`
34+
* `s` consists only of English lowercase letters.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package g3101_3200.s3145_find_products_of_elements_of_big_array;
2+
3+
// #Hard #Array #Binary_Search #Bit_Manipulation
4+
// #2024_05_15_Time_3_ms_(98.41%)_Space_44.5_MB_(96.83%)
5+
6+
public class Solution {
7+
public int[] findProductsOfElements(long[][] queries) {
8+
int[] ans = new int[queries.length];
9+
for (int i = 0; i < queries.length; i++) {
10+
long[] q = queries[i];
11+
long er = sumE(q[1] + 1);
12+
long el = sumE(q[0]);
13+
ans[i] = pow(2, er - el, q[2]);
14+
}
15+
return ans;
16+
}
17+
18+
private long sumE(long k) {
19+
long res = 0, n = 0, cnt1 = 0, sumI = 0;
20+
for (long i = 63 - Long.numberOfLeadingZeros(k + 1); i > 0; i--) {
21+
long c = (cnt1 << i) + (i << (i - 1));
22+
if (c <= k) {
23+
k -= c;
24+
res += (sumI << i) + ((i * (i - 1) / 2) << (i - 1));
25+
sumI += i;
26+
cnt1++;
27+
n |= 1L << i;
28+
}
29+
}
30+
if (cnt1 <= k) {
31+
k -= cnt1;
32+
res += sumI;
33+
n++;
34+
}
35+
while (k-- > 0) {
36+
res += Long.numberOfTrailingZeros(n);
37+
n &= n - 1;
38+
}
39+
return res;
40+
}
41+
42+
private int pow(long x, long n, long mod) {
43+
long res = 1 % mod;
44+
for (; n > 0; n /= 2) {
45+
if (n % 2 == 1) {
46+
res = (res * x) % mod;
47+
}
48+
x = (x * x) % mod;
49+
}
50+
return (int) res;
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
3145\. Find Products of Elements of Big Array
2+
3+
Hard
4+
5+
A **powerful array** for an integer `x` is the shortest sorted array of powers of two that sum up to `x`. For example, the powerful array for 11 is `[1, 2, 8]`.
6+
7+
The array `big_nums` is created by concatenating the **powerful** arrays for every positive integer `i` in ascending order: 1, 2, 3, and so forth. Thus, `big_nums` starts as <code>[<ins>1</ins>, <ins>2</ins>, <ins>1, 2</ins>, <ins>4</ins>, <ins>1, 4</ins>, <ins>2, 4</ins>, <ins>1, 2, 4</ins>, <ins>8</ins>, ...]</code>.
8+
9+
You are given a 2D integer matrix `queries`, where for <code>queries[i] = [from<sub>i</sub>, to<sub>i</sub>, mod<sub>i</sub>]</code> you should calculate <code>(big_nums[from<sub>i</sub>] * big_nums[from<sub>i</sub> + 1] * ... * big_nums[to<sub>i</sub>]) % mod<sub>i</sub></code>.
10+
11+
Return an integer array `answer` such that `answer[i]` is the answer to the <code>i<sup>th</sup></code> query.
12+
13+
**Example 1:**
14+
15+
**Input:** queries = [[1,3,7]]
16+
17+
**Output:** [4]
18+
19+
**Explanation:**
20+
21+
There is one query.
22+
23+
`big_nums[1..3] = [2,1,2]`. The product of them is 4. The remainder of 4 under 7 is 4.
24+
25+
**Example 2:**
26+
27+
**Input:** queries = [[2,5,3],[7,7,4]]
28+
29+
**Output:** [2,2]
30+
31+
**Explanation:**
32+
33+
There are two queries.
34+
35+
First query: `big_nums[2..5] = [1,2,4,1]`. The product of them is 8. The remainder of 8 under 3 is 2.
36+
37+
Second query: `big_nums[7] = 2`. The remainder of 2 under 4 is 2.
38+
39+
**Constraints:**
40+
41+
* `1 <= queries.length <= 500`
42+
* `queries[i].length == 3`
43+
* <code>0 <= queries[i][0] <= queries[i][1] <= 10<sup>15</sup></code>
44+
* <code>1 <= queries[i][2] <= 10<sup>5</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g3101_3200.s3146_permutation_difference_between_two_strings;
2+
3+
// #Easy #String #Hash_Table #2024_05_15_Time_1_ms_(100.00%)_Space_42.4_MB_(84.38%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int findPermutationDifference(String s, String t) {
9+
int[] res = new int[26];
10+
Arrays.fill(res, -1);
11+
int sum = 0;
12+
for (int i = 0; i < s.length(); ++i) {
13+
res[s.charAt(i) - 'a'] = i;
14+
}
15+
for (int i = 0; i < t.length(); ++i) {
16+
sum += Math.abs(res[t.charAt(i) - 'a'] - i);
17+
}
18+
return sum;
19+
}
20+
}

0 commit comments

Comments
 (0)