Skip to content

Commit 2925017

Browse files
authored
Added tasks 2732-2736
1 parent 62ba4ef commit 2925017

File tree

15 files changed

+548
-0
lines changed

15 files changed

+548
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g2701_2800.s2732_find_a_good_subset_of_the_matrix
2+
3+
// #Hard #Array #Greedy #Matrix #Bit_Manipulation
4+
// #2023_08_05_Time_760_ms_(98.36%)_Space_71.4_MB_(78.69%)
5+
6+
class Solution {
7+
fun goodSubsetofBinaryMatrix(grid: Array<IntArray>): List<Int> {
8+
val m = grid.size
9+
val n = grid[0].size
10+
if (m == 1 && grid[0].sum() == 0) {
11+
return listOf(0)
12+
}
13+
val pos = mutableMapOf<Int, Int>()
14+
for (i in grid.indices) {
15+
for (mask in 0 until (1 shl n)) {
16+
var valid = true
17+
for (j in 0 until n) {
18+
if ((mask and (1 shl j)) != 0 && grid[i][j] + 1 > 1) {
19+
valid = false
20+
break
21+
}
22+
}
23+
if (valid && mask in pos) {
24+
return listOf(pos[mask]!!, i)
25+
}
26+
}
27+
var curr = 0
28+
for (j in 0 until n) {
29+
if (grid[i][j] == 1) {
30+
curr = curr or (1 shl j)
31+
}
32+
}
33+
pos[curr] = i
34+
}
35+
return emptyList()
36+
}
37+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2732\. Find a Good Subset of the Matrix
2+
3+
Hard
4+
5+
You are given a **0-indexed** `m x n` binary matrix `grid`.
6+
7+
Let us call a **non-empty** subset of rows **good** if the sum of each column of the subset is at most half of the length of the subset.
8+
9+
More formally, if the length of the chosen subset of rows is `k`, then the sum of each column should be at most `floor(k / 2)`.
10+
11+
Return _an integer array that contains row indices of a good subset sorted in **ascending** order._
12+
13+
If there are multiple good subsets, you can return any of them. If there are no good subsets, return an empty array.
14+
15+
A **subset** of rows of the matrix `grid` is any matrix that can be obtained by deleting some (possibly none or all) rows from `grid`.
16+
17+
**Example 1:**
18+
19+
**Input:** grid = [[0,1,1,0],[0,0,0,1],[1,1,1,1]]
20+
21+
**Output:** [0,1]
22+
23+
**Explanation:** We can choose the 0<sup>th</sup> and 1<sup>st</sup> rows to create a good subset of rows. The length of the chosen subset is 2.
24+
- The sum of the 0<sup>th</sup> column is 0 + 0 = 0, which is at most half of the length of the subset.
25+
- The sum of the 1<sup>st</sup> column is 1 + 0 = 1, which is at most half of the length of the subset.
26+
- The sum of the 2<sup>nd</sup> column is 1 + 0 = 1, which is at most half of the length of the subset.
27+
- The sum of the 3<sup>rd</sup> column is 0 + 1 = 1, which is at most half of the length of the subset.
28+
29+
**Example 2:**
30+
31+
**Input:** grid = [[0]]
32+
33+
**Output:** [0]
34+
35+
**Explanation:** We can choose the 0<sup>th</sup> row to create a good subset of rows. The length of the chosen subset is 1.
36+
- The sum of the 0<sup>th</sup> column is 0, which is at most half of the length of the subset.
37+
38+
**Example 3:**
39+
40+
**Input:** grid = [[1,1,1],[1,1,1]]
41+
42+
**Output:** []
43+
44+
**Explanation:** It is impossible to choose any subset of rows to create a good subset.
45+
46+
**Constraints:**
47+
48+
* `m == grid.length`
49+
* `n == grid[i].length`
50+
* <code>1 <= m <= 10<sup>4</sup></code>
51+
* `1 <= n <= 5`
52+
* `grid[i][j]` is either `0` or `1`.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2701_2800.s2733_neither_minimum_nor_maximum
2+
3+
// #Easy #Array #Sorting #2023_08_05_Time_286_ms_(100.00%)_Space_44.2_MB_(92.79%)
4+
5+
class Solution {
6+
fun findNonMinOrMax(nums: IntArray): Int {
7+
var mn = 999
8+
var mx: Int = -1
9+
nums.forEach {
10+
mn = kotlin.math.min(it, mn)
11+
mx = kotlin.math.max(it, mx)
12+
}
13+
nums.forEach {
14+
if (it != mn && it != mx)return it
15+
}
16+
return -1
17+
}
18+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2733\. Neither Minimum nor Maximum
2+
3+
Easy
4+
5+
Given an integer array `nums` containing **distinct** **positive** integers, find and return **any** number from the array that is neither the **minimum** nor the **maximum** value in the array, or **`-1`** if there is no such number.
6+
7+
Return _the selected integer._
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [3,2,1,4]
12+
13+
**Output:** 2
14+
15+
**Explanation:** In this example, the minimum value is 1 and the maximum value is 4. Therefore, either 2 or 3 can be valid answers.
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [1,2]
20+
21+
**Output:** -1
22+
23+
**Explanation:** Since there is no number in nums that is neither the maximum nor the minimum, we cannot select a number that satisfies the given condition. Therefore, there is no answer.
24+
25+
**Example 3:**
26+
27+
**Input:** nums = [2,1,3]
28+
29+
**Output:** 2
30+
31+
**Explanation:** Since 2 is neither the maximum nor the minimum value in nums, it is the only valid answer.
32+
33+
**Constraints:**
34+
35+
* `1 <= nums.length <= 100`
36+
* `1 <= nums[i] <= 100`
37+
* All values in `nums` are distinct
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g2701_2800.s2734_lexicographically_smallest_string_after_substring_operation
2+
3+
// #Medium #String #Greedy #2023_08_05_Time_384_ms_(100.00%)_Space_48.4_MB_(95.56%)
4+
5+
class Solution {
6+
fun smallestString(s: String): String {
7+
var resultArray = CharArray(s.length)
8+
var found = false
9+
var i = 0
10+
while (i < s.length) {
11+
var c = s[i]
12+
if (c != 'a' && !found) {
13+
found = true
14+
while (i < s.length) {
15+
c = s[i]
16+
if (c != 'a') {
17+
c--
18+
resultArray[i] = c
19+
} else {
20+
i--
21+
break
22+
}
23+
i++
24+
}
25+
} else {
26+
resultArray[i] = c
27+
}
28+
i++
29+
}
30+
if (!found) {
31+
resultArray = CharArray(s.length)
32+
i = 0
33+
while (i < s.length - 1) {
34+
resultArray[i] = 'a'
35+
i++
36+
}
37+
resultArray[s.length - 1] = 'z'
38+
}
39+
return String(resultArray)
40+
}
41+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2734\. Lexicographically Smallest String After Substring Operation
2+
3+
Medium
4+
5+
You are given a string `s` consisting of only lowercase English letters. In one operation, you can do the following:
6+
7+
* Select any non-empty substring of `s`, possibly the entire string, then replace each one of its characters with the previous character of the English alphabet. For example, 'b' is converted to 'a', and 'a' is converted to 'z'.
8+
9+
Return _the **lexicographically smallest** string you can obtain after performing the above operation **exactly once**._
10+
11+
A **substring** is a contiguous sequence of characters in a string.
12+
13+
A string `x` is **lexicographically smaller** than a string `y` of the same length if `x[i]` comes before `y[i]` in alphabetic order for the first position `i` such that `x[i] != y[i]`.
14+
15+
**Example 1:**
16+
17+
**Input:** s = "cbabc"
18+
19+
**Output:** "baabc"
20+
21+
**Explanation:** We apply the operation on the substring starting at index 0, and ending at index 1 inclusive. It can be proven that the resulting string is the lexicographically smallest.
22+
23+
**Example 2:**
24+
25+
**Input:** s = "acbbc"
26+
27+
**Output:** "abaab"
28+
29+
**Explanation:** We apply the operation on the substring starting at index 1, and ending at index 4 inclusive. It can be proven that the resulting string is the lexicographically smallest.
30+
31+
**Example 3:**
32+
33+
**Input:** s = "leetcode"
34+
35+
**Output:** "kddsbncd"
36+
37+
**Explanation:** We apply the operation on the entire string. It can be proven that the resulting string is the lexicographically smallest.
38+
39+
**Constraints:**
40+
41+
* <code>1 <= s.length <= 3 * 10<sup>5</sup></code>
42+
* `s` consists of lowercase English letters
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g2701_2800.s2735_collecting_chocolates
2+
3+
// #Medium #Array #Enumeration #2023_08_05_Time_237_ms_(100.00%)_Space_39.6_MB_(95.45%)
4+
5+
class Solution {
6+
fun minCost(nums: IntArray, x: Int): Long {
7+
val n = nums.size
8+
val dp = IntArray(n)
9+
var res: Long = 0
10+
for (i in 0 until n) {
11+
dp[i] = nums[i]
12+
res += nums[i].toLong()
13+
}
14+
for (i in 1 until n) {
15+
var sum: Long = i.toLong() * x.toLong()
16+
for (j in 0 until n) {
17+
val currIndex: Int = if (j + i >= n) j + i - n else j + i
18+
dp[j] = dp[j].coerceAtMost(nums[currIndex])
19+
sum += dp[j].toLong()
20+
}
21+
res = res.coerceAtMost(sum)
22+
}
23+
return res
24+
}
25+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2735\. Collecting Chocolates
2+
3+
Medium
4+
5+
You are given a **0-indexed** integer array `nums` of size `n` representing the cost of collecting different chocolates. The cost of collecting the chocolate at the index `i` is `nums[i]`. Each chocolate is of a different type, and initially, the chocolate at the index `i` is of <code>i<sup>th</sup></code> type.
6+
7+
In one operation, you can do the following with an incurred **cost** of `x`:
8+
9+
* Simultaneously change the chocolate of <code>i<sup>th</sup></code> type to <code>((i + 1) mod n)<sup>th</sup></code> type for all chocolates.
10+
11+
Return _the minimum cost to collect chocolates of all types, given that you can perform as many operations as you would like._
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [20,1,15], x = 5
16+
17+
**Output:** 13
18+
19+
**Explanation:** Initially, the chocolate types are [0,1,2]. We will buy the 1<sup>st</sup> type of chocolate at a cost of 1.
20+
21+
Now, we will perform the operation at a cost of 5, and the types of chocolates will become [1,2,0]. We will buy the 2<sup>nd</sup> type of chocolate at a cost of 1.
22+
23+
Now, we will again perform the operation at a cost of 5, and the chocolate types will become [2,0,1]. We will buy the 0<sup>th</sup> type of chocolate at a cost of 1.
24+
25+
Thus, the total cost will become (1 + 5 + 1 + 5 + 1) = 13. We can prove that this is optimal.
26+
27+
**Example 2:**
28+
29+
**Input:** nums = [1,2,3], x = 4
30+
31+
**Output:** 6
32+
33+
**Explanation:** We will collect all three types of chocolates at their own price without performing any operations. Therefore, the total cost is 1 + 2 + 3 = 6.
34+
35+
**Constraints:**
36+
37+
* `1 <= nums.length <= 1000`
38+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
39+
* <code>1 <= x <= 10<sup>9</sup></code>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package g2701_2800.s2736_maximum_sum_queries
2+
3+
// #Hard #Array #Sorting #Binary_Search #Stack #Monotonic_Stack #Segment_Tree #Binary_Indexed_Tree
4+
// #2023_08_05_Time_1043_ms_(100.00%)_Space_126.2_MB_(9.09%)
5+
6+
import java.util.TreeMap
7+
8+
class Solution {
9+
private fun update(map: TreeMap<Int, Int>, num: Int, sum: Int) {
10+
var entry = map.floorEntry(num)
11+
while (entry != null && entry.value <= sum) {
12+
map.remove(entry.key)
13+
val x = entry.key
14+
entry = map.floorEntry(x)
15+
}
16+
entry = map.ceilingEntry(num)
17+
if (entry == null || entry.value < sum) map.put(num, sum)
18+
}
19+
20+
private fun queryVal(map: TreeMap<Int, Int>, num: Int): Int {
21+
val (_, value) = map.ceilingEntry(num) ?: return -1
22+
return value
23+
}
24+
25+
fun maximumSumQueries(nums1: IntArray, nums2: IntArray, queries: Array<IntArray>): IntArray {
26+
val n = nums1.size
27+
val m = queries.size
28+
val v: MutableList<IntArray> = ArrayList()
29+
for (i in 0 until n) {
30+
v.add(intArrayOf(nums1[i], nums2[i]))
31+
}
32+
v.sortWith(
33+
Comparator { a: IntArray, b: IntArray ->
34+
a[0] - b[0]
35+
}
36+
)
37+
val ind: MutableList<Int> = ArrayList()
38+
for (i in 0 until m) ind.add(i)
39+
ind.sortWith(Comparator { a: Int?, b: Int? -> queries[b!!][0] - queries[a!!][0] })
40+
val values = TreeMap<Int, Int>()
41+
var j = n - 1
42+
val ans = IntArray(m)
43+
for (i in ind) {
44+
val a = queries[i][0]
45+
val b = queries[i][1]
46+
while (j >= 0 && v[j][0] >= a) {
47+
update(values, v[j][1], v[j][0] + v[j][1])
48+
j--
49+
}
50+
ans[i] = queryVal(values, b)
51+
}
52+
return ans
53+
}
54+
}

0 commit comments

Comments
 (0)