Skip to content

Commit 81a81cc

Browse files
authored
Added tasks 2942-2944
1 parent 49ef41e commit 81a81cc

File tree

4 files changed

+289
-0
lines changed

4 files changed

+289
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,6 +1821,9 @@
18211821
| 2947 |[Count Beautiful Substrings I](src/main/java/g2901_3000/s2947_count_beautiful_substrings_i)| Medium | String, Prefix_Sum, Enumeration | 2 | 100.00
18221822
| 2946 |[Matrix Similarity After Cyclic Shifts](src/main/java/g2901_3000/s2946_matrix_similarity_after_cyclic_shifts)| Easy | Array, Math, Matrix, Simulation | 1 | 100.00
18231823
| 2945 |[Find Maximum Non-decreasing Array Length](src/main/java/g2901_3000/s2945_find_maximum_non_decreasing_array_length)| Hard | Array, Dynamic_Programming, Binary_Search, Stack, Monotonic_Stack, Queue, Monotonic_Queue | 11 | 98.10
1824+
| 2944 |[Minimum Number of Coins for Fruits](src/main/java/g2901_3000/s2944_minimum_number_of_coins_for_fruits)| Medium | Array, Dynamic_Programming, Heap_Priority_Queue, Queue, Monotonic_Queue | 2 | 99.43
1825+
| 2943 |[Maximize Area of Square Hole in Grid](src/main/java/g2901_3000/s2943_maximize_area_of_square_hole_in_grid)| Medium | Array, Sorting | 2 | 100.00
1826+
| 2942 |[Find Words Containing Character](src/main/java/g2901_3000/s2942_find_words_containing_character)| Easy | Array, String | 2 | 72.65
18241827
| 2940 |[Find Building Where Alice and Bob Can Meet](src/main/java/g2901_3000/s2940_find_building_where_alice_and_bob_can_meet)| Hard | Array, Binary_Search, Stack, Heap_Priority_Queue, Monotonic_Stack, Segment_Tree, Binary_Indexed_Tree | 19 | 94.91
18251828
| 2939 |[Maximum Xor Product](src/main/java/g2901_3000/s2939_maximum_xor_product)| Medium | Math, Greedy, Bit_Manipulation | 1 | 100.00
18261829
| 2938 |[Separate Black and White Balls](src/main/java/g2901_3000/s2938_separate_black_and_white_balls)| Medium | String, Greedy, Two_Pointers | 7 | 99.65
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 2942\. Find Words Containing Character
5+
6+
Easy
7+
8+
You are given a **0-indexed** array of strings `words` and a character `x`.
9+
10+
Return _an **array of indices** representing the words that contain the character_ `x`.
11+
12+
**Note** that the returned array may be in **any** order.
13+
14+
**Example 1:**
15+
16+
**Input:** words = ["leet","code"], x = "e"
17+
18+
**Output:** [0,1]
19+
20+
**Explanation:** "e" occurs in both words: "l**<ins>ee</ins>**t", and "cod<ins>**e**</ins>". Hence, we return indices 0 and 1.
21+
22+
**Example 2:**
23+
24+
**Input:** words = ["abc","bcd","aaaa","cbc"], x = "a"
25+
26+
**Output:** [0,2]
27+
28+
**Explanation:** "a" occurs in "**<ins>a</ins>**bc", and "<ins>**aaaa**</ins>". Hence, we return indices 0 and 2.
29+
30+
**Example 3:**
31+
32+
**Input:** words = ["abc","bcd","aaaa","cbc"], x = "z"
33+
34+
**Output:** []
35+
36+
**Explanation:** "z" does not occur in any of the words. Hence, we return an empty array.
37+
38+
**Constraints:**
39+
40+
* `1 <= words.length <= 50`
41+
* `1 <= words[i].length <= 50`
42+
* `x` is a lowercase English letter.
43+
* `words[i]` consists only of lowercase English letters.
44+
45+
## Solution
46+
47+
```java
48+
import java.util.ArrayList;
49+
import java.util.List;
50+
51+
public class Solution {
52+
public List<Integer> findWordsContaining(String[] words, char x) {
53+
List<Integer> ans = new ArrayList<>();
54+
for (int i = 0; i < words.length; i++) {
55+
for (int j = 0; j < words[i].length(); j++) {
56+
if (words[i].charAt(j) == x) {
57+
ans.add(i);
58+
break;
59+
}
60+
}
61+
}
62+
return ans;
63+
}
64+
}
65+
```
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 2943\. Maximize Area of Square Hole in Grid
5+
6+
Medium
7+
8+
There is a grid with `n + 2` **horizontal** bars and `m + 2` **vertical** bars, and initially containing `1 x 1` unit cells.
9+
10+
The bars are **1-indexed**.
11+
12+
You are given the two integers, `n` and `m`.
13+
14+
You are also given two integer arrays: `hBars` and `vBars`.
15+
16+
* `hBars` contains **distinct** horizontal bars in the range `[2, n + 1]`.
17+
* `vBars` contains **distinct** vertical bars in the range `[2, m + 1]`.
18+
19+
You are allowed to **remove** bars that satisfy any of the following conditions:
20+
21+
* If it is a horizontal bar, it must correspond to a value in `hBars`.
22+
* If it is a vertical bar, it must correspond to a value in `vBars`.
23+
24+
Return _an integer denoting the **maximum** area of a **square-shaped** hole in the grid after removing some bars (**possibly none**)._
25+
26+
**Example 1:**
27+
28+
![](https://assets.leetcode.com/uploads/2023/11/05/screenshot-from-2023-11-05-22-40-25.png)
29+
30+
**Input:** n = 2, m = 1, hBars = [2,3], vBars = [2]
31+
32+
**Output:** 4
33+
34+
**Explanation:** The left image shows the initial grid formed by the bars.
35+
36+
The horizontal bars are in the range [1,4], and the vertical bars are in the range [1,3].
37+
38+
It is allowed to remove horizontal bars [2,3] and the vertical bar [2].
39+
40+
One way to get the maximum square-shaped hole is by removing horizontal bar 2 and vertical bar 2.
41+
42+
The resulting grid is shown on the right.
43+
44+
The hole has an area of 4.
45+
46+
It can be shown that it is not possible to get a square hole with an area more than 4.
47+
48+
Hence, the answer is 4.
49+
50+
**Example 2:**
51+
52+
![](https://assets.leetcode.com/uploads/2023/11/04/screenshot-from-2023-11-04-17-01-02.png)
53+
54+
**Input:** n = 1, m = 1, hBars = [2], vBars = [2]
55+
56+
**Output:** 4
57+
58+
**Explanation:** The left image shows the initial grid formed by the bars.
59+
60+
The horizontal bars are in the range [1,3], and the vertical bars are in the range [1,3].
61+
62+
It is allowed to remove the horizontal bar [2] and the vertical bar [2].
63+
64+
To get the maximum square-shaped hole, we remove horizontal bar 2 and vertical bar 2.
65+
66+
The resulting grid is shown on the right.
67+
68+
The hole has an area of 4.
69+
70+
Hence, the answer is 4, and it is the maximum possible.
71+
72+
**Example 3:**
73+
74+
![](https://assets.leetcode.com/uploads/2023/11/05/screenshot-from-2023-11-05-22-33-35.png)
75+
76+
**Input:** n = 2, m = 3, hBars = [2,3], vBars = [2,3,4]
77+
78+
**Output:** 9
79+
80+
**Explanation:** The left image shows the initial grid formed by the bars.
81+
82+
The horizontal bars are in the range [1,4], and the vertical bars are in the range [1,5].
83+
84+
It is allowed to remove horizontal bars [2,3] and vertical bars [2,3,4].
85+
86+
One way to get the maximum square-shaped hole is by removing horizontal bars 2 and 3, and vertical bars 3 and 4.
87+
88+
The resulting grid is shown on the right.
89+
90+
The hole has an area of 9.
91+
92+
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.
93+
94+
**Constraints:**
95+
96+
* <code>1 <= n <= 10<sup>9</sup></code>
97+
* <code>1 <= m <= 10<sup>9</sup></code>
98+
* `1 <= hBars.length <= 100`
99+
* `2 <= hBars[i] <= n + 1`
100+
* `1 <= vBars.length <= 100`
101+
* `2 <= vBars[i] <= m + 1`
102+
* All values in `hBars` are distinct.
103+
* All values in `vBars` are distinct.
104+
105+
## Solution
106+
107+
```java
108+
import java.util.Arrays;
109+
110+
@SuppressWarnings("java:S1172")
111+
public class Solution {
112+
public int maximizeSquareHoleArea(int n, int m, int[] hBars, int[] vBars) {
113+
int x = find(hBars);
114+
int y = find(vBars);
115+
int res = Math.min(x, y) + 1;
116+
return res * res;
117+
}
118+
119+
private int find(int[] arr) {
120+
Arrays.sort(arr);
121+
int res = 1;
122+
int i = 0;
123+
int n = arr.length;
124+
while (i < n) {
125+
int count = 1;
126+
while (i + 1 < n && arr[i] + 1 == arr[i + 1]) {
127+
i++;
128+
count++;
129+
}
130+
i++;
131+
res = Math.max(res, count);
132+
}
133+
return res;
134+
}
135+
}
136+
```
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 2944\. Minimum Number of Coins for Fruits
5+
6+
Medium
7+
8+
You are at a fruit market with different types of exotic fruits on display.
9+
10+
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.
11+
12+
The fruit market has the following offer:
13+
14+
* If you purchase the <code>i<sup>th</sup></code> fruit at `prices[i]` coins, you can get the next `i` fruits for free.
15+
16+
**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.
17+
18+
Return _the **minimum** number of coins needed to acquire all the fruits_.
19+
20+
**Example 1:**
21+
22+
**Input:** prices = [3,1,2]
23+
24+
**Output:** 4
25+
26+
**Explanation:** You can acquire the fruits as follows:
27+
28+
- Purchase the 1<sup>st</sup> fruit with 3 coins, you are allowed to take the 2<sup>nd</sup> fruit for free.
29+
30+
- Purchase the 2<sup>nd</sup> fruit with 1 coin, you are allowed to take the 3<sup>rd</sup> fruit for free.
31+
32+
- Take the 3<sup>rd</sup> fruit for free.
33+
34+
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.
35+
36+
It can be proven that 4 is the minimum number of coins needed to acquire all the fruits.
37+
38+
**Example 2:**
39+
40+
**Input:** prices = [1,10,1,1]
41+
42+
**Output:** 2
43+
44+
**Explanation:** You can acquire the fruits as follows:
45+
46+
- Purchase the 1<sup>st</sup> fruit with 1 coin, you are allowed to take the 2<sup>nd</sup> fruit for free.
47+
48+
- Take the 2<sup>nd</sup> fruit for free.
49+
50+
- Purchase the 3<sup>rd</sup> fruit for 1 coin, you are allowed to take the 4<sup>th</sup> fruit for free.
51+
52+
- Take the 4<sup>t</sup><sup>h</sup> fruit for free.
53+
54+
It can be proven that 2 is the minimum number of coins needed to acquire all the fruits.
55+
56+
**Constraints:**
57+
58+
* `1 <= prices.length <= 1000`
59+
* <code>1 <= prices[i] <= 10<sup>5</sup></code>
60+
61+
## Solution
62+
63+
```java
64+
public class Solution {
65+
public int minimumCoins(int[] prices) {
66+
int n = prices.length;
67+
int[] dp = new int[n];
68+
dp[n - 1] = prices[n - 1];
69+
for (int i = n - 2; i >= 0; i--) {
70+
int pos = i + 1;
71+
int acquired = i + pos;
72+
if (acquired + 1 < n) {
73+
int min = Integer.MAX_VALUE;
74+
for (int j = acquired + 1; j >= i + 1; j--) {
75+
min = Math.min(min, dp[j]);
76+
}
77+
dp[i] = prices[i] + min;
78+
} else {
79+
dp[i] = prices[i];
80+
}
81+
}
82+
return dp[0];
83+
}
84+
}
85+
```

0 commit comments

Comments
 (0)