πŸŒ“

653. Two Sum IV - Input is a BST

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public boolean findTarget(TreeNode root, int k) {
List<Integer> sortedList = new ArrayList<>();
dfs(sortedList, root);
int left = 0, right = sortedList.size() - 1;
while (left < right) {
int currSum = sortedList.get(left) + sortedList.get(right);
if (currSum < k) {
left += 1;
} else if (currSum > k) {
right -= 1;
} else {
return true;
}
}
return false;
}

private void dfs(List<Integer> sortedList, TreeNode node) {
if (node == null) {
return;
}
dfs(sortedList, node.left);
sortedList.add(node.val);
dfs(sortedList, node.right);
}
}

Read More

172. Factorial Trailing Zeroes

1
2
3
4
5
6
7
8
9
10
class Solution {
public int trailingZeroes(int n) {
int ans = 0, currFactor = 5;
while (n / currFactor > 0) {
ans += n / currFactor;
currFactor *= 5;
}
return ans;
}
}

Read More

267. Palindrome Permutation II

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Solution {
public List<String> generatePalindromes(String s) {
int[] charCount = new int[26];
int oddCount = 0;
for (char letter : s.toCharArray()) {
charCount[letter - 'a'] += 1;
if (charCount[letter - 'a'] % 2 == 1) {
oddCount += 1;
} else {
oddCount -= 1;
}
}
if (oddCount > 1) {
return new ArrayList<>();
}

String oddLetter = "";
List<Character> candidates = new ArrayList<>();
for (int i = 0; i < charCount.length; i++) {
char currLetter = (char) (i + 'a');
if (charCount[i] % 2 != 0) {
oddLetter = String.valueOf(currLetter);
charCount[i] -= 1;
}
for (int j = 0; j < charCount[i] / 2; j++) {
candidates.add(currLetter);
}
}
List<String> ans = new ArrayList<>();
dfs(ans, candidates, new StringBuilder(), new boolean[candidates.size()], oddLetter);
return ans;
}

private void dfs(List<String> ans, List<Character> candidates, StringBuilder curr, boolean[] used, String mid) {
if (curr.length() == candidates.size()) {
ans.add(curr.toString() + mid + curr.reverse().toString());
curr.reverse();
return;
}
for (int i = 0; i < candidates.size(); i++) {
if (used[i] || (i > 0 && candidates.get(i) == candidates.get(i - 1) && !used[i - 1])) {
continue;
}
curr.append(candidates.get(i));
used[i] = true;
dfs(ans, candidates, curr, used, mid);
used[i] = false;
curr.setLength(curr.length() - 1);
}
}
}

Read More

259. 3Sum Smaller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public int threeSumSmaller(int[] nums, int target) {
Arrays.sort(nums);
int ans = 0;
for (int i = 0; i < nums.length - 2; i++) {
int left = i + 1, right = nums.length - 1;
while (left < right) {
if (nums[left] + nums[right] + nums[i] < target) {
ans += right - left;
left += 1;
} else {
right -= 1;
}
}
}
return ans;
}
}

Read More

255. Verify Preorder Sequence in Binary Search Tree

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
private int ptr;

public boolean verifyPreorder(int[] preorder) {
ptr = 0;
dfs(preorder, Integer.MIN_VALUE, Integer.MAX_VALUE);
return ptr == preorder.length;
}

private void dfs(int[] preorder, int low, int up) {
if (ptr == preorder.length || preorder[ptr] < low || preorder[ptr] > up) {
return;
}
int currVal = preorder[ptr++];
dfs(preorder, low, currVal);
dfs(preorder, currVal, up);
}
}

Read More

254. Factor Combinations

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
public List<List<Integer>> getFactors(int n) {
List<List<Integer>> results = new ArrayList<>();
if (n <= 3) {
return results;
}

getFactors(n, 2, new ArrayList<Integer>(), results);
return results;
}

private void getFactors(int n, int start, List<Integer> current, List<List<Integer>> results) {
if (n == 1) {
// Factor should be in range of [2, n] therefore if there is only one facotr in
// the current list, we shouldn't consider this case
if (current.size() > 1) {
results.add(new ArrayList<Integer>(current));
}
return;
}

for (int i = start; i <= (int) Math.sqrt(n); i++) { // ==> here, change 1
if (n % i != 0) {
continue;
}
current.add(i);
getFactors(n / i, i, current, results);
current.remove(current.size() - 1);
}

int i = n; // ===> here, change 2
current.add(i);
getFactors(n / i, i, current, results);
current.remove(current.size() - 1);
}
}

Read More

223. Rectangle Area

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
int[][] xIntervals = new int[][] { { ax1, ax2 }, { bx1, bx2 } };
int[][] yIntervals = new int[][] { { ay1, ay2 }, { by1, by2 } };
Arrays.sort(xIntervals, (a, b) -> a[0] - b[0]);
Arrays.sort(yIntervals, (a, b) -> a[0] - b[0]);
int areaSum = (ax2 - ax1) * (ay2 - ay1) + (bx2 - bx1) * (by2 - by1);
if (xIntervals[1][0] >= xIntervals[0][1] || yIntervals[1][0] >= yIntervals[0][1]) {
return areaSum;
}
int leftBound = xIntervals[1][0];
int rightBound = Math.min(xIntervals[0][1], xIntervals[1][1]);
int lowBound = yIntervals[1][0];
int upBound = Math.min(yIntervals[0][1], yIntervals[1][1]);
return areaSum - (rightBound - leftBound) * (upBound - lowBound);
}
}

Read More

212. Word Search II

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Solution {
private static class TrieNode {
Map<Character, TrieNode> children;
String word;

TrieNode() {
this.children = new HashMap<>();
word = null;
}
}

private void addWord(String word, TrieNode root) {
TrieNode ptr = root;
for (char letter : word.toCharArray()) {
ptr.children.putIfAbsent(letter, new TrieNode());
ptr = ptr.children.get(letter);
}
ptr.word = word;
}

private int[][] directions = new int[][] { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };

public List<String> findWords(char[][] board, String[] words) {
List<String> ans = new ArrayList<>();
TrieNode root = new TrieNode();
for (String word : words) {
addWord(word, root);
}
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (root.children.containsKey(board[i][j])) {
backtrack(board, ans, i, j, root);
}
}
}
return ans;
}

private void backtrack(char[][] board, List<String> ans, int row, int col, TrieNode parent) {
char currLetter = board[row][col];
TrieNode currNode = parent.children.get(currLetter);
if (currNode.word != null) {
ans.add(currNode.word);
currNode.word = null;
}
board[row][col] = '#';
for (int[] direction : directions) {
int newRow = row + direction[0];
int newCol = col + direction[1];
if (!isOutOfBound(board.length, board[0].length, newRow, newCol)
&& currNode.children.containsKey(board[newRow][newCol])) {
backtrack(board, ans, newRow, newCol, currNode);
}
}
board[row][col] = currLetter;
if (currNode.children.isEmpty()) {
parent.children.remove(currLetter);
}
}

private boolean isOutOfBound(int rowSize, int colSize, int row, int col) {
return row < 0 || row >= rowSize || col < 0 || col >= colSize;
}
}

Read More

1444. Number of Ways of Cutting a Pizza

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution {
private int modulo = (int) 1e9 + 7;

public int ways(String[] pizza, int k) {
int m = pizza.length, n = pizza[0].length();
int[][] prefixSum = new int[m + 1][n + 1];
Integer[][][] dp = new Integer[k][m][n];
for (int i = m - 1; i >= 0; i--) {
int currSum = 0;
for (int j = n - 1; j >= 0; j--) {
currSum += pizza[i].charAt(j) == 'A' ? 1 : 0;
prefixSum[i][j] += prefixSum[i + 1][j] + currSum;
}
}
return dfs(m, n, 0, 0, k - 1, prefixSum, dp);
}

private int dfs(int m, int n, int row, int col, int k, int[][] prefixSum, Integer[][][] dp) {
if (prefixSum[row][col] == 0)
return 0;
if (k == 0)
return 1;
if (dp[k][row][col] != null) {
return dp[k][row][col];
}
int ans = 0;
for (int newRow = row + 1; newRow < m; newRow++) {
if (prefixSum[row][col] - prefixSum[newRow][col] > 0) {
ans = (ans + dfs(m, n, newRow, col, k - 1, prefixSum, dp)) % modulo;
}
}
for (int newCol = col + 1; newCol < n; newCol++) {
if (prefixSum[row][col] - prefixSum[row][newCol] > 0) {
ans = (ans + dfs(m, n, row, newCol, k - 1, prefixSum, dp)) % modulo;
}
}
return dp[k][row][col] = ans;
}
}

Read More

623. Add One Row to Tree

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public TreeNode addOneRow(TreeNode root, int val, int depth) {
TreeNode dummy = new TreeNode(0);
dummy.left = root;
dfs(dummy, 0, depth - 1, val);
return dummy.left;
}

private void dfs(TreeNode node, int currDepth, int targetDepth, int val) {
if (node == null) {
return;
}
if (currDepth == targetDepth) {
TreeNode currLeftTree = node.left;
TreeNode currRightTree = node.right;
TreeNode newLeftTree = new TreeNode(val);
TreeNode newRightTree = new TreeNode(val);
node.left = newLeftTree;
newLeftTree.left = currLeftTree;
node.right = newRightTree;
newRightTree.right = currRightTree;
} else {
dfs(node.left, currDepth + 1, targetDepth, val);
dfs(node.right, currDepth + 1, targetDepth, val);
}
}
}

Read More