Skip to content

Commit cacb2e7

Browse files
committed
Update
1 parent 3ca49ed commit cacb2e7

File tree

5 files changed

+306
-9
lines changed

5 files changed

+306
-9
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package binary_tree;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal
8+
* Difficulty: Medium
9+
* Time complexity: O(n)
10+
* Space complexity: O(n)
11+
*/
12+
public class ConstructBinaryTreeFromPreorderAndInorderTraversal {
13+
14+
private Map<Integer, Integer> inorderValueToIndexMap;
15+
private int rootIndexInPreorderArray;
16+
17+
public TreeNode buildTree(int[] preorder, int[] inorder) {
18+
rootIndexInPreorderArray = 0;
19+
inorderValueToIndexMap = new HashMap<>();
20+
for (int i = 0; i < inorder.length; i++) {
21+
inorderValueToIndexMap.put(inorder[i], i);
22+
}
23+
24+
return toTree(preorder, 0, preorder.length - 1);
25+
}
26+
27+
private TreeNode toTree(int[] preorder, int left, int right) {
28+
if (left > right) return null;
29+
30+
TreeNode root = new TreeNode();
31+
root.val = preorder[rootIndexInPreorderArray++];
32+
int rootIndexInInorderArray = inorderValueToIndexMap.get(root.val);
33+
34+
root.left = toTree(preorder, left, rootIndexInInorderArray - 1);
35+
root.right = toTree(preorder, rootIndexInInorderArray + 1, right);
36+
37+
return root;
38+
}
39+
40+
private static class TreeNode {
41+
int val;
42+
TreeNode left;
43+
TreeNode right;
44+
}
45+
}

src/heap/KClosestPointsToOrigin.java

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,6 @@
1010
*/
1111
public class KClosestPointsToOrigin {
1212

13-
/**
14-
* Time complexity: O(nlog n)
15-
* Space complexity: O(log n)
16-
*/
17-
public int[][] kClosestViaSorting(int[][] points, int k) {
18-
Arrays.sort(points, (p1, p2) -> Integer.compare(distance(p1), distance(p2)));
19-
return Arrays.copyOfRange(points, 0, k);
20-
}
21-
2213
/**
2314
* Time complexity: O(nlog k)
2415
* Space complexity: O(k)
@@ -41,6 +32,56 @@ public int[][] kClosestViaMaxHeap(int[][] points, int k) {
4132
return result;
4233
}
4334

35+
/**
36+
* Time complexity: O(n)
37+
* Space complexity: O(n)
38+
*/
39+
public int[][] kClosestViaQuickSelect(int[][] points, int k) {
40+
return quickSelect(points, 0, points.length - 1, k);
41+
}
42+
43+
private int[][] quickSelect(int[][] points, int left, int right, int k) {
44+
int partition = lomutoPartition(points, left, right);
45+
if (partition == k - 1) return Arrays.copyOfRange(points, 0, k);
46+
47+
if (partition > k - 1) {
48+
return quickSelect(points, left, partition - 1, k);
49+
} else {
50+
return quickSelect(points, partition + 1, right, k);
51+
}
52+
}
53+
54+
private int lomutoPartition(int[][] points, int left, int right) {
55+
int pivotDistance = distance(points[right]);
56+
int pivotPointer = left;
57+
58+
for (int i = left; i < right; i++) {
59+
if (distance(points[i]) < pivotDistance) {
60+
swap(points, i, pivotPointer);
61+
pivotPointer++;
62+
}
63+
}
64+
65+
swap(points, right, pivotPointer);
66+
67+
return pivotPointer;
68+
}
69+
70+
private void swap(int[][] points, int i, int j) {
71+
int[] tmp = points[i];
72+
points[i] = points[j];
73+
points[j] = tmp;
74+
}
75+
76+
/**
77+
* Time complexity: O(nlog n)
78+
* Space complexity: O(log n)
79+
*/
80+
public int[][] kClosestViaSorting(int[][] points, int k) {
81+
Arrays.sort(points, (p1, p2) -> Integer.compare(distance(p1), distance(p2)));
82+
return Arrays.copyOfRange(points, 0, k);
83+
}
84+
4485
private int distance(int[] point) {
4586
return point[0] * point[0] + point[1] * point[1];
4687
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package heap;
2+
3+
import java.util.PriorityQueue;
4+
import java.util.Queue;
5+
6+
/**
7+
* Description: https://leetcode.com/proiblems/kth-largest-element-in-an-array
8+
* Difficulty: Medium
9+
*/
10+
public class KthLargestElementInArray {
11+
12+
/**
13+
* Time complexity: O(n)
14+
* Space complexity: O(n)
15+
*/
16+
public int findKthLargestViaQuickSelect(int[] nums, int k) {
17+
return quickSelect(nums, 0, nums.length - 1, k);
18+
}
19+
20+
private int quickSelect(int[] nums, int left, int right, int k) {
21+
int pivotPointer = lomutoPartition(nums, left, right);
22+
23+
if (pivotPointer == k - 1) return nums[pivotPointer];
24+
25+
if (pivotPointer > k - 1) {
26+
return quickSelect(nums, left, pivotPointer - 1, k);
27+
} else {
28+
return quickSelect(nums, pivotPointer + 1, right, k);
29+
}
30+
}
31+
32+
private int lomutoPartition(int[] nums, int left, int right) {
33+
int pivot = nums[right];
34+
int pivotPointer = left;
35+
36+
for (int i = left; i < right; i++) {
37+
if (nums[i] > pivot) {
38+
swap(nums, i, pivotPointer);
39+
pivotPointer++;
40+
}
41+
}
42+
43+
swap(nums, pivotPointer, right);
44+
45+
return pivotPointer;
46+
}
47+
48+
private void swap(int[] nums, int l, int r) {
49+
int tmp = nums[l];
50+
nums[l] = nums[r];
51+
nums[r] = tmp;
52+
}
53+
54+
/**
55+
* Time complexity: O(nlog k)
56+
* Space complexity: O(k)
57+
*/
58+
public int findKthLargestViaMinHeap(int[] nums, int k) {
59+
Queue<Integer> minHeap = new PriorityQueue<>();
60+
for (int num : nums) {
61+
minHeap.offer(num);
62+
if (minHeap.size() > k) minHeap.poll();
63+
}
64+
65+
return minHeap.peek();
66+
}
67+
}

src/linked_list/SwapNodesInPairs.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package linked_list;
2+
3+
4+
/**
5+
* Description: https://leetcode.com/problems/swap-nodes-in-pairs
6+
* Difficulty: Medium
7+
* Time complexity: O(n)
8+
* Space complexity: O(1)
9+
*/
10+
public class SwapNodesInPairs {
11+
12+
public ListNode swapPairs(ListNode head) {
13+
if (head == null || head.next == null) return head;
14+
15+
ListNode dummy = new ListNode();
16+
ListNode current = head;
17+
ListNode prev = dummy;
18+
19+
while (current != null && current.next != null) {
20+
prev.next = current.next;
21+
current.next = current.next.next;
22+
prev.next.next = current;
23+
24+
prev = current;
25+
current = current.next;
26+
}
27+
28+
return dummy.next;
29+
}
30+
31+
private static class ListNode {
32+
ListNode next;
33+
}
34+
}

src/matrix/SetMatrixZeroes.java

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package matrix;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/set-matrix-zeroes
8+
* Difficulty: Medium
9+
*/
10+
public class SetMatrixZeroes {
11+
12+
/**
13+
* Time complexity: O(m * n)
14+
* Space complexity: O(1)
15+
*/
16+
public void setZeroesWithConstantSpace(int[][] matrix) {
17+
// Use separate flags for the first col and row
18+
boolean isFirstColZeroed = isFirstColZeroed(matrix);
19+
boolean isFirstRowZeroed = isFirstRowZeroed(matrix);
20+
21+
// Use the first col and row cells as flags
22+
markZeroedRowsAndCols(matrix);
23+
24+
// Set zeroes, but not in the first row and col
25+
setZeroesInInnerMatrix(matrix);
26+
27+
if (isFirstColZeroed) setZeroesInFirstCol(matrix);
28+
if (isFirstRowZeroed) setZeroesInFirstRow(matrix);
29+
}
30+
31+
private static boolean isFirstColZeroed(int[][] matrix) {
32+
for (int i = 0; i < matrix.length; i++) {
33+
if (matrix[i][0] == 0) {
34+
return true;
35+
}
36+
}
37+
38+
return false;
39+
}
40+
41+
private static boolean isFirstRowZeroed(int[][] matrix) {
42+
for (int j = 0; j < matrix[0].length; j++) {
43+
if (matrix[0][j] == 0) {
44+
return true;
45+
}
46+
}
47+
48+
return false;
49+
}
50+
51+
private static void markZeroedRowsAndCols(int[][] matrix) {
52+
for (int i = 1; i < matrix.length; i++) {
53+
for (int j = 1; j < matrix[0].length; j++) {
54+
if (matrix[i][j] == 0) {
55+
matrix[i][0] = 0;
56+
matrix[0][j] = 0;
57+
}
58+
}
59+
}
60+
}
61+
62+
private static void setZeroesInInnerMatrix(int[][] matrix) {
63+
for (int i = 1; i < matrix.length; i++) {
64+
for (int j = 1; j < matrix[0].length; j++) {
65+
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
66+
matrix[i][j] = 0;
67+
}
68+
}
69+
}
70+
}
71+
72+
private static void setZeroesInFirstCol(int[][] matrix) {
73+
for (int i = 0; i < matrix.length; i++) {
74+
matrix[i][0] = 0;
75+
}
76+
}
77+
78+
79+
private static void setZeroesInFirstRow(int[][] matrix) {
80+
for (int j = 0; j < matrix[0].length; j++) {
81+
matrix[0][j] = 0;
82+
}
83+
}
84+
85+
/**
86+
* Time complexity: O(m * n)
87+
* Space complexity: O(m + n)
88+
*/
89+
public void setZeroesViaTwoSets(int[][] matrix) {
90+
Set<Integer> zeroedCols = new HashSet<>();
91+
Set<Integer> zeroedRows = new HashSet<>();
92+
93+
for (int i = 0; i < matrix.length; i++) {
94+
for (int j = 0; j < matrix[0].length; j++) {
95+
if (matrix[i][j] == 0) {
96+
zeroedCols.add(i);
97+
zeroedRows.add(j);
98+
}
99+
}
100+
}
101+
102+
for (int i = 0; i < matrix.length; i++) {
103+
for (int j = 0; j < matrix[0].length; j++) {
104+
if (zeroedCols.contains(i) || zeroedRows.contains(j)) {
105+
matrix[i][j] = 0;
106+
}
107+
}
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)