Skip to content

Commit 16aaf87

Browse files
authored
Added tasks 2319, 2320.
1 parent 11c28a4 commit 16aaf87

File tree

7 files changed

+194
-0
lines changed

7 files changed

+194
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,8 @@ implementation 'com.github.javadev:leetcode-in-java:1.11'
14851485

14861486
| # | Title | Difficulty | Tag | Time, ms | Time, %
14871487
|------|----------------|-------------|-------------|----------|---------
1488+
| 2320 |[Count Number of Ways to Place Houses](src/main/java/g2301_2400/s2320_count_number_of_ways_to_place_houses/Solution.java)| Medium | Dynamic_Programming | 13 | 82.46
1489+
| 2319 |[Check if Matrix Is X-Matrix](src/main/java/g2301_2400/s2319_check_if_matrix_is_x_matrix/Solution.java)| Easy | Array, Matrix | 3 | 53.58
14881490
| 2318 |[Number of Distinct Roll Sequences](src/main/java/g2301_2400/s2318_number_of_distinct_roll_sequences/Solution.java)| Hard || 254 | 91.67
14891491
| 2317 |[Maximum XOR After Operations](src/main/java/g2301_2400/s2317_maximum_xor_after_operations/Solution.java)| Medium || 1 | 100.00
14901492
| 2316 |[Count Unreachable Pairs of Nodes in an Undirected Graph](src/main/java/g2301_2400/s2316_count_unreachable_pairs_of_nodes_in_an_undirected_graph/Solution.java)| Medium || 32 | 100.00
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g2301_2400.s2319_check_if_matrix_is_x_matrix;
2+
3+
// #Easy #Array #Matrix #2022_06_28_Time_3_ms_(53.58%)_Space_54_MB_(83.44%)
4+
5+
public class Solution {
6+
public boolean checkXMatrix(int[][] grid) {
7+
for (int i = 0; i < grid.length; i++) {
8+
for (int j = 0; j < grid[0].length; j++) {
9+
if (i == j || i + j == grid.length - 1) {
10+
if (grid[i][j] == 0) {
11+
return false;
12+
}
13+
} else {
14+
if (grid[i][j] != 0) {
15+
return false;
16+
}
17+
}
18+
}
19+
}
20+
return true;
21+
}
22+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2319\. Check if Matrix Is X-Matrix
2+
3+
Easy
4+
5+
A square matrix is said to be an **X-Matrix** if **both** of the following conditions hold:
6+
7+
1. All the elements in the diagonals of the matrix are **non-zero**.
8+
2. All other elements are 0.
9+
10+
Given a 2D integer array `grid` of size `n x n` representing a square matrix, return `true` _if_ `grid` _is an X-Matrix_. Otherwise, return `false`.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2022/05/03/ex1.jpg)
15+
16+
**Input:** grid = [[2,0,0,1],[0,3,1,0],[0,5,2,0],[4,0,0,2]]
17+
18+
**Output:** true
19+
20+
**Explanation:** Refer to the diagram above.
21+
22+
An X-Matrix should have the green elements (diagonals) be non-zero and the red elements be 0.
23+
24+
Thus, grid is an X-Matrix.
25+
26+
**Example 2:**
27+
28+
![](https://assets.leetcode.com/uploads/2022/05/03/ex2.jpg)
29+
30+
**Input:** grid = [[5,7,0],[0,3,1],[0,5,0]]
31+
32+
**Output:** false
33+
34+
**Explanation:** Refer to the diagram above.
35+
36+
An X-Matrix should have the green elements (diagonals) be non-zero and the red elements be 0.
37+
38+
Thus, grid is not an X-Matrix.
39+
40+
**Constraints:**
41+
42+
* `n == grid.length == grid[i].length`
43+
* `3 <= n <= 100`
44+
* <code>0 <= grid[i][j] <= 10<sup>5</sup></code>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g2301_2400.s2320_count_number_of_ways_to_place_houses;
2+
3+
// #Medium #Dynamic_Programming #2022_06_28_Time_13_ms_(82.46%)_Space_40.6_MB_(94.91%)
4+
5+
public class Solution {
6+
public int countHousePlacements(int n) {
7+
// algo - 1st solve one side of the street
8+
// think 0 - space , 1 - house
9+
// if n = 1 then we can take one 0 and one 1 (total ways = 2)
10+
// if n = 2 then 00 , 01 , 10 , 11 but we cant take 11 as two house cant be adjacent.
11+
// so the 1 ended string will be only 1 which is same as previous 0 ended string and 0 ended
12+
// string are 2 which is previous sum(total ways)
13+
// apply this formula for n no's
14+
long mod = 1000000007;
15+
long space = 1;
16+
long house = 1;
17+
long sum = space + house;
18+
while (--n > 0) {
19+
house = space;
20+
space = sum;
21+
sum = (house + space) % mod;
22+
}
23+
// as street has two side
24+
return (int) ((sum * sum) % mod);
25+
}
26+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2320\. Count Number of Ways to Place Houses
2+
3+
Medium
4+
5+
There is a street with `n * 2` **plots**, where there are `n` plots on each side of the street. The plots on each side are numbered from `1` to `n`. On each plot, a house can be placed.
6+
7+
Return _the number of ways houses can be placed such that no two houses are adjacent to each other on the same side of the street_. Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
8+
9+
Note that if a house is placed on the <code>i<sup>th</sup></code> plot on one side of the street, a house can also be placed on the <code>i<sup>th</sup></code> plot on the other side of the street.
10+
11+
**Example 1:**
12+
13+
**Input:** n = 1
14+
15+
**Output:** 4
16+
17+
**Explanation:**
18+
19+
Possible arrangements:
20+
21+
1. All plots are empty.
22+
23+
2. A house is placed on one side of the street.
24+
25+
3. A house is placed on the other side of the street.
26+
27+
4. Two houses are placed, one on each side of the street.
28+
29+
**Example 2:**
30+
31+
![](https://assets.leetcode.com/uploads/2022/05/12/arrangements.png)
32+
33+
**Input:** n = 2
34+
35+
**Output:** 9
36+
37+
**Explanation:** The 9 possible arrangements are shown in the diagram above.
38+
39+
**Constraints:**
40+
41+
* <code>1 <= n <= 10<sup>4</sup></code>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g2301_2400.s2319_check_if_matrix_is_x_matrix;
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 checkXMatrix() {
11+
assertThat(
12+
new Solution()
13+
.checkXMatrix(
14+
new int[][] {
15+
{2, 0, 0, 1}, {0, 3, 1, 0}, {0, 5, 2, 0}, {4, 0, 0, 2}
16+
}),
17+
equalTo(true));
18+
}
19+
20+
@Test
21+
void checkXMatrix2() {
22+
assertThat(
23+
new Solution().checkXMatrix(new int[][] {{5, 7, 0}, {0, 3, 1}, {0, 5, 0}}),
24+
equalTo(false));
25+
}
26+
27+
@Test
28+
void checkXMatrix3() {
29+
assertThat(
30+
new Solution()
31+
.checkXMatrix(
32+
new int[][] {
33+
{0, 0, 0, 0, 1},
34+
{0, 4, 0, 1, 0},
35+
{0, 0, 5, 0, 0},
36+
{0, 5, 0, 2, 0},
37+
{4, 0, 0, 0, 2}
38+
}),
39+
equalTo(false));
40+
}
41+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2301_2400.s2320_count_number_of_ways_to_place_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 countHousePlacements() {
11+
assertThat(new Solution().countHousePlacements(1), equalTo(4));
12+
}
13+
14+
@Test
15+
void countHousePlacements2() {
16+
assertThat(new Solution().countHousePlacements(2), equalTo(9));
17+
}
18+
}

0 commit comments

Comments
 (0)