Skip to content

Commit 0efd0e5

Browse files
authored
Added tasks 3127-3149
1 parent 781e51f commit 0efd0e5

File tree

30 files changed

+2020
-241
lines changed
  • src/main/java
    • g0001_0100
    • g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal
    • g0201_0300/s0232_implement_queue_using_stacks
    • g0301_0400/s0315_count_of_smaller_numbers_after_self
    • g0901_1000/s0990_satisfiability_of_equality_equations
    • g1101_1200/s1131_maximum_of_absolute_value_expression
    • g1301_1400/s1391_check_if_there_is_a_valid_path_in_a_grid
    • g2301_2400/s2397_maximum_rows_covered_by_columns
    • g3101_3200
      • s3127_make_a_square_with_the_same_color
      • s3128_right_triangles
      • s3129_find_all_possible_stable_binary_arrays_i
      • s3130_find_all_possible_stable_binary_arrays_ii
      • s3131_find_the_integer_added_to_array_i
      • s3132_find_the_integer_added_to_array_ii
      • s3133_minimum_array_end
      • s3134_find_the_median_of_the_uniqueness_array
      • s3136_valid_word
      • s3137_minimum_number_of_operations_to_make_word_k_periodic
      • s3138_minimum_length_of_anagram_concatenation
      • s3139_minimum_cost_to_equalize_array
      • s3142_check_if_grid_satisfies_conditions
      • s3143_maximum_points_inside_the_square
      • s3144_minimum_substring_partition_of_equal_character_frequency
      • s3145_find_products_of_elements_of_big_array
      • s3146_permutation_difference_between_two_strings
      • s3147_taking_maximum_energy_from_the_mystic_dungeon
      • s3148_maximum_difference_score_in_a_grid
      • s3149_find_the_minimum_cost_array_permutation

30 files changed

+2020
-241
lines changed

README.md

Lines changed: 169 additions & 149 deletions
Large diffs are not rendered by default.

src/main/java/g0001_0100/s0002_add_two_numbers/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public class Solution {
125125
solution.printList(result2);
126126

127127
ListNode l5 = new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9)))))));
128-
ListNode l6 = new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9)))));
128+
ListNode l6 = new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9))));
129129
ListNode result3 = solution.addTwoNumbers(l5, l6);
130130
System.out.print("Example 3 Output: ");
131131
solution.printList(result3);

src/main/java/g0001_0100/s0093_restore_ip_addresses/readme.md

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,36 +53,48 @@ import java.util.ArrayList;
5353
import java.util.List;
5454

5555
public class Solution {
56+
private static final int SEG_COUNT = 4;
57+
private List<String> result = new ArrayList<>();
58+
private int[] segments = new int[SEG_COUNT];
59+
5660
public List<String> restoreIpAddresses(String s) {
57-
List<String> results = new ArrayList<>();
58-
step(s, 0, new int[4], 0, results);
59-
return results;
61+
dfs(s, 0, 0);
62+
return result;
6063
}
6164

62-
void step(String s, int pos, int[] octets, int count, List<String> results) {
63-
if (count == 4 && pos == s.length()) {
64-
results.add(
65-
String.valueOf(octets[0])
66-
+ '.'
67-
+ octets[1]
68-
+ '.'
69-
+ octets[2]
70-
+ '.'
71-
+ octets[3]);
72-
} else if (count < 4 && pos < 12) {
73-
int octet = 0;
74-
for (int i = 0; i < 3; i++) {
75-
if (pos + i < s.length()) {
76-
int digit = s.charAt(pos + i) - '0';
77-
octet = octet * 10 + digit;
78-
if (octet < 256) {
79-
octets[count] = octet;
80-
step(s, pos + i + 1, octets, count + 1, results);
81-
}
82-
if (i == 0 && digit == 0) {
83-
break;
65+
public void dfs(String s, int segId, int segStart) {
66+
// find 4 segments and get to last index
67+
if (segId == SEG_COUNT) {
68+
if (segStart == s.length()) {
69+
StringBuilder addr = new StringBuilder();
70+
for (int i = 0; i < SEG_COUNT; i++) {
71+
addr.append(segments[i]);
72+
if (i != SEG_COUNT - 1) {
73+
addr.append('.');
8474
}
8575
}
76+
result.add(addr.toString());
77+
}
78+
return;
79+
}
80+
// last index and no 4 segments
81+
if (segStart == s.length()) {
82+
return;
83+
}
84+
// start with a zero
85+
if (s.charAt(segStart) == '0') {
86+
segments[segId] = 0;
87+
dfs(s, segId + 1, segStart + 1);
88+
return;
89+
}
90+
int addr = 0;
91+
for (int index = segStart; index < s.length(); index++) {
92+
addr = addr * 10 + s.charAt(index) - '0';
93+
if (addr >= 0 && addr <= 255) {
94+
segments[segId] = addr;
95+
dfs(s, segId + 1, index + 1);
96+
} else {
97+
break;
8698
}
8799
}
88100
}

src/main/java/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class Solution {
6666
}
6767

6868
// Recursive helper method to construct binary tree
69-
private TreeNode build(int[] preorder, int[] inorder, int preStart, preEnd, int inStart, int inEnd) {
69+
private TreeNode build(int[] preorder, int[] inorder, int preStart, int preEnd, int inStart, int inEnd) {
7070
if (preStart > preEnd || inStart > inEnd) return null; // Base case
7171

7272
int rootValue = preorder[preStart]; // Root node value

src/main/java/g0201_0300/s0232_implement_queue_using_stacks/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import java.util.Deque;
5656
public class MyQueue {
5757
private Deque<Integer> left;
5858
private Deque<Integer> right;
59+
5960
// Initialize your data structure here.
6061
public MyQueue() {
6162
left = new ArrayDeque<>();

src/main/java/g0301_0400/s0315_count_of_smaller_numbers_after_self/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public class Solution {
7272
bit[i] += v;
7373
}
7474
}
75+
7576
// prefix sum query
7677
private int ps(int j) {
7778
int ps = 0;

src/main/java/g0901_1000/s0990_satisfiability_of_equality_equations/readme.md

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -39,59 +39,32 @@ There is no way to assign the variables to satisfy both equations.
3939
## Solution
4040

4141
```java
42-
import java.util.HashMap;
43-
4442
public class Solution {
45-
private int[] par;
43+
private int[] parent = new int[26];
4644

47-
public boolean equationsPossible(String[] equations) {
48-
int counter = 0;
49-
HashMap<Character, Integer> map = new HashMap<>();
50-
for (String str : equations) {
51-
char ch = str.charAt(0);
52-
if (!map.containsKey(ch)) {
53-
map.put(ch, counter);
54-
counter++;
55-
}
56-
ch = str.charAt(3);
57-
if (!map.containsKey(ch)) {
58-
map.put(ch, counter);
59-
counter++;
60-
}
45+
private int find(int x) {
46+
if (parent[x] == x) {
47+
return x;
6148
}
62-
par = new int[counter];
63-
for (int i = 0; i < par.length; i++) {
64-
par[i] = i;
49+
parent[x] = find(parent[x]);
50+
return parent[x];
51+
}
52+
53+
public boolean equationsPossible(String[] equations) {
54+
for (int i = 0; i < 26; i++) {
55+
parent[i] = i;
6556
}
66-
for (String str : equations) {
67-
String oper = str.substring(1, 3);
68-
if (oper.equals("==")) {
69-
int px = find(map.get(str.charAt(0)));
70-
int py = find(map.get(str.charAt(3)));
71-
if (px != py) {
72-
par[px] = py;
73-
}
57+
for (String e : equations) {
58+
if (e.charAt(1) == '=') {
59+
parent[find(e.charAt(0) - 'a')] = find(e.charAt(3) - 'a');
7460
}
7561
}
76-
for (String str : equations) {
77-
String oper = str.substring(1, 3);
78-
if (oper.equals("!=")) {
79-
int px = find(map.get(str.charAt(0)));
80-
int py = find(map.get(str.charAt(3)));
81-
if (px == py) {
82-
return false;
83-
}
62+
for (String e : equations) {
63+
if (e.charAt(1) == '!' && find(e.charAt(0) - 'a') == find(e.charAt(3) - 'a')) {
64+
return false;
8465
}
8566
}
8667
return true;
8768
}
88-
89-
private int find(int x) {
90-
if (par[x] == x) {
91-
return x;
92-
}
93-
par[x] = find(par[x]);
94-
return par[x];
95-
}
9669
}
9770
```

src/main/java/g1101_1200/s1131_maximum_of_absolute_value_expression/readme.md

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,30 @@ where the maximum is taken over all `0 <= i, j < arr1.length`.
3232

3333
```java
3434
public class Solution {
35-
public int maxAbsValExpr(int[] arr1, int[] arr2) {
36-
if (arr1.length != arr2.length) {
35+
private int max(int[] a1, int[] a2, int k1, int k2, int k3) {
36+
int result = Integer.MIN_VALUE;
37+
for (int i = 0; i < a1.length; i++) {
38+
result = Math.max(result, a1[i] * k1 + a2[i] * k2 + i * k3);
39+
}
40+
return result;
41+
}
42+
43+
private int min(int[] a1, int[] a2, int k1, int k2, int k3) {
44+
return -max(a1, a2, -k1, -k2, -k3);
45+
}
46+
47+
public int maxAbsValExpr(int[] a1, int[] a2) {
48+
if (a1 == null || a2 == null || a1.length == 0 || a2.length == 0) {
3749
return 0;
3850
}
39-
int max1 = Integer.MIN_VALUE;
40-
int max2 = Integer.MIN_VALUE;
41-
int max3 = Integer.MIN_VALUE;
42-
int max4 = Integer.MIN_VALUE;
43-
int min1 = Integer.MAX_VALUE;
44-
int min2 = Integer.MAX_VALUE;
45-
int min3 = Integer.MAX_VALUE;
46-
int min4 = Integer.MAX_VALUE;
47-
for (int i = 0; i < arr1.length; i++) {
48-
max1 = Math.max(arr1[i] + arr2[i] + i, max1);
49-
min1 = Math.min(arr1[i] + arr2[i] + i, min1);
50-
max2 = Math.max(i - arr1[i] - arr2[i], max2);
51-
min2 = Math.min(i - arr1[i] - arr2[i], min2);
52-
max3 = Math.max(arr1[i] - arr2[i] + i, max3);
53-
min3 = Math.min(arr1[i] - arr2[i] + i, min3);
54-
max4 = Math.max(arr2[i] - arr1[i] + i, max4);
55-
min4 = Math.min(arr2[i] - arr1[i] + i, min4);
51+
int result = 0;
52+
int[][] ksArray = { {1, 1, 1}, {1, 1, -1}, {1, -1, 1}, {1, -1, -1}};
53+
for (int[] ks : ksArray) {
54+
int max = max(a1, a2, ks[0], ks[1], ks[2]);
55+
int min = min(a1, a2, ks[0], ks[1], ks[2]);
56+
result = Math.max(result, max - min);
5657
}
57-
return Math.max(Math.max(max1 - min1, max2 - min2), Math.max(max3 - min3, max4 - min4));
58+
return result;
5859
}
5960
}
6061
```

src/main/java/g1301_1400/s1391_check_if_there_is_a_valid_path_in_a_grid/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public class Solution {
7272
{ {0, -1}, {-1, 0}},
7373
{ {0, 1}, {-1, 0}}
7474
};
75+
7576
// the idea is you need to check port direction match, you can go to next cell and check whether
7677
// you can come back.
7778
public boolean hasValidPath(int[][] grid) {

src/main/java/g2301_2400/s2397_maximum_rows_covered_by_columns/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public class Solution {
6767
private int ans = 0;
6868

6969
public int maximumRows(int[][] matrix, int numSelect) {
70-
dfs(matrix, /*colIndex=*/ 0, numSelect, /*mask=*/ 0);
70+
dfs(matrix, /* colIndex= */ 0, numSelect, /* mask= */ 0);
7171
return ans;
7272
}
7373

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
## 3127\. Make a Square with the Same Color
5+
6+
Easy
7+
8+
You are given a 2D matrix `grid` of size `3 x 3` consisting only of characters `'B'` and `'W'`. Character `'W'` represents the white color, and character `'B'` represents the black color.
9+
10+
Your task is to change the color of **at most one** cell so that the matrix has a `2 x 2` square where all cells are of the same color.
11+
12+
Return `true` if it is possible to create a `2 x 2` square of the same color, otherwise, return `false`.
13+
14+
**Example 1:**
15+
16+
**Input:** grid = \[\["B","W","B"],["B","W","W"],["B","W","B"]]
17+
18+
**Output:** true
19+
20+
**Explanation:**
21+
22+
It can be done by changing the color of the `grid[0][2]`.
23+
24+
**Example 2:**
25+
26+
**Input:** grid = \[\["B","W","B"],["W","B","W"],["B","W","B"]]
27+
28+
**Output:** false
29+
30+
**Explanation:**
31+
32+
It cannot be done by changing at most one cell.
33+
34+
**Example 3:**
35+
36+
**Input:** grid = \[\["B","W","B"],["B","W","W"],["B","W","W"]]
37+
38+
**Output:** true
39+
40+
**Explanation:**
41+
42+
The `grid` already contains a `2 x 2` square of the same color.
43+
44+
**Constraints:**
45+
46+
* `grid.length == 3`
47+
* `grid[i].length == 3`
48+
* `grid[i][j]` is either `'W'` or `'B'`.
49+
50+
## Solution
51+
52+
```java
53+
public class Solution {
54+
public boolean canMakeSquare(char[][] grid) {
55+
int n = grid.length;
56+
int m = grid[0].length;
57+
for (int i = 0; i < n - 1; i++) {
58+
for (int j = 0; j < m - 1; j++) {
59+
int countBlack = 0;
60+
int countWhite = 0;
61+
for (int k = i; k <= i + 1; k++) {
62+
for (int l = j; l <= j + 1; l++) {
63+
if (grid[k][l] == 'W') {
64+
countWhite++;
65+
} else {
66+
countBlack++;
67+
}
68+
}
69+
}
70+
if (countBlack >= 3 || countWhite >= 3) {
71+
return true;
72+
}
73+
}
74+
}
75+
return false;
76+
}
77+
}
78+
```

0 commit comments

Comments
 (0)