Skip to content

Commit 3ca49ed

Browse files
committed
Update
1 parent a293914 commit 3ca49ed

12 files changed

+624
-1
lines changed

src/array/RotateArray.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package array;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/rotate-array
5+
* Difficulty: Medium
6+
*/
7+
public class RotateArray {
8+
9+
/**
10+
* Time complexity: O(n)
11+
* Space complexity: O(n)
12+
*/
13+
public void rotateViaExtraArray(int[] nums, int k) {
14+
k = k % nums.length;
15+
if (k == 0) return;
16+
17+
int[] tmp = new int[nums.length];
18+
for (int i = 0; i < nums.length; i++) {
19+
tmp[(i + k) % tmp.length] = nums[i];
20+
}
21+
22+
for (int i = 0; i < tmp.length; i++) {
23+
nums[i] = tmp[i];
24+
}
25+
}
26+
27+
/**
28+
* Time complexity: O(n)
29+
* Space complexity: O(1)
30+
*/
31+
public void rotateViaTripleReversal(int[] nums, int k) {
32+
k = k % nums.length;
33+
if (k == 0) return;
34+
// k = 3
35+
reverse(nums, 0, nums.length - 1); // 1 2 3 4 5 -> 5 4 3 2 1
36+
reverse(nums, 0, k - 1); // 5 4 3 2 1 -> 3 4 5 2 1
37+
reverse(nums, k, nums.length - 1); // 3 4 5 2 1 -> 3 4 5 1 2
38+
}
39+
40+
private void reverse(int[] nums, int left, int right) {
41+
while (left < right) {
42+
swap(nums, left, right);
43+
left++;
44+
right--;
45+
}
46+
}
47+
48+
private void swap(int[] nums, int i, int j) {
49+
int tmp = nums[i];
50+
nums[i] = nums[j];
51+
nums[j] = tmp;
52+
}
53+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package binary_search;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/koko-eating-bananas
5+
* Difficulty: Medium
6+
* Time complexity: O(n log(Integer.MAX_VALUE))
7+
* Space complexity: O(1)
8+
*/
9+
public class KokoEatingBananas {
10+
11+
public int minEatingSpeed(int[] piles, int hours) {
12+
int minSpeed = 1;
13+
int maxSpeed = Integer.MAX_VALUE;
14+
15+
int min = Integer.MAX_VALUE;
16+
while (minSpeed <= maxSpeed) {
17+
int midSpeed = minSpeed + (maxSpeed - minSpeed) / 2;
18+
19+
if (canEatAllBananas(piles, hours, midSpeed)) {
20+
min = Math.min(min, midSpeed);
21+
maxSpeed = midSpeed - 1;
22+
} else {
23+
minSpeed = midSpeed + 1;
24+
}
25+
}
26+
27+
return min;
28+
}
29+
30+
private boolean canEatAllBananas(int[] piles, int totalTime, int speed) {
31+
int currentTime = 0;
32+
33+
for (int pile : piles) {
34+
currentTime += pile / speed;
35+
currentTime += pile % speed == 0 ? 0 : 1;
36+
if (currentTime > totalTime) return false;
37+
}
38+
39+
return true;
40+
}
41+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package binary_search;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/maximum-candies-allocated-to-k-children
5+
* Difficulty: Medium
6+
* Time complexity: O(n log(Integer.MAX_VALUE))
7+
* Space complexity: O(1)
8+
*/
9+
public class MaximumCandiesAllocatedToKChildren {
10+
11+
public int maximumCandies(int[] candies, long children) {
12+
int minAllocation = 0;
13+
int maxAllocation = Integer.MAX_VALUE;
14+
15+
// Possible optimization:
16+
// int maxAllocation = Arrays.stream(candies).max().getAsInt();
17+
18+
int max = 0;
19+
while (minAllocation <= maxAllocation) {
20+
int midAllocation = minAllocation + (maxAllocation - minAllocation) / 2;
21+
22+
if (canAllocateCandyToEveryChild(candies, children, midAllocation)) {
23+
max = Math.max(max, midAllocation);
24+
minAllocation = midAllocation + 1;
25+
} else {
26+
maxAllocation = midAllocation - 1;
27+
}
28+
}
29+
30+
return max;
31+
}
32+
33+
private boolean canAllocateCandyToEveryChild(int[] candies, long totalChildren, int candiesPerChild) {
34+
if (candiesPerChild == 0) return true;
35+
36+
long childrenWithCandies = 0;
37+
for (int pile : candies) {
38+
childrenWithCandies += pile / candiesPerChild;
39+
if (childrenWithCandies >= totalChildren) return true;
40+
}
41+
42+
return false;
43+
}
44+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package binary_search_tree;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Description: https://leetcode.com/problems/kth-smallest-element-in-a-bst
7+
* Difficulty: Medium
8+
*/
9+
public class KthSmallestElementInBST {
10+
11+
/**
12+
* Time complexity: O(h + k)
13+
* Space complexity: O(h)
14+
*/
15+
public int kthSmallestViaIterativeInorderTraversal(TreeNode root, int k) {
16+
Deque<TreeNode> stack = new LinkedList<>();
17+
18+
while (root != null || !stack.isEmpty()) {
19+
if (root != null) {
20+
stack.push(root);
21+
root = root.left;
22+
} else {
23+
root = stack.pop();
24+
if (--k == 0) return root.val;
25+
root = root.right;
26+
}
27+
}
28+
29+
return -1;
30+
}
31+
32+
/**
33+
* Time complexity: O(n)
34+
* Space complexity: O(n)
35+
*/
36+
public int kthSmallestViaRecursiveInorderTraversal(TreeNode root, int k) {
37+
List<Integer> inorder = new ArrayList<>();
38+
traverseInorder(root, inorder);
39+
40+
return inorder.get(k - 1);
41+
}
42+
43+
private void traverseInorder(TreeNode root, List<Integer> result) {
44+
if (root == null) return;
45+
46+
traverseInorder(root.left, result);
47+
result.add(root.val);
48+
traverseInorder(root.right, result);
49+
}
50+
51+
/**
52+
* Time complexity: O(nlog k)
53+
* Space complexity: O(h + k)
54+
*/
55+
public int kthSmallestViaMaxHeap(TreeNode root, int k) {
56+
Queue<Integer> maxHeap = new PriorityQueue<>((a, b) -> Integer.compare(b, a));
57+
backtrack(root, maxHeap, k);
58+
59+
return maxHeap.peek();
60+
}
61+
62+
private void backtrack(TreeNode root, Queue<Integer> maxHeap, int k) {
63+
if (root == null) return;
64+
65+
maxHeap.offer(root.val);
66+
if (maxHeap.size() > k) maxHeap.poll();
67+
68+
backtrack(root.left, maxHeap, k);
69+
backtrack(root.right, maxHeap, k);
70+
}
71+
72+
private static class TreeNode {
73+
int val;
74+
TreeNode left;
75+
TreeNode right;
76+
}
77+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package binary_tree;
2+
3+
import java.util.Deque;
4+
import java.util.LinkedList;
5+
6+
/**
7+
* Description:https://leetcode.com/problems/binary-search-tree-iterator
8+
* Difficulty: Medium
9+
* Time complexity: O(1) on average for next(), O(1) for hasNext()
10+
* Space complexity: O(h)
11+
*/
12+
public class BinarySearchTreeIterator {
13+
14+
private static class BSTIterator {
15+
16+
private final Deque<TreeNode> stack;
17+
private TreeNode current;
18+
19+
public BSTIterator(TreeNode root) {
20+
this.stack = new LinkedList<>();
21+
this.current = root;
22+
}
23+
24+
public int next() {
25+
while (current != null) {
26+
stack.push(current);
27+
current = current.left;
28+
}
29+
30+
current = stack.pop();
31+
int val = current.val;
32+
current = current.right;
33+
34+
return val;
35+
}
36+
37+
public boolean hasNext() {
38+
return !stack.isEmpty() || current != null;
39+
}
40+
41+
private static class TreeNode {
42+
private int val;
43+
private TreeNode left;
44+
private TreeNode right;
45+
}
46+
}
47+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package binary_tree;
2+
3+
import java.util.ArrayList;
4+
import java.util.Deque;
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
8+
/**
9+
* Description:https://leetcode.com/problems/binary-tree-inorder-traversal
10+
* Difficulty: Easy
11+
* Time complexity: O(n)
12+
* Space complexity: O(h)
13+
*/
14+
public class BinaryTreeInorderTraversal {
15+
16+
public List<Integer> inorderTraversalViaIteration(TreeNode root) {
17+
List<Integer> inorder = new ArrayList<>();
18+
Deque<TreeNode> stack = new LinkedList<>();
19+
20+
while (!stack.isEmpty() || root != null) {
21+
if (root != null) {
22+
stack.push(root);
23+
root = root.left;
24+
} else {
25+
root = stack.pop();
26+
inorder.add(root.val);
27+
root = root.right;
28+
}
29+
}
30+
31+
return inorder;
32+
}
33+
34+
public List<Integer> inorderTraversalViaRecursion(TreeNode root) {
35+
List<Integer> inorder = new ArrayList<>();
36+
traverseInorder(root, inorder);
37+
38+
return inorder;
39+
}
40+
41+
private void traverseInorder(TreeNode root, List<Integer> result) {
42+
if (root == null) return;
43+
44+
traverseInorder(root.left, result);
45+
result.add(root.val);
46+
traverseInorder(root.right, result);
47+
}
48+
49+
private static class TreeNode {
50+
int val;
51+
TreeNode left;
52+
TreeNode right;
53+
}
54+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package binary_tree;
2+
3+
import java.util.ArrayList;
4+
import java.util.Deque;
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
8+
/**
9+
* Description:https://leetcode.com/problems/binary-tree-postorder-traversal
10+
* Difficulty: Easy
11+
* Time complexity: O(n)
12+
* Space complexity: O(h)
13+
*/
14+
public class BinaryTreePostorderTraversal {
15+
16+
public List<Integer> postorderTraversalViaTwoStacks(TreeNode root) {
17+
if (root == null) return List.of();
18+
19+
List<Integer> postorder = new ArrayList<>();
20+
Deque<TreeNode> stack = new LinkedList<>();
21+
Deque<TreeNode> out = new LinkedList<>();
22+
23+
stack.push(root);
24+
while (!stack.isEmpty()) {
25+
TreeNode current = stack.poll();
26+
out.push(current);
27+
28+
if (current.left != null) stack.push(current.left);
29+
if (current.right != null) stack.push(current.right);
30+
}
31+
32+
while (!out.isEmpty()) {
33+
postorder.add(out.pop().val);
34+
}
35+
36+
return postorder;
37+
}
38+
39+
public List<Integer> postorderTraversalViaRecursion(TreeNode root) {
40+
List<Integer> postorder = new ArrayList<>();
41+
traversePostorder(root, postorder);
42+
43+
return postorder;
44+
}
45+
46+
private void traversePostorder(TreeNode root, List<Integer> result) {
47+
if (root == null) return;
48+
49+
traversePostorder(root.left, result);
50+
traversePostorder(root.right, result);
51+
result.add(root.val);
52+
}
53+
54+
private static class TreeNode {
55+
int val;
56+
TreeNode left;
57+
TreeNode right;
58+
}
59+
}

0 commit comments

Comments
 (0)