Skip to content

Commit 6865197

Browse files
authored
Added tasks 2942, 2943, 2944
1 parent cafca11 commit 6865197

File tree

9 files changed

+352
-0
lines changed

9 files changed

+352
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g2901_3000.s2942_find_words_containing_character;
2+
3+
// #Easy #Array #String #2024_01_04_Time_2_ms_(72.65%)_Space_44.8_MB_(14.23%)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class Solution {
9+
public List<Integer> findWordsContaining(String[] words, char x) {
10+
List<Integer> ans = new ArrayList<>();
11+
for (int i = 0; i < words.length; i++) {
12+
for (int j = 0; j < words[i].length(); j++) {
13+
if (words[i].charAt(j) == x) {
14+
ans.add(i);
15+
break;
16+
}
17+
}
18+
}
19+
return ans;
20+
}
21+
}
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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g2901_3000.s2943_maximize_area_of_square_hole_in_grid;
2+
3+
// #Medium #Array #Sorting #2024_01_04_Time_2_ms_(100.00%)_Space_43.2_MB_(70.48%)
4+
5+
import java.util.Arrays;
6+
7+
@SuppressWarnings("java:S1172")
8+
public class Solution {
9+
public int maximizeSquareHoleArea(int n, int m, int[] hBars, int[] vBars) {
10+
int x = find(hBars);
11+
int y = find(vBars);
12+
int res = Math.min(x, y) + 1;
13+
return res * res;
14+
}
15+
16+
private int find(int[] arr) {
17+
Arrays.sort(arr);
18+
int res = 1;
19+
int i = 0;
20+
int n = arr.length;
21+
while (i < n) {
22+
int count = 1;
23+
while (i + 1 < n && arr[i] + 1 == arr[i + 1]) {
24+
i++;
25+
count++;
26+
}
27+
i++;
28+
res = Math.max(res, count);
29+
}
30+
return res;
31+
}
32+
}
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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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_04_Time_2_ms_(99.43%)_Space_43.9_MB_(56.19%)
5+
6+
public class Solution {
7+
public int minimumCoins(int[] prices) {
8+
int n = prices.length;
9+
int[] dp = new int[n];
10+
dp[n - 1] = prices[n - 1];
11+
for (int i = n - 2; i >= 0; i--) {
12+
int pos = i + 1;
13+
int acquired = i + pos;
14+
if (acquired + 1 < n) {
15+
int min = Integer.MAX_VALUE;
16+
for (int j = acquired + 1; j >= i + 1; j--) {
17+
min = Math.min(min, dp[j]);
18+
}
19+
dp[i] = prices[i] + min;
20+
} else {
21+
dp[i] = prices[i];
22+
}
23+
}
24+
return dp[0];
25+
}
26+
}
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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g2901_3000.s2942_find_words_containing_character;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import java.util.Arrays;
7+
import org.junit.jupiter.api.Test;
8+
9+
class SolutionTest {
10+
@Test
11+
void findWordsContaining() {
12+
assertThat(
13+
new Solution().findWordsContaining(new String[] {"leet", "code"}, 'e'),
14+
equalTo(Arrays.asList(0, 1)));
15+
}
16+
17+
@Test
18+
void findWordsContaining2() {
19+
assertThat(
20+
new Solution().findWordsContaining(new String[] {"abc", "bcd", "aaaa", "cbc"}, 'a'),
21+
equalTo(Arrays.asList(0, 2)));
22+
}
23+
24+
@Test
25+
void findWordsContaining3() {
26+
assertThat(
27+
new Solution().findWordsContaining(new String[] {"abc", "bcd", "aaaa", "cbc"}, 'z'),
28+
equalTo(Arrays.asList()));
29+
}
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g2901_3000.s2943_maximize_area_of_square_hole_in_grid;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void maximizeSquareHoleArea() {
11+
assertThat(
12+
new Solution().maximizeSquareHoleArea(2, 1, new int[] {2, 3}, new int[] {2}),
13+
equalTo(4));
14+
}
15+
16+
@Test
17+
void maximizeSquareHoleArea2() {
18+
assertThat(
19+
new Solution().maximizeSquareHoleArea(1, 1, new int[] {2}, new int[] {2}),
20+
equalTo(4));
21+
}
22+
23+
@Test
24+
void maximizeSquareHoleArea3() {
25+
assertThat(
26+
new Solution().maximizeSquareHoleArea(2, 3, new int[] {2, 3}, new int[] {2, 3, 4}),
27+
equalTo(9));
28+
}
29+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2901_3000.s2944_minimum_number_of_coins_for_fruits;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void minimumCoins() {
11+
assertThat(new Solution().minimumCoins(new int[] {3, 1, 2}), equalTo(4));
12+
}
13+
14+
@Test
15+
void minimumCoins2() {
16+
assertThat(new Solution().minimumCoins(new int[] {1, 10, 1, 1}), equalTo(2));
17+
}
18+
}

0 commit comments

Comments
 (0)