Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
318 changes: 169 additions & 149 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/main/java/g0001_0100/s0002_add_two_numbers/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public class Solution {
solution.printList(result2);

ListNode l5 = new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9)))))));
ListNode l6 = new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9)))));
ListNode l6 = new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9))));
ListNode result3 = solution.addTwoNumbers(l5, l6);
System.out.print("Example 3 Output: ");
solution.printList(result3);
Expand Down
62 changes: 37 additions & 25 deletions src/main/java/g0001_0100/s0093_restore_ip_addresses/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,36 +53,48 @@ import java.util.ArrayList;
import java.util.List;

public class Solution {
private static final int SEG_COUNT = 4;
private List<String> result = new ArrayList<>();
private int[] segments = new int[SEG_COUNT];

public List<String> restoreIpAddresses(String s) {
List<String> results = new ArrayList<>();
step(s, 0, new int[4], 0, results);
return results;
dfs(s, 0, 0);
return result;
}

void step(String s, int pos, int[] octets, int count, List<String> results) {
if (count == 4 && pos == s.length()) {
results.add(
String.valueOf(octets[0])
+ '.'
+ octets[1]
+ '.'
+ octets[2]
+ '.'
+ octets[3]);
} else if (count < 4 && pos < 12) {
int octet = 0;
for (int i = 0; i < 3; i++) {
if (pos + i < s.length()) {
int digit = s.charAt(pos + i) - '0';
octet = octet * 10 + digit;
if (octet < 256) {
octets[count] = octet;
step(s, pos + i + 1, octets, count + 1, results);
}
if (i == 0 && digit == 0) {
break;
public void dfs(String s, int segId, int segStart) {
// find 4 segments and get to last index
if (segId == SEG_COUNT) {
if (segStart == s.length()) {
StringBuilder addr = new StringBuilder();
for (int i = 0; i < SEG_COUNT; i++) {
addr.append(segments[i]);
if (i != SEG_COUNT - 1) {
addr.append('.');
}
}
result.add(addr.toString());
}
return;
}
// last index and no 4 segments
if (segStart == s.length()) {
return;
}
// start with a zero
if (s.charAt(segStart) == '0') {
segments[segId] = 0;
dfs(s, segId + 1, segStart + 1);
return;
}
int addr = 0;
for (int index = segStart; index < s.length(); index++) {
addr = addr * 10 + s.charAt(index) - '0';
if (addr >= 0 && addr <= 255) {
segments[segId] = addr;
dfs(s, segId + 1, index + 1);
} else {
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Solution {
}

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

int rootValue = preorder[preStart]; // Root node value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import java.util.Deque;
public class MyQueue {
private Deque<Integer> left;
private Deque<Integer> right;

// Initialize your data structure here.
public MyQueue() {
left = new ArrayDeque<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public class Solution {
bit[i] += v;
}
}

// prefix sum query
private int ps(int j) {
int ps = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,59 +39,32 @@ There is no way to assign the variables to satisfy both equations.
## Solution

```java
import java.util.HashMap;

public class Solution {
private int[] par;
private int[] parent = new int[26];

public boolean equationsPossible(String[] equations) {
int counter = 0;
HashMap<Character, Integer> map = new HashMap<>();
for (String str : equations) {
char ch = str.charAt(0);
if (!map.containsKey(ch)) {
map.put(ch, counter);
counter++;
}
ch = str.charAt(3);
if (!map.containsKey(ch)) {
map.put(ch, counter);
counter++;
}
private int find(int x) {
if (parent[x] == x) {
return x;
}
par = new int[counter];
for (int i = 0; i < par.length; i++) {
par[i] = i;
parent[x] = find(parent[x]);
return parent[x];
}

public boolean equationsPossible(String[] equations) {
for (int i = 0; i < 26; i++) {
parent[i] = i;
}
for (String str : equations) {
String oper = str.substring(1, 3);
if (oper.equals("==")) {
int px = find(map.get(str.charAt(0)));
int py = find(map.get(str.charAt(3)));
if (px != py) {
par[px] = py;
}
for (String e : equations) {
if (e.charAt(1) == '=') {
parent[find(e.charAt(0) - 'a')] = find(e.charAt(3) - 'a');
}
}
for (String str : equations) {
String oper = str.substring(1, 3);
if (oper.equals("!=")) {
int px = find(map.get(str.charAt(0)));
int py = find(map.get(str.charAt(3)));
if (px == py) {
return false;
}
for (String e : equations) {
if (e.charAt(1) == '!' && find(e.charAt(0) - 'a') == find(e.charAt(3) - 'a')) {
return false;
}
}
return true;
}

private int find(int x) {
if (par[x] == x) {
return x;
}
par[x] = find(par[x]);
return par[x];
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,30 @@ where the maximum is taken over all `0 <= i, j < arr1.length`.

```java
public class Solution {
public int maxAbsValExpr(int[] arr1, int[] arr2) {
if (arr1.length != arr2.length) {
private int max(int[] a1, int[] a2, int k1, int k2, int k3) {
int result = Integer.MIN_VALUE;
for (int i = 0; i < a1.length; i++) {
result = Math.max(result, a1[i] * k1 + a2[i] * k2 + i * k3);
}
return result;
}

private int min(int[] a1, int[] a2, int k1, int k2, int k3) {
return -max(a1, a2, -k1, -k2, -k3);
}

public int maxAbsValExpr(int[] a1, int[] a2) {
if (a1 == null || a2 == null || a1.length == 0 || a2.length == 0) {
return 0;
}
int max1 = Integer.MIN_VALUE;
int max2 = Integer.MIN_VALUE;
int max3 = Integer.MIN_VALUE;
int max4 = Integer.MIN_VALUE;
int min1 = Integer.MAX_VALUE;
int min2 = Integer.MAX_VALUE;
int min3 = Integer.MAX_VALUE;
int min4 = Integer.MAX_VALUE;
for (int i = 0; i < arr1.length; i++) {
max1 = Math.max(arr1[i] + arr2[i] + i, max1);
min1 = Math.min(arr1[i] + arr2[i] + i, min1);
max2 = Math.max(i - arr1[i] - arr2[i], max2);
min2 = Math.min(i - arr1[i] - arr2[i], min2);
max3 = Math.max(arr1[i] - arr2[i] + i, max3);
min3 = Math.min(arr1[i] - arr2[i] + i, min3);
max4 = Math.max(arr2[i] - arr1[i] + i, max4);
min4 = Math.min(arr2[i] - arr1[i] + i, min4);
int result = 0;
int[][] ksArray = { {1, 1, 1}, {1, 1, -1}, {1, -1, 1}, {1, -1, -1}};
for (int[] ks : ksArray) {
int max = max(a1, a2, ks[0], ks[1], ks[2]);
int min = min(a1, a2, ks[0], ks[1], ks[2]);
result = Math.max(result, max - min);
}
return Math.max(Math.max(max1 - min1, max2 - min2), Math.max(max3 - min3, max4 - min4));
return result;
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public class Solution {
{ {0, -1}, {-1, 0}},
{ {0, 1}, {-1, 0}}
};

// the idea is you need to check port direction match, you can go to next cell and check whether
// you can come back.
public boolean hasValidPath(int[][] grid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class Solution {
private int ans = 0;

public int maximumRows(int[][] matrix, int numSelect) {
dfs(matrix, /*colIndex=*/ 0, numSelect, /*mask=*/ 0);
dfs(matrix, /* colIndex= */ 0, numSelect, /* mask= */ 0);
return ans;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
[![](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)

## 3127\. Make a Square with the Same Color

Easy

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.

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.

Return `true` if it is possible to create a `2 x 2` square of the same color, otherwise, return `false`.

**Example 1:**

**Input:** grid = \[\["B","W","B"],["B","W","W"],["B","W","B"]]

**Output:** true

**Explanation:**

It can be done by changing the color of the `grid[0][2]`.

**Example 2:**

**Input:** grid = \[\["B","W","B"],["W","B","W"],["B","W","B"]]

**Output:** false

**Explanation:**

It cannot be done by changing at most one cell.

**Example 3:**

**Input:** grid = \[\["B","W","B"],["B","W","W"],["B","W","W"]]

**Output:** true

**Explanation:**

The `grid` already contains a `2 x 2` square of the same color.

**Constraints:**

* `grid.length == 3`
* `grid[i].length == 3`
* `grid[i][j]` is either `'W'` or `'B'`.

## Solution

```java
public class Solution {
public boolean canMakeSquare(char[][] grid) {
int n = grid.length;
int m = grid[0].length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < m - 1; j++) {
int countBlack = 0;
int countWhite = 0;
for (int k = i; k <= i + 1; k++) {
for (int l = j; l <= j + 1; l++) {
if (grid[k][l] == 'W') {
countWhite++;
} else {
countBlack++;
}
}
}
if (countBlack >= 3 || countWhite >= 3) {
return true;
}
}
}
return false;
}
}
```
Loading