Skip to content

Commit c0a23d5

Browse files
authored
Added tasks 2942, 2943, 2944
1 parent c91cb45 commit c0a23d5

File tree

9 files changed

+354
-0
lines changed

9 files changed

+354
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2901_3000.s2942_find_words_containing_character
2+
3+
// #Easy #Array #String #2024_01_07_Time_216_ms_(98.97%)_Space_37.6_MB_(98.46%)
4+
5+
class Solution {
6+
fun findWordsContaining(words: Array<String>, x: Char): List<Int> {
7+
val ans: MutableList<Int> = ArrayList()
8+
for (i in words.indices) {
9+
for (j in 0 until words[i].length) {
10+
if (words[i][j] == x) {
11+
ans.add(i)
12+
break
13+
}
14+
}
15+
}
16+
return ans
17+
}
18+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2942\. Find Words Containing Character
2+
3+
Easy
4+
5+
You are given a **0-indexed** array of strings `words` and a character `x`.
6+
7+
Return _an **array of indices** representing the words that contain the character_ `x`.
8+
9+
**Note** that the returned array may be in **any** order.
10+
11+
**Example 1:**
12+
13+
**Input:** words = ["leet","code"], x = "e"
14+
15+
**Output:** [0,1]
16+
17+
**Explanation:** "e" occurs in both words: "l**<ins>ee</ins>**t", and "cod<ins>**e**</ins>". Hence, we return indices 0 and 1.
18+
19+
**Example 2:**
20+
21+
**Input:** words = ["abc","bcd","aaaa","cbc"], x = "a"
22+
23+
**Output:** [0,2]
24+
25+
**Explanation:** "a" occurs in "**<ins>a</ins>**bc", and "<ins>**aaaa**</ins>". Hence, we return indices 0 and 2.
26+
27+
**Example 3:**
28+
29+
**Input:** words = ["abc","bcd","aaaa","cbc"], x = "z"
30+
31+
**Output:** []
32+
33+
**Explanation:** "z" does not occur in any of the words. Hence, we return an empty array.
34+
35+
**Constraints:**
36+
37+
* `1 <= words.length <= 50`
38+
* `1 <= words[i].length <= 50`
39+
* `x` is a lowercase English letter.
40+
* `words[i]` consists only of lowercase English letters.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g2901_3000.s2943_maximize_area_of_square_hole_in_grid
2+
3+
// #Medium #Array #Sorting #2024_01_07_Time_180_ms_(86.67%)_Space_38.1_MB_(60.00%)
4+
5+
import kotlin.math.max
6+
import kotlin.math.min
7+
8+
@Suppress("UNUSED_PARAMETER")
9+
class Solution {
10+
fun maximizeSquareHoleArea(n: Int, m: Int, hBars: IntArray, vBars: IntArray): Int {
11+
val x = find(hBars)
12+
val y = find(vBars)
13+
val res = min(x, y) + 1
14+
return res * res
15+
}
16+
17+
private fun find(arr: IntArray): Int {
18+
arr.sort()
19+
var res = 1
20+
var i = 0
21+
val n = arr.size
22+
while (i < n) {
23+
var count = 1
24+
while (i + 1 < n && arr[i] + 1 == arr[i + 1]) {
25+
i++
26+
count++
27+
}
28+
i++
29+
res = max(res, count)
30+
}
31+
return res
32+
}
33+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
2943\. Maximize Area of Square Hole in Grid
2+
3+
Medium
4+
5+
There is a grid with `n + 2` **horizontal** bars and `m + 2` **vertical** bars, and initially containing `1 x 1` unit cells.
6+
7+
The bars are **1-indexed**.
8+
9+
You are given the two integers, `n` and `m`.
10+
11+
You are also given two integer arrays: `hBars` and `vBars`.
12+
13+
* `hBars` contains **distinct** horizontal bars in the range `[2, n + 1]`.
14+
* `vBars` contains **distinct** vertical bars in the range `[2, m + 1]`.
15+
16+
You are allowed to **remove** bars that satisfy any of the following conditions:
17+
18+
* If it is a horizontal bar, it must correspond to a value in `hBars`.
19+
* If it is a vertical bar, it must correspond to a value in `vBars`.
20+
21+
Return _an integer denoting the **maximum** area of a **square-shaped** hole in the grid after removing some bars (**possibly none**)._
22+
23+
**Example 1:**
24+
25+
![](https://assets.leetcode.com/uploads/2023/11/05/screenshot-from-2023-11-05-22-40-25.png)
26+
27+
**Input:** n = 2, m = 1, hBars = [2,3], vBars = [2]
28+
29+
**Output:** 4
30+
31+
**Explanation:** The left image shows the initial grid formed by the bars.
32+
33+
The horizontal bars are in the range [1,4], and the vertical bars are in the range [1,3].
34+
35+
It is allowed to remove horizontal bars [2,3] and the vertical bar [2].
36+
37+
One way to get the maximum square-shaped hole is by removing horizontal bar 2 and vertical bar 2.
38+
39+
The resulting grid is shown on the right.
40+
41+
The hole has an area of 4.
42+
43+
It can be shown that it is not possible to get a square hole with an area more than 4.
44+
45+
Hence, the answer is 4.
46+
47+
**Example 2:**
48+
49+
![](https://assets.leetcode.com/uploads/2023/11/04/screenshot-from-2023-11-04-17-01-02.png)
50+
51+
**Input:** n = 1, m = 1, hBars = [2], vBars = [2]
52+
53+
**Output:** 4
54+
55+
**Explanation:** The left image shows the initial grid formed by the bars.
56+
57+
The horizontal bars are in the range [1,3], and the vertical bars are in the range [1,3].
58+
59+
It is allowed to remove the horizontal bar [2] and the vertical bar [2].
60+
61+
To get the maximum square-shaped hole, we remove horizontal bar 2 and vertical bar 2.
62+
63+
The resulting grid is shown on the right.
64+
65+
The hole has an area of 4.
66+
67+
Hence, the answer is 4, and it is the maximum possible.
68+
69+
**Example 3:**
70+
71+
![](https://assets.leetcode.com/uploads/2023/11/05/screenshot-from-2023-11-05-22-33-35.png)
72+
73+
**Input:** n = 2, m = 3, hBars = [2,3], vBars = [2,3,4]
74+
75+
**Output:** 9
76+
77+
**Explanation:** The left image shows the initial grid formed by the bars.
78+
79+
The horizontal bars are in the range [1,4], and the vertical bars are in the range [1,5].
80+
81+
It is allowed to remove horizontal bars [2,3] and vertical bars [2,3,4].
82+
83+
One way to get the maximum square-shaped hole is by removing horizontal bars 2 and 3, and vertical bars 3 and 4.
84+
85+
The resulting grid is shown on the right.
86+
87+
The hole has an area of 9.
88+
89+
It can be shown that it is not possible to get a square hole with an area more than 9. Hence, the answer is 9.
90+
91+
**Constraints:**
92+
93+
* <code>1 <= n <= 10<sup>9</sup></code>
94+
* <code>1 <= m <= 10<sup>9</sup></code>
95+
* `1 <= hBars.length <= 100`
96+
* `2 <= hBars[i] <= n + 1`
97+
* `1 <= vBars.length <= 100`
98+
* `2 <= vBars[i] <= m + 1`
99+
* All values in `hBars` are distinct.
100+
* All values in `vBars` are distinct.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g2901_3000.s2944_minimum_number_of_coins_for_fruits
2+
3+
// #Medium #Array #Dynamic_Programming #Heap_Priority_Queue #Queue #Monotonic_Queue
4+
// #2024_01_07_Time_194_ms_(84.62%)_Space_37.5_MB_(92.31%)
5+
6+
import kotlin.math.min
7+
8+
class Solution {
9+
fun minimumCoins(prices: IntArray): Int {
10+
val n = prices.size
11+
val dp = IntArray(n)
12+
dp[n - 1] = prices[n - 1]
13+
for (i in n - 2 downTo 0) {
14+
val pos = i + 1
15+
val acquired = i + pos
16+
if (acquired + 1 < n) {
17+
var min = Int.MAX_VALUE
18+
for (j in acquired + 1 downTo i + 1) {
19+
min = min(min.toDouble(), dp[j].toDouble()).toInt()
20+
}
21+
dp[i] = prices[i] + min
22+
} else {
23+
dp[i] = prices[i]
24+
}
25+
}
26+
return dp[0]
27+
}
28+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2944\. Minimum Number of Coins for Fruits
2+
3+
Medium
4+
5+
You are at a fruit market with different types of exotic fruits on display.
6+
7+
You are given a **1-indexed** array `prices`, where `prices[i]` denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.
8+
9+
The fruit market has the following offer:
10+
11+
* If you purchase the <code>i<sup>th</sup></code> fruit at `prices[i]` coins, you can get the next `i` fruits for free.
12+
13+
**Note** that even if you **can** take fruit `j` for free, you can still purchase it for `prices[j]` coins to receive a new offer.
14+
15+
Return _the **minimum** number of coins needed to acquire all the fruits_.
16+
17+
**Example 1:**
18+
19+
**Input:** prices = [3,1,2]
20+
21+
**Output:** 4
22+
23+
**Explanation:** You can acquire the fruits as follows:
24+
25+
- Purchase the 1<sup>st</sup> fruit with 3 coins, you are allowed to take the 2<sup>nd</sup> fruit for free.
26+
27+
- Purchase the 2<sup>nd</sup> fruit with 1 coin, you are allowed to take the 3<sup>rd</sup> fruit for free.
28+
29+
- Take the 3<sup>rd</sup> fruit for free.
30+
31+
Note that even though you were allowed to take the 2<sup>nd</sup> fruit for free, you purchased it because it is more optimal.
32+
33+
It can be proven that 4 is the minimum number of coins needed to acquire all the fruits.
34+
35+
**Example 2:**
36+
37+
**Input:** prices = [1,10,1,1]
38+
39+
**Output:** 2
40+
41+
**Explanation:** You can acquire the fruits as follows:
42+
43+
- Purchase the 1<sup>st</sup> fruit with 1 coin, you are allowed to take the 2<sup>nd</sup> fruit for free.
44+
45+
- Take the 2<sup>nd</sup> fruit for free.
46+
47+
- Purchase the 3<sup>rd</sup> fruit for 1 coin, you are allowed to take the 4<sup>th</sup> fruit for free.
48+
49+
- Take the 4<sup>t</sup><sup>h</sup> fruit for free.
50+
51+
It can be proven that 2 is the minimum number of coins needed to acquire all the fruits.
52+
53+
**Constraints:**
54+
55+
* `1 <= prices.length <= 1000`
56+
* <code>1 <= prices[i] <= 10<sup>5</sup></code>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g2901_3000.s2942_find_words_containing_character
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun findWordsContaining() {
10+
assertThat(
11+
Solution().findWordsContaining(arrayOf("leet", "code"), 'e'),
12+
equalTo(mutableListOf(0, 1))
13+
)
14+
}
15+
16+
@Test
17+
fun findWordsContaining2() {
18+
assertThat(
19+
Solution().findWordsContaining(arrayOf("abc", "bcd", "aaaa", "cbc"), 'a'),
20+
equalTo(mutableListOf(0, 2))
21+
)
22+
}
23+
24+
@Test
25+
fun findWordsContaining3() {
26+
assertThat(
27+
Solution().findWordsContaining(arrayOf("abc", "bcd", "aaaa", "cbc"), 'z'),
28+
equalTo(mutableListOf<Any>())
29+
)
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g2901_3000.s2943_maximize_area_of_square_hole_in_grid
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun maximizeSquareHoleArea() {
10+
assertThat(
11+
Solution().maximizeSquareHoleArea(2, 1, intArrayOf(2, 3), intArrayOf(2)),
12+
equalTo(4)
13+
)
14+
}
15+
16+
@Test
17+
fun maximizeSquareHoleArea2() {
18+
assertThat(
19+
Solution().maximizeSquareHoleArea(1, 1, intArrayOf(2), intArrayOf(2)),
20+
equalTo(4)
21+
)
22+
}
23+
24+
@Test
25+
fun maximizeSquareHoleArea3() {
26+
assertThat(
27+
Solution().maximizeSquareHoleArea(2, 3, intArrayOf(2, 3), intArrayOf(2, 3, 4)),
28+
equalTo(9)
29+
)
30+
}
31+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g2901_3000.s2944_minimum_number_of_coins_for_fruits
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun minimumCoins() {
10+
assertThat(Solution().minimumCoins(intArrayOf(3, 1, 2)), equalTo(4))
11+
}
12+
13+
@Test
14+
fun minimumCoins2() {
15+
assertThat(Solution().minimumCoins(intArrayOf(1, 10, 1, 1)), equalTo(2))
16+
}
17+
}

0 commit comments

Comments
 (0)