Skip to content

Commit 8878669

Browse files
authored
Added tasks 2086, 2087, 2088.
1 parent 116d770 commit 8878669

File tree

9 files changed

+364
-0
lines changed

9 files changed

+364
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package g2001_2100.s2086_minimum_number_of_buckets_required_to_collect_rainwater_from_houses;
2+
3+
// #Medium #String #Dynamic_Programming #Greedy
4+
// #2022_05_27_Time_8_ms_(73.71%)_Space_50.4_MB_(40.57%)
5+
6+
@SuppressWarnings("java:S135")
7+
public class Solution {
8+
public int minimumBuckets(String street) {
9+
// check if houses have space in between or not
10+
// eg:".HHH."
11+
// array formation
12+
char[] arr = street.toCharArray();
13+
for (int i = 0; i < arr.length; i++) {
14+
if (arr[i] == '.') {
15+
continue;
16+
}
17+
if (i + 1 < arr.length && arr[i + 1] == '.') {
18+
continue;
19+
}
20+
// H is present before curr character
21+
if (i - 1 >= 0 && arr[i - 1] == '.') {
22+
continue;
23+
}
24+
return -1;
25+
}
26+
int x = 0;
27+
for (int j = 0; j < arr.length; j++) {
28+
// point move next we only take care of H
29+
if (arr[j] == 'H') {
30+
if (j - 1 >= 0 && arr[j - 1] == 'X') {
31+
continue;
32+
}
33+
if (j + 1 < arr.length && arr[j + 1] == '.') {
34+
arr[j + 1] = 'X';
35+
} else {
36+
arr[j - 1] = 'X';
37+
}
38+
x++;
39+
}
40+
}
41+
return x;
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2086\. Minimum Number of Buckets Required to Collect Rainwater from Houses
2+
3+
Medium
4+
5+
You are given a **0-index****ed** string `street`. Each character in `street` is either `'H'` representing a house or `'.'` representing an empty space.
6+
7+
You can place buckets on the **empty spaces** to collect rainwater that falls from the adjacent houses. The rainwater from a house at index `i` is collected if a bucket is placed at index `i - 1` **and/or** index `i + 1`. A single bucket, if placed adjacent to two houses, can collect the rainwater from **both** houses.
8+
9+
Return _the **minimum** number of buckets needed so that for **every** house, there is **at least** one bucket collecting rainwater from it, or_ `-1` _if it is impossible._
10+
11+
**Example 1:**
12+
13+
**Input:** street = "H..H"
14+
15+
**Output:** 2
16+
17+
**Explanation:**
18+
19+
We can put buckets at index 1 and index 2.
20+
21+
"H..H" -> "HBBH" ('B' denotes where a bucket is placed).
22+
23+
The house at index 0 has a bucket to its right, and the house at index 3 has a bucket to its left.
24+
25+
Thus, for every house, there is at least one bucket collecting rainwater from it.
26+
27+
**Example 2:**
28+
29+
**Input:** street = ".H.H."
30+
31+
**Output:** 1
32+
33+
**Explanation:**
34+
35+
We can put a bucket at index 2.
36+
37+
".H.H." -> ".HBH." ('B' denotes where a bucket is placed).
38+
39+
The house at index 1 has a bucket to its right, and the house at index 3 has a bucket to its left.
40+
41+
Thus, for every house, there is at least one bucket collecting rainwater from it.
42+
43+
**Example 3:**
44+
45+
**Input:** street = ".HHH."
46+
47+
**Output:** -1
48+
49+
**Explanation:**
50+
51+
There is no empty space to place a bucket to collect the rainwater from the house at index 2.
52+
53+
Thus, it is impossible to collect the rainwater from all the houses.
54+
55+
**Constraints:**
56+
57+
* <code>1 <= street.length <= 10<sup>5</sup></code>
58+
* `street[i]` is either`'H'` or `'.'`.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g2001_2100.s2087_minimum_cost_homecoming_of_a_robot_in_a_grid;
2+
3+
// #Medium #Array #Greedy #Matrix #2022_05_27_Time_2_ms_(79.89%)_Space_61.9_MB_(83.07%)
4+
5+
public class Solution {
6+
public int minCost(int[] startPos, int[] homePos, int[] rowCosts, int[] colCosts) {
7+
int cost = 0;
8+
for (int i = Math.min(startPos[0], homePos[0]);
9+
i <= Math.max(startPos[0], homePos[0]);
10+
i++) {
11+
cost += rowCosts[i];
12+
}
13+
for (int j = Math.min(startPos[1], homePos[1]);
14+
j <= Math.max(startPos[1], homePos[1]);
15+
j++) {
16+
cost += colCosts[j];
17+
}
18+
return cost - rowCosts[startPos[0]] - colCosts[startPos[1]];
19+
}
20+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2087\. Minimum Cost Homecoming of a Robot in a Grid
2+
3+
Medium
4+
5+
There is an `m x n` grid, where `(0, 0)` is the top-left cell and `(m - 1, n - 1)` is the bottom-right cell. You are given an integer array `startPos` where <code>startPos = [start<sub>row</sub>, start<sub>col</sub>]</code> indicates that **initially**, a **robot** is at the cell <code>(start<sub>row</sub>, start<sub>col</sub>)</code>. You are also given an integer array `homePos` where <code>homePos = [home<sub>row</sub>, home<sub>col</sub>]</code> indicates that its **home** is at the cell <code>(home<sub>row</sub>, home<sub>col</sub>)</code>.
6+
7+
The robot needs to go to its home. It can move one cell in four directions: **left**, **right**, **up**, or **down**, and it can not move outside the boundary. Every move incurs some cost. You are further given two **0-indexed** integer arrays: `rowCosts` of length `m` and `colCosts` of length `n`.
8+
9+
* If the robot moves **up** or **down** into a cell whose **row** is `r`, then this move costs `rowCosts[r]`.
10+
* If the robot moves **left** or **right** into a cell whose **column** is `c`, then this move costs `colCosts[c]`.
11+
12+
Return _the **minimum total cost** for this robot to return home_.
13+
14+
**Example 1:**
15+
16+
![](https://assets.leetcode.com/uploads/2021/10/11/eg-1.png)
17+
18+
**Input:** startPos = [1, 0], homePos = [2, 3], rowCosts = [5, 4, 3], colCosts = [8, 2, 6, 7]
19+
20+
**Output:** 18
21+
22+
**Explanation:** One optimal path is that:
23+
24+
Starting from (1, 0)
25+
26+
-> It goes down to (**2**, 0). This move costs rowCosts[2] = 3.
27+
28+
-> It goes right to (2, **1**). This move costs colCosts[1] = 2.
29+
30+
-> It goes right to (2, **2**). This move costs colCosts[2] = 6.
31+
32+
-> It goes right to (2, **3**). This move costs colCosts[3] = 7.
33+
34+
The total cost is 3 + 2 + 6 + 7 = 18
35+
36+
**Example 2:**
37+
38+
**Input:** startPos = [0, 0], homePos = [0, 0], rowCosts = [5], colCosts = [26]
39+
40+
**Output:** 0
41+
42+
**Explanation:** The robot is already at its home. Since no moves occur, the total cost is 0.
43+
44+
**Constraints:**
45+
46+
* `m == rowCosts.length`
47+
* `n == colCosts.length`
48+
* <code>1 <= m, n <= 10<sup>5</sup></code>
49+
* <code>0 <= rowCosts[r], colCosts[c] <= 10<sup>4</sup></code>
50+
* `startPos.length == 2`
51+
* `homePos.length == 2`
52+
* <code>0 <= start<sub>row</sub>, home<sub>row</sub> < m</code>
53+
* <code>0 <= start<sub>col</sub>, home<sub>col</sub> < n</code>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g2001_2100.s2088_count_fertile_pyramids_in_a_land;
2+
3+
// #Hard #Array #Dynamic_Programming #Matrix #2022_05_27_Time_12_ms_(83.56%)_Space_75.5_MB_(41.09%)
4+
5+
public class Solution {
6+
public int countPyramids(int[][] grid) {
7+
int m = grid.length;
8+
int n = grid[0].length;
9+
int[][] rev = new int[m][n];
10+
for (int i = 0; i < m; ++i) {
11+
System.arraycopy(grid[i], 0, rev[m - i - 1], 0, n);
12+
}
13+
return cal(grid) + cal(rev);
14+
}
15+
16+
private int cal(int[][] grid) {
17+
int m = grid.length;
18+
int n = grid[0].length;
19+
int res = 0;
20+
for (int i = 1; i < m; ++i) {
21+
int cnt = 0;
22+
for (int j = 0; j < n; ++j) {
23+
if (0 != grid[i][j]) {
24+
cnt++;
25+
} else {
26+
cnt = 0;
27+
}
28+
if (0 == cnt || 0 == j) {
29+
continue;
30+
}
31+
grid[i][j] = Math.min(grid[i - 1][j - 1] + 1, (cnt + 1) >> 1);
32+
res += grid[i][j] - 1;
33+
}
34+
}
35+
return res;
36+
}
37+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2088\. Count Fertile Pyramids in a Land
2+
3+
Hard
4+
5+
A farmer has a **rectangular grid** of land with `m` rows and `n` columns that can be divided into unit cells. Each cell is either **fertile** (represented by a `1`) or **barren** (represented by a `0`). All cells outside the grid are considered barren.
6+
7+
A **pyramidal plot** of land can be defined as a set of cells with the following criteria:
8+
9+
1. The number of cells in the set has to be **greater than** `1` and all cells must be **fertile**.
10+
2. The **apex** of a pyramid is the **topmost** cell of the pyramid. The **height** of a pyramid is the number of rows it covers. Let `(r, c)` be the apex of the pyramid, and its height be `h`. Then, the plot comprises of cells `(i, j)` where `r <= i <= r + h - 1` **and** `c - (i - r) <= j <= c + (i - r)`.
11+
12+
An **inverse pyramidal plot** of land can be defined as a set of cells with similar criteria:
13+
14+
1. The number of cells in the set has to be **greater than** `1` and all cells must be **fertile**.
15+
2. The **apex** of an inverse pyramid is the **bottommost** cell of the inverse pyramid. The **height** of an inverse pyramid is the number of rows it covers. Let `(r, c)` be the apex of the pyramid, and its height be `h`. Then, the plot comprises of cells `(i, j)` where `r - h + 1 <= i <= r` **and** `c - (r - i) <= j <= c + (r - i)`.
16+
17+
Some examples of valid and invalid pyramidal (and inverse pyramidal) plots are shown below. Black cells indicate fertile cells.
18+
19+
![](https://assets.leetcode.com/uploads/2021/11/08/image.png)
20+
21+
Given a **0-indexed** `m x n` binary matrix `grid` representing the farmland, return _the **total number** of pyramidal and inverse pyramidal plots that can be found in_ `grid`.
22+
23+
**Example 1:**
24+
25+
![](https://assets.leetcode.com/uploads/2021/12/22/1.JPG)
26+
27+
**Input:** grid = [[0,1,1,0],[1,1,1,1]]
28+
29+
**Output:** 2
30+
31+
**Explanation:** The 2 possible pyramidal plots are shown in blue and red respectively.
32+
33+
There are no inverse pyramidal plots in this grid.
34+
35+
Hence total number of pyramidal and inverse pyramidal plots is 2 + 0 = 2.
36+
37+
**Example 2:**
38+
39+
![](https://assets.leetcode.com/uploads/2021/12/22/2.JPG)
40+
41+
**Input:** grid = [[1,1,1],[1,1,1]]
42+
43+
**Output:** 2
44+
45+
**Explanation:** The pyramidal plot is shown in blue, and the inverse pyramidal plot is shown in red.
46+
47+
Hence the total number of plots is 1 + 1 = 2.
48+
49+
**Example 3:**
50+
51+
![](https://assets.leetcode.com/uploads/2021/12/22/3.JPG)
52+
53+
**Input:** grid = [[1,1,1,1,0],[1,1,1,1,1],[1,1,1,1,1],[0,1,0,0,1]]
54+
55+
**Output:** 13
56+
57+
**Explanation:** There are 7 pyramidal plots, 3 of which are shown in the 2nd and 3rd figures.
58+
59+
There are 6 inverse pyramidal plots, 2 of which are shown in the last figure.
60+
61+
The total number of plots is 7 + 6 = 13.
62+
63+
**Constraints:**
64+
65+
* `m == grid.length`
66+
* `n == grid[i].length`
67+
* `1 <= m, n <= 1000`
68+
* <code>1 <= m * n <= 10<sup>5</sup></code>
69+
* `grid[i][j]` is either `0` or `1`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2001_2100.s2086_minimum_number_of_buckets_required_to_collect_rainwater_from_houses;
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 minimumBuckets() {
11+
assertThat(new Solution().minimumBuckets("H..H"), equalTo(2));
12+
}
13+
14+
@Test
15+
void minimumBuckets2() {
16+
assertThat(new Solution().minimumBuckets(".H.H."), equalTo(1));
17+
}
18+
19+
@Test
20+
void minimumBuckets3() {
21+
assertThat(new Solution().minimumBuckets(".HHH."), equalTo(-1));
22+
}
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g2001_2100.s2087_minimum_cost_homecoming_of_a_robot_in_a_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 minCost() {
11+
assertThat(
12+
new Solution()
13+
.minCost(
14+
new int[] {1, 0},
15+
new int[] {2, 3},
16+
new int[] {5, 4, 3},
17+
new int[] {8, 2, 6, 7}),
18+
equalTo(18));
19+
}
20+
21+
@Test
22+
void minCost2() {
23+
assertThat(
24+
new Solution()
25+
.minCost(new int[] {0, 0}, new int[] {0, 0}, new int[] {5}, new int[] {26}),
26+
equalTo(0));
27+
}
28+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g2001_2100.s2088_count_fertile_pyramids_in_a_land;
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 countPyramids() {
11+
assertThat(
12+
new Solution().countPyramids(new int[][] {{0, 1, 1, 0}, {1, 1, 1, 1}}), equalTo(2));
13+
}
14+
15+
@Test
16+
void countPyramids2() {
17+
assertThat(new Solution().countPyramids(new int[][] {{1, 1, 1}, {1, 1, 1}}), equalTo(2));
18+
}
19+
20+
@Test
21+
void countPyramids3() {
22+
assertThat(
23+
new Solution()
24+
.countPyramids(
25+
new int[][] {
26+
{1, 1, 1, 1, 0},
27+
{1, 1, 1, 1, 1},
28+
{1, 1, 1, 1, 1},
29+
{0, 1, 0, 0, 1}
30+
}),
31+
equalTo(13));
32+
}
33+
}

0 commit comments

Comments
 (0)