Skip to content

Commit

Permalink
Added tasks 3142-3149
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev authored May 15, 2024
1 parent 98a6e75 commit 85fb916
Show file tree
Hide file tree
Showing 24 changed files with 830 additions and 0 deletions.
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_170_ms_(91.84%)_Space_37.9_MB_(93.88%)

class Solution {
fun satisfiesConditions(grid: Array<IntArray>): Boolean {
val m = grid.size
val n = grid[0].size
for (i in 0 until m - 1) {
if (n > 1) {
for (j in 0 until n - 1) {
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 (j in 0 until n - 1) {
if (grid[m - 1][j] == grid[m - 1][j + 1]) {
return false
}
}
return true
}
}
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`
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package g3101_3200.s3143_maximum_points_inside_the_square

// #Medium #Array #String #Hash_Table #Sorting #Binary_Search
// #2024_05_15_Time_650_ms_(59.52%)_Space_93.5_MB_(54.76%)

import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min

class Solution {
fun maxPointsInsideSquare(points: Array<IntArray>, s: String): Int {
val tags = IntArray(26)
tags.fill(Int.MAX_VALUE)
var secondMin = Int.MAX_VALUE
for (i in s.indices) {
val dist = max(abs(points[i][0]), abs(points[i][1]))
val c = s[i]
if (tags[c.code - 'a'.code] == Int.MAX_VALUE) {
tags[c.code - 'a'.code] = dist
} else if (dist < tags[c.code - 'a'.code]) {
secondMin = min(secondMin, tags[c.code - 'a'.code])
tags[c.code - 'a'.code] = dist
} else {
secondMin = min(secondMin, dist)
}
}
var count = 0
for (dist in tags) {
if (dist < secondMin) {
count++
}
}
return count
}
}
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.
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_232_ms_(88.00%)_Space_38.9_MB_(48.00%)

import kotlin.math.min

class Solution {
fun minimumSubstringsInPartition(s: String): Int {
val cs = s.toCharArray()
val n = cs.size
val dp = IntArray(n + 1)
dp.fill(n)
dp[0] = 0
for (i in 1..n) {
val count = IntArray(26)
var distinct = 0
var maxCount = 0
for (j in i - 1 downTo 0) {
val index = cs[j].code - 'a'.code
if (++count[index] == 1) {
distinct++
}
if (count[index] > maxCount) {
maxCount = count[index]
}
if (maxCount * distinct == i - j) {
dp[i] = min(dp[i], (dp[j] + 1))
}
}
}
return dp[n]
}
}
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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package g3101_3200.s3145_find_products_of_elements_of_big_array

// #Hard #Array #Binary_Search #Bit_Manipulation
// #2024_05_15_Time_210_ms_(100.00%)_Space_40_MB_(100.00%)

class Solution {
fun findProductsOfElements(queries: Array<LongArray>): IntArray {
val ans = IntArray(queries.size)
for (i in queries.indices) {
val q = queries[i]
val er = sumE(q[1] + 1)
val el = sumE(q[0])
ans[i] = pow(2, er - el, q[2])
}
return ans
}

private fun sumE(k: Long): Long {
var k = k
var res: Long = 0
var n: Long = 0
var cnt1: Long = 0
var sumI: Long = 0
for (i in 63L - java.lang.Long.numberOfLeadingZeros(k + 1) downTo 1) {
val c = (cnt1 shl i.toInt()) + (i shl (i - 1).toInt())
if (c <= k) {
k -= c
res += (sumI shl i.toInt()) + ((i * (i - 1) / 2) shl (i - 1).toInt())
sumI += i
cnt1++
n = n or (1L shl i.toInt())
}
}
if (cnt1 <= k) {
k -= cnt1
res += sumI
n++
}
while (k-- > 0) {
res += java.lang.Long.numberOfTrailingZeros(n).toLong()
n = n and n - 1
}
return res
}

private fun pow(x: Long, n: Long, mod: Long): Int {
var x = x
var n = n
var res = 1 % mod
while (n > 0) {
if (n % 2 == 1L) {
res = (res * x) % mod
}
x = (x * x) % mod
n /= 2
}
return res.toInt()
}
}
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>
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_177_ms_(58.21%)_Space_38.1_MB_(7.46%)

import kotlin.math.abs

class Solution {
fun findPermutationDifference(s: String, t: String): Int {
val res = IntArray(26)
res.fill(-1)
var sum = 0
for (i in s.indices) {
res[s[i].code - 'a'.code] = i
}
for (i in t.indices) {
sum += abs((res[t[i].code - 'a'.code] - i))
}
return sum
}
}
Loading

0 comments on commit 85fb916

Please sign in to comment.