forked from javadev/LeetCode-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
793 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
src/main/java/g3101_3200/s3142_check_if_grid_satisfies_conditions/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package g3101_3200.s3142_check_if_grid_satisfies_conditions; | ||
|
||
// #Easy #Array #Matrix #2024_05_15_Time_1_ms_(95.76%)_Space_44.4_MB_(59.70%) | ||
|
||
public class Solution { | ||
public boolean satisfiesConditions(int[][] grid) { | ||
int m = grid.length; | ||
int n = grid[0].length; | ||
for (int i = 0; i < m - 1; i++) { | ||
if (n > 1) { | ||
for (int j = 0; j < n - 1; j++) { | ||
if ((grid[i][j] != grid[i + 1][j]) || (grid[i][j] == grid[i][j + 1])) { | ||
return false; | ||
} | ||
} | ||
} else { | ||
if (grid[i][0] != grid[i + 1][0]) { | ||
return false; | ||
} | ||
} | ||
} | ||
for (int j = 0; j < n - 1; j++) { | ||
if (grid[m - 1][j] == grid[m - 1][j + 1]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/g3101_3200/s3142_check_if_grid_satisfies_conditions/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
3142\. Check if Grid Satisfies Conditions | ||
|
||
Easy | ||
|
||
You are given a 2D matrix `grid` of size `m x n`. You need to check if each cell `grid[i][j]` is: | ||
|
||
* Equal to the cell below it, i.e. `grid[i][j] == grid[i + 1][j]` (if it exists). | ||
* Different from the cell to its right, i.e. `grid[i][j] != grid[i][j + 1]` (if it exists). | ||
|
||
Return `true` if **all** the cells satisfy these conditions, otherwise, return `false`. | ||
|
||
**Example 1:** | ||
|
||
**Input:** grid = [[1,0,2],[1,0,2]] | ||
|
||
**Output:** true | ||
|
||
**Explanation:** | ||
|
||
**![](https://assets.leetcode.com/uploads/2024/04/15/examplechanged.png)** | ||
|
||
All the cells in the grid satisfy the conditions. | ||
|
||
**Example 2:** | ||
|
||
**Input:** grid = [[1,1,1],[0,0,0]] | ||
|
||
**Output:** false | ||
|
||
**Explanation:** | ||
|
||
**![](https://assets.leetcode.com/uploads/2024/03/27/example21.png)** | ||
|
||
All cells in the first row are equal. | ||
|
||
**Example 3:** | ||
|
||
**Input:** grid = [[1],[2],[3]] | ||
|
||
**Output:** false | ||
|
||
**Explanation:** | ||
|
||
![](https://assets.leetcode.com/uploads/2024/03/31/changed.png) | ||
|
||
Cells in the first column have different values. | ||
|
||
**Constraints:** | ||
|
||
* `1 <= n, m <= 10` | ||
* `0 <= grid[i][j] <= 9` |
33 changes: 33 additions & 0 deletions
33
src/main/java/g3101_3200/s3143_maximum_points_inside_the_square/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package g3101_3200.s3143_maximum_points_inside_the_square; | ||
|
||
// #Medium #Array #String #Hash_Table #Sorting #Binary_Search | ||
// #2024_05_15_Time_2_ms_(100.00%)_Space_100.1_MB_(61.27%) | ||
|
||
import java.util.Arrays; | ||
|
||
public class Solution { | ||
public int maxPointsInsideSquare(int[][] points, String s) { | ||
int[] tags = new int[26]; | ||
Arrays.fill(tags, Integer.MAX_VALUE); | ||
int secondMin = Integer.MAX_VALUE; | ||
for (int i = 0; i < s.length(); i++) { | ||
int dist = Math.max(Math.abs(points[i][0]), Math.abs(points[i][1])); | ||
char c = s.charAt(i); | ||
if (tags[c - 'a'] == Integer.MAX_VALUE) { | ||
tags[c - 'a'] = dist; | ||
} else if (dist < tags[c - 'a']) { | ||
secondMin = Math.min(secondMin, tags[c - 'a']); | ||
tags[c - 'a'] = dist; | ||
} else { | ||
secondMin = Math.min(secondMin, dist); | ||
} | ||
} | ||
int count = 0; | ||
for (int dist : tags) { | ||
if (dist < secondMin) { | ||
count++; | ||
} | ||
} | ||
return count; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/g3101_3200/s3143_maximum_points_inside_the_square/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
3143\. Maximum Points Inside the Square | ||
|
||
Medium | ||
|
||
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`. | ||
|
||
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. | ||
|
||
Return the **maximum** number of points contained in a **valid** square. | ||
|
||
Note: | ||
|
||
* A point is considered to be inside the square if it lies on or within the square's boundaries. | ||
* The side length of the square can be zero. | ||
|
||
**Example 1:** | ||
|
||
![](https://assets.leetcode.com/uploads/2024/03/29/3708-tc1.png) | ||
|
||
**Input:** points = [[2,2],[-1,-2],[-4,4],[-3,1],[3,-3]], s = "abdca" | ||
|
||
**Output:** 2 | ||
|
||
**Explanation:** | ||
|
||
The square of side length 4 covers two points `points[0]` and `points[1]`. | ||
|
||
**Example 2:** | ||
|
||
![](https://assets.leetcode.com/uploads/2024/03/29/3708-tc2.png) | ||
|
||
**Input:** points = [[1,1],[-2,-2],[-2,2]], s = "abb" | ||
|
||
**Output:** 1 | ||
|
||
**Explanation:** | ||
|
||
The square of side length 2 covers one point, which is `points[0]`. | ||
|
||
**Example 3:** | ||
|
||
**Input:** points = [[1,1],[-1,-1],[2,-2]], s = "ccd" | ||
|
||
**Output:** 0 | ||
|
||
**Explanation:** | ||
|
||
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]`. | ||
|
||
**Constraints:** | ||
|
||
* <code>1 <= s.length, points.length <= 10<sup>5</sup></code> | ||
* `points[i].length == 2` | ||
* <code>-10<sup>9</sup> <= points[i][0], points[i][1] <= 10<sup>9</sup></code> | ||
* `s.length == points.length` | ||
* `points` consists of distinct coordinates. | ||
* `s` consists only of lowercase English letters. |
34 changes: 34 additions & 0 deletions
34
...a/g3101_3200/s3144_minimum_substring_partition_of_equal_character_frequency/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package g3101_3200.s3144_minimum_substring_partition_of_equal_character_frequency; | ||
|
||
// #Medium #String #Hash_Table #Dynamic_Programming #Counting | ||
// #2024_05_15_Time_37_ms_(100.00%)_Space_44.9_MB_(72.95%) | ||
|
||
import java.util.Arrays; | ||
|
||
public class Solution { | ||
public int minimumSubstringsInPartition(String s) { | ||
char[] cs = s.toCharArray(); | ||
int n = cs.length; | ||
int[] dp = new int[n + 1]; | ||
Arrays.fill(dp, n); | ||
dp[0] = 0; | ||
for (int i = 1; i <= n; ++i) { | ||
int[] count = new int[26]; | ||
int distinct = 0; | ||
int maxCount = 0; | ||
for (int j = i - 1; j >= 0; --j) { | ||
int index = cs[j] - 'a'; | ||
if (++count[index] == 1) { | ||
distinct++; | ||
} | ||
if (count[index] > maxCount) { | ||
maxCount = count[index]; | ||
} | ||
if (maxCount * distinct == i - j) { | ||
dp[i] = Math.min(dp[i], dp[j] + 1); | ||
} | ||
} | ||
} | ||
return dp[n]; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...1_3200/s3144_minimum_substring_partition_of_equal_character_frequency/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
3144\. Minimum Substring Partition of Equal Character Frequency | ||
|
||
Medium | ||
|
||
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. | ||
|
||
Return the **minimum** number of substrings that you can partition `s` into. | ||
|
||
**Note:** A **balanced** string is a string where each character in the string occurs the same number of times. | ||
|
||
**Example 1:** | ||
|
||
**Input:** s = "fabccddg" | ||
|
||
**Output:** 3 | ||
|
||
**Explanation:** | ||
|
||
We can partition the string `s` into 3 substrings in one of the following ways: `("fab, "ccdd", "g")`, or `("fabc", "cd", "dg")`. | ||
|
||
**Example 2:** | ||
|
||
**Input:** s = "abababaccddb" | ||
|
||
**Output:** 2 | ||
|
||
**Explanation:** | ||
|
||
We can partition the string `s` into 2 substrings like so: `("abab", "abaccddb")`. | ||
|
||
**Constraints:** | ||
|
||
* `1 <= s.length <= 1000` | ||
* `s` consists only of English lowercase letters. |
55 changes: 55 additions & 0 deletions
55
src/main/java/g3101_3200/s3145_find_products_of_elements_of_big_array/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package g3101_3200.s3145_find_products_of_elements_of_big_array; | ||
|
||
// #Hard #Array #Binary_Search #Bit_Manipulation | ||
// #2024_05_15_Time_3_ms_(98.41%)_Space_44.5_MB_(96.83%) | ||
|
||
public class Solution { | ||
public int[] findProductsOfElements(long[][] queries) { | ||
int[] ans = new int[queries.length]; | ||
for (int i = 0; i < queries.length; i++) { | ||
long[] q = queries[i]; | ||
long er = sumE(q[1] + 1); | ||
long el = sumE(q[0]); | ||
ans[i] = pow(2, er - el, q[2]); | ||
} | ||
return ans; | ||
} | ||
|
||
private long sumE(long k) { | ||
long res = 0; | ||
long n = 0; | ||
long cnt1 = 0; | ||
long sumI = 0; | ||
for (long i = 63L - Long.numberOfLeadingZeros(k + 1); i > 0; i--) { | ||
long c = (cnt1 << i) + (i << (i - 1)); | ||
if (c <= k) { | ||
k -= c; | ||
res += (sumI << i) + ((i * (i - 1) / 2) << (i - 1)); | ||
sumI += i; | ||
cnt1++; | ||
n |= 1L << i; | ||
} | ||
} | ||
if (cnt1 <= k) { | ||
k -= cnt1; | ||
res += sumI; | ||
n++; | ||
} | ||
while (k-- > 0) { | ||
res += Long.numberOfTrailingZeros(n); | ||
n &= n - 1; | ||
} | ||
return res; | ||
} | ||
|
||
private int pow(long x, long n, long mod) { | ||
long res = 1 % mod; | ||
for (; n > 0; n /= 2) { | ||
if (n % 2 == 1) { | ||
res = (res * x) % mod; | ||
} | ||
x = (x * x) % mod; | ||
} | ||
return (int) res; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/g3101_3200/s3145_find_products_of_elements_of_big_array/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
3145\. Find Products of Elements of Big Array | ||
|
||
Hard | ||
|
||
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]`. | ||
|
||
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>. | ||
|
||
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>. | ||
|
||
Return an integer array `answer` such that `answer[i]` is the answer to the <code>i<sup>th</sup></code> query. | ||
|
||
**Example 1:** | ||
|
||
**Input:** queries = [[1,3,7]] | ||
|
||
**Output:** [4] | ||
|
||
**Explanation:** | ||
|
||
There is one query. | ||
|
||
`big_nums[1..3] = [2,1,2]`. The product of them is 4. The remainder of 4 under 7 is 4. | ||
|
||
**Example 2:** | ||
|
||
**Input:** queries = [[2,5,3],[7,7,4]] | ||
|
||
**Output:** [2,2] | ||
|
||
**Explanation:** | ||
|
||
There are two queries. | ||
|
||
First query: `big_nums[2..5] = [1,2,4,1]`. The product of them is 8. The remainder of 8 under 3 is 2. | ||
|
||
Second query: `big_nums[7] = 2`. The remainder of 2 under 4 is 2. | ||
|
||
**Constraints:** | ||
|
||
* `1 <= queries.length <= 500` | ||
* `queries[i].length == 3` | ||
* <code>0 <= queries[i][0] <= queries[i][1] <= 10<sup>15</sup></code> | ||
* <code>1 <= queries[i][2] <= 10<sup>5</sup></code> |
20 changes: 20 additions & 0 deletions
20
src/main/java/g3101_3200/s3146_permutation_difference_between_two_strings/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package g3101_3200.s3146_permutation_difference_between_two_strings; | ||
|
||
// #Easy #String #Hash_Table #2024_05_15_Time_1_ms_(100.00%)_Space_42.4_MB_(84.38%) | ||
|
||
import java.util.Arrays; | ||
|
||
public class Solution { | ||
public int findPermutationDifference(String s, String t) { | ||
int[] res = new int[26]; | ||
Arrays.fill(res, -1); | ||
int sum = 0; | ||
for (int i = 0; i < s.length(); ++i) { | ||
res[s.charAt(i) - 'a'] = i; | ||
} | ||
for (int i = 0; i < t.length(); ++i) { | ||
sum += Math.abs(res[t.charAt(i) - 'a'] - i); | ||
} | ||
return sum; | ||
} | ||
} |
Oops, something went wrong.