Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added tasks 2132, 2133, 2134, 2135, 2136
  • Loading branch information
ThanhNIT committed Jun 4, 2022
commit 0e313acaca99dc7a60da5ab46aaa9215f6deb0bb
75 changes: 75 additions & 0 deletions src/main/java/g2101_2200/s2132_stamping_the_grid/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package g2101_2200.s2132_stamping_the_grid;

// #Hard #Array #Greedy #Matrix #Prefix_Sum #2022_06_04_Time_11_ms_(93.06%)_Space_129.3_MB_(71.53%)

public class Solution {
private boolean canPaved(int[][] grid, int is, int js, int ie, int je) {
for (int i = is; i <= ie; i++) {
for (int j = js; j <= je; j++) {
if(grid[i][j] == 1) {
return true;
}
}
}
return false;
}

public boolean possibleToStamp(int[][] grid, int h, int w) {
int rl = grid[0].length;
for (int i = 0; i < grid.length; i++) {
int[] row = grid[i];
int prev = -1;
for (int j = 0; j < row.length; j++) {
if(row[j] == 0) {
if(j == prev + 1 && j > 0 && j+w-1 < rl) {
if(i>0 && grid[i-1][j] == 1 && i+h-1 < grid.length && canPaved(grid, i, j, i + h - 1, j + w - 1)) {
return false;
}
if(i+1 < grid.length && grid[i+1][j] == 1 && i-h+1 >= 0 && canPaved(grid, i - h + 1, j, i, j + w - 1)) {
return false;
}
}

if(j+1 < rl && row[j+1]==1 && j-w+1 >= 0) {
if(i>0 && grid[i-1][j] == 1 && i+h-1 < grid.length && canPaved(grid, i, j - w + 1, i + h - 1, j)) {
return false;
}
if(i+1 < grid.length && grid[i+1][j] == 1 && i-h+1 >= 0 && canPaved(grid, i - h + 1, j - w + 1, i, j)) {
return false;
}
}
continue;
}

if(1 < j-prev && j-prev <= w) {
return false;
}

prev = j;
}
if(1 < row.length-prev && row.length-prev <= w) {
return false;
}
}

for (int i = 0; i < rl; i++) {
int prev = -1;
for (int j = 0; j < grid.length; j++) {
if(grid[j][i] == 0) {
continue;
}

if(1 < j-prev && j-prev <= h) {
return false;
}

prev = j;
}

if(1 < grid.length-prev && grid.length-prev <= h) {
return false;
}
}
return true;
}
}
45 changes: 45 additions & 0 deletions src/main/java/g2101_2200/s2132_stamping_the_grid/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
2132\. Stamping the Grid

Hard

You are given an `m x n` binary matrix `grid` where each cell is either `0` (empty) or `1` (occupied).

You are then given stamps of size `stampHeight x stampWidth`. We want to fit the stamps such that they follow the given **restrictions** and **requirements**:

1. Cover all the **empty** cells.
2. Do not cover any of the **occupied** cells.
3. We can put as **many** stamps as we want.
4. Stamps can **overlap** with each other.
5. Stamps are not allowed to be **rotated**.
6. Stamps must stay completely **inside** the grid.

Return `true` _if it is possible to fit the stamps while following the given restrictions and requirements. Otherwise, return_ `false`.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/11/03/ex1.png)

**Input:** grid = [[1,0,0,0],[1,0,0,0],[1,0,0,0],[1,0,0,0],[1,0,0,0]], stampHeight = 4, stampWidth = 3

**Output:** true

**Explanation:** We have two overlapping stamps (labeled 1 and 2 in the image) that are able to cover all the empty cells.

**Example 2:**

![](https://assets.leetcode.com/uploads/2021/11/03/ex2.png)

**Input:** grid = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]], stampHeight = 2, stampWidth = 2

**Output:** false

**Explanation:** There is no way to fit the stamps onto all the empty cells without the stamps going outside the grid.

**Constraints:**

* `m == grid.length`
* `n == grid[r].length`
* <code>1 <= m, n <= 10<sup>5</sup></code>
* <code>1 <= m * n <= 2 * 10<sup>5</sup></code>
* `grid[r][c]` is either `0` or `1`.
* <code>1 <= stampHeight, stampWidth <= 10<sup>5</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package g2101_2200.s2133_check_if_every_row_and_column_contains_all_numbers;

// #Easy #Array #Hash_Table #Matrix #2022_06_04_Time_32_ms_(64.12%)_Space_83_MB_(46.34%)

import java.util.HashSet;
import java.util.Set;

public class Solution {
public boolean checkValid(int[][] matrix) {
int n = matrix.length;
Set<Integer> set = new HashSet<>();
for (int[] ints : matrix) {
for (int anInt : ints) {
set.add(anInt);
}
if (set.size() != n) {
return false;
}
set.clear();
}
for(int i = 0; i<matrix[0].length; i++){
for (int[] ints : matrix) {
set.add(ints[i]);
}
if(set.size() != n){
return false;
}
set.clear();
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
2133\. Check if Every Row and Column Contains All Numbers

Easy

An `n x n` matrix is **valid** if every row and every column contains **all** the integers from `1` to `n` (**inclusive**).

Given an `n x n` integer matrix `matrix`, return `true` _if the matrix is **valid**._ Otherwise, return `false`.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/12/21/example1drawio.png)

**Input:** matrix = [[1,2,3],[3,1,2],[2,3,1]]

**Output:** true

**Explanation:** In this case, n = 3, and every row and column contains the numbers 1, 2, and 3. Hence, we return true.

**Example 2:**

![](https://assets.leetcode.com/uploads/2021/12/21/example2drawio.png)

**Input:** matrix = [[1,1,1],[1,2,3],[1,2,3]]

**Output:** false

**Explanation:** In this case, n = 3, but the first row and the first column do not contain the numbers 2 or 3. Hence, we return false.

**Constraints:**

* `n == matrix.length == matrix[i].length`
* `1 <= n <= 100`
* `1 <= matrix[i][j] <= n`
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package g2101_2200.s2134_minimum_swaps_to_group_all_1s_together_ii;

// #Medium #Array #Sliding_Window #2022_06_04_Time_11_ms_(72.59%)_Space_86.6_MB_(27.41%)

public class Solution {
public int minSwaps(int[] nums) {
int l = nums.length;
int[] ones = new int[l];
ones[0] = nums[0] == 1 ? 1 : 0;
for (int i = 1; i < l; i++) {
if (nums[i] == 1) {
ones[i] = ones[i - 1] + 1;
}
else {
ones[i] = ones[i - 1];
}
}

if (ones[l - 1] == l || ones[l - 1] == 0) {
return 0;
}

int ws = ones[l - 1];
int minSwaps = Integer.MAX_VALUE;
int si = 0, ei;

while (si < nums.length) {
ei = (si + ws - 1) % l;
int totalones;

if (ei >= si) {
totalones = ones[ei] - (si == 0 ? 0 : ones[si - 1]);
}
else {
totalones = ones[ei] + (ones[l - 1] - ones[si - 1]);
}

int swapsreq = ws - totalones;
if (swapsreq < minSwaps) {
minSwaps = swapsreq;
}

si++;
}

return minSwaps;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
2134\. Minimum Swaps to Group All 1's Together II

Medium

A **swap** is defined as taking two **distinct** positions in an array and swapping the values in them.

A **circular** array is defined as an array where we consider the **first** element and the **last** element to be **adjacent**.

Given a **binary** **circular** array `nums`, return _the minimum number of swaps required to group all_ `1`_'s present in the array together at **any location**_.

**Example 1:**

**Input:** nums = [0,1,0,1,1,0,0]

**Output:** 1

**Explanation:** Here are a few of the ways to group all the 1's together:

[0,0,1,1,1,0,0] using 1 swap.

[0,1,1,1,0,0,0] using 1 swap.

[1,1,0,0,0,0,1] using 2 swaps (using the circular property of the array).

There is no way to group all 1's together with 0 swaps.

Thus, the minimum number of swaps required is 1.

**Example 2:**

**Input:** nums = [0,1,1,1,0,0,1,1,0]

**Output:** 2

**Explanation:** Here are a few of the ways to group all the 1's together:

[1,1,1,0,0,0,0,1,1] using 2 swaps (using the circular property of the array).

[1,1,1,1,1,0,0,0,0] using 2 swaps.

There is no way to group all 1's together with 0 or 1 swaps.

Thus, the minimum number of swaps required is 2.

**Example 3:**

**Input:** nums = [1,1,0,0,1]

**Output:** 0

**Explanation:** All the 1's are already grouped together due to the circular property of the array.

Thus, the minimum number of swaps required is 0.

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* `nums[i]` is either `0` or `1`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package g2101_2200.s2135_count_words_obtained_after_adding_a_letter;

// #Medium #Array #String #Hash_Table #Sorting #Bit_Manipulation
// #2022_06_04_Time_67_ms_(93.08%)_Space_63.2_MB_(89.53%)

import java.util.HashSet;
import java.util.Set;

public class Solution {
private Set<Integer> set;

private void preprocess(String[] words) {
set = new HashSet<>();
for (String word : words) {
int bitMap = getBitMap(word);
set.add(bitMap);
}
}

private boolean matches(String word) {
return matches(getBitMap(word));
}

private boolean matches(int bitMap) {
return set.contains(bitMap);
}

private int getBitMap(String word) {
int result = 0;
for (int i = 0; i < word.length(); i++) {
int position = word.charAt(i) - 'a';

result |= (1 << position);
}

return result;
}

private int addBit(int bitMap, char c) {
int position = c - 'a';
bitMap |= (1 << position);
return bitMap;
}

private int removeBit(int bitMap, char c) {
int position = c - 'a';
bitMap &= ~(1 << position);
return bitMap;
}

public int wordCount(String[] startWords, String[] targetWords) {
if (startWords == null || startWords.length == 0) {
return 0;
}

if (targetWords == null || targetWords.length == 0) {
return 0;
}

preprocess(startWords);
int count = 0;
for (String word : targetWords) {
int bitMap = getBitMap(word);
for (int i = 0; i < word.length(); i++) {
bitMap = removeBit(bitMap, word.charAt(i));
if (i > 0) {
bitMap = addBit(bitMap, word.charAt(i - 1));
}

if (matches(bitMap)) {
count++;
break;
}
}
}

return count;
}
}
Loading