Skip to content

Commit 85fb916

Browse files
authored
Added tasks 3142-3149
1 parent 98a6e75 commit 85fb916

File tree

24 files changed

+830
-0
lines changed

24 files changed

+830
-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_170_ms_(91.84%)_Space_37.9_MB_(93.88%)
4+
5+
class Solution {
6+
fun satisfiesConditions(grid: Array<IntArray>): Boolean {
7+
val m = grid.size
8+
val n = grid[0].size
9+
for (i in 0 until m - 1) {
10+
if (n > 1) {
11+
for (j in 0 until n - 1) {
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 (j in 0 until n - 1) {
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,35 @@
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_650_ms_(59.52%)_Space_93.5_MB_(54.76%)
5+
6+
import kotlin.math.abs
7+
import kotlin.math.max
8+
import kotlin.math.min
9+
10+
class Solution {
11+
fun maxPointsInsideSquare(points: Array<IntArray>, s: String): Int {
12+
val tags = IntArray(26)
13+
tags.fill(Int.MAX_VALUE)
14+
var secondMin = Int.MAX_VALUE
15+
for (i in s.indices) {
16+
val dist = max(abs(points[i][0]), abs(points[i][1]))
17+
val c = s[i]
18+
if (tags[c.code - 'a'.code] == Int.MAX_VALUE) {
19+
tags[c.code - 'a'.code] = dist
20+
} else if (dist < tags[c.code - 'a'.code]) {
21+
secondMin = min(secondMin, tags[c.code - 'a'.code])
22+
tags[c.code - 'a'.code] = dist
23+
} else {
24+
secondMin = min(secondMin, dist)
25+
}
26+
}
27+
var count = 0
28+
for (dist in tags) {
29+
if (dist < secondMin) {
30+
count++
31+
}
32+
}
33+
return count
34+
}
35+
}
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_232_ms_(88.00%)_Space_38.9_MB_(48.00%)
5+
6+
import kotlin.math.min
7+
8+
class Solution {
9+
fun minimumSubstringsInPartition(s: String): Int {
10+
val cs = s.toCharArray()
11+
val n = cs.size
12+
val dp = IntArray(n + 1)
13+
dp.fill(n)
14+
dp[0] = 0
15+
for (i in 1..n) {
16+
val count = IntArray(26)
17+
var distinct = 0
18+
var maxCount = 0
19+
for (j in i - 1 downTo 0) {
20+
val index = cs[j].code - 'a'.code
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] = 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,59 @@
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_210_ms_(100.00%)_Space_40_MB_(100.00%)
5+
6+
class Solution {
7+
fun findProductsOfElements(queries: Array<LongArray>): IntArray {
8+
val ans = IntArray(queries.size)
9+
for (i in queries.indices) {
10+
val q = queries[i]
11+
val er = sumE(q[1] + 1)
12+
val el = sumE(q[0])
13+
ans[i] = pow(2, er - el, q[2])
14+
}
15+
return ans
16+
}
17+
18+
private fun sumE(k: Long): Long {
19+
var k = k
20+
var res: Long = 0
21+
var n: Long = 0
22+
var cnt1: Long = 0
23+
var sumI: Long = 0
24+
for (i in 63L - java.lang.Long.numberOfLeadingZeros(k + 1) downTo 1) {
25+
val c = (cnt1 shl i.toInt()) + (i shl (i - 1).toInt())
26+
if (c <= k) {
27+
k -= c
28+
res += (sumI shl i.toInt()) + ((i * (i - 1) / 2) shl (i - 1).toInt())
29+
sumI += i
30+
cnt1++
31+
n = n or (1L shl i.toInt())
32+
}
33+
}
34+
if (cnt1 <= k) {
35+
k -= cnt1
36+
res += sumI
37+
n++
38+
}
39+
while (k-- > 0) {
40+
res += java.lang.Long.numberOfTrailingZeros(n).toLong()
41+
n = n and n - 1
42+
}
43+
return res
44+
}
45+
46+
private fun pow(x: Long, n: Long, mod: Long): Int {
47+
var x = x
48+
var n = n
49+
var res = 1 % mod
50+
while (n > 0) {
51+
if (n % 2 == 1L) {
52+
res = (res * x) % mod
53+
}
54+
x = (x * x) % mod
55+
n /= 2
56+
}
57+
return res.toInt()
58+
}
59+
}
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_177_ms_(58.21%)_Space_38.1_MB_(7.46%)
4+
5+
import kotlin.math.abs
6+
7+
class Solution {
8+
fun findPermutationDifference(s: String, t: String): Int {
9+
val res = IntArray(26)
10+
res.fill(-1)
11+
var sum = 0
12+
for (i in s.indices) {
13+
res[s[i].code - 'a'.code] = i
14+
}
15+
for (i in t.indices) {
16+
sum += abs((res[t[i].code - 'a'.code] - i))
17+
}
18+
return sum
19+
}
20+
}

0 commit comments

Comments
 (0)