diff --git a/clone-graph/minji-go.java b/clone-graph/minji-go.java
new file mode 100644
index 000000000..cb1fe26b6
--- /dev/null
+++ b/clone-graph/minji-go.java
@@ -0,0 +1,28 @@
+/**
+ * week8-3.clone-graph
+ *
Description: Return a deep copy (clone) of the graph
+ * Topics: Hash Table, Depth-First Search, Breadth-First Search, Graph
+ * Time Complexity: O(N+E), Runtime 26ms
+ * Space Complexity: O(N), Memory 42.77MB
+ */
+
+class Solution {
+ private Map map = new HashMap<>();
+
+ public Node cloneGraph(Node node) {
+ if(node == null) return null;
+
+ if (map.containsKey(node)) {
+ return map.get(node);
+ }
+
+ Node clone = new Node(node.val);
+ map.put(node, clone);
+
+ for(Node neighbor : node.neighbors) {
+ clone.neighbors.add(cloneGraph(neighbor));
+ }
+
+ return clone;
+ }
+}
diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/minji-go.java b/construct-binary-tree-from-preorder-and-inorder-traversal/minji-go.java
index bb5efde92..4d6e05b9d 100644
--- a/construct-binary-tree-from-preorder-and-inorder-traversal/minji-go.java
+++ b/construct-binary-tree-from-preorder-and-inorder-traversal/minji-go.java
@@ -1,67 +1,31 @@
-/*
- Problem: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
- Description: Given two integer arrays preorder and inorder, construct and return the binary tree.
- Concept: Array, Hash Table, Divide and Conquer, Tree, Binary Tree
- Time Complexity: O(N²), Runtime 2ms
- Space Complexity: O(N), Memory 45.02MB
-*/
-import java.util.HashMap;
-import java.util.Map;
+/**
+ * week15-2. construct-binary-tree-from-preorder-and-inorder-traversal
+ * Description: Given two integer arrays preorder and inorder, construct and return the binary tree
+ * Topics: Array, Hash Table, Divide and Conquer, Tree, Binary Tree
+ * Time Complexity: O(N), Runtime 2ms
+ * Space Complexity: O(N), Memory 44.41MB
+ */
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
- boolean isLeft = true;
- Map parents = new HashMap<>();
- TreeNode rootNode = null, parentNode = null;
+ Map inorderMap = new HashMap<>();
+ for (int i = 0; i < inorder.length; i++) {
+ inorderMap.put(inorder[i], i);
+ }
+ return buildTree(preorder, new AtomicInteger(0), inorderMap, 0, inorder.length - 1);
+ }
- for (int pidx=0, iidx=0; pidx inorderMap, int start, int end) {
+ if (start > end) return null;
- if(pidx==0) {
- rootNode = parentNode = new TreeNode(pval);
- } else if (isLeft) {
- parents.put(pval, parentNode);
- parentNode = parentNode.left = new TreeNode(pval);
- } else {
- isLeft = true;
- parents.put(pval, parentNode);
- parentNode = parentNode.right = new TreeNode(pval);
- }
+ int nodeValue = preorder[index.getAndIncrement()];
+ TreeNode node = new TreeNode(nodeValue);
- if(pval==ival) {
- isLeft = false;
- TreeNode targetNode = parentNode;
- while (iidxfind-minimum-in-rotated-sorted-array
+ * Description: Given the sorted rotated array nums of unique elements, return the minimum element of this array
+ * Topics: Array, Binary Search
+ * Time Complexity: O(logN), Runtime 0ms
+ * Space Complexity: O(1), Memory 41.78MB
+ */
+
+class Solution {
+ public int findMin(int[] nums) {
+ if (nums[0] <= nums[nums.length - 1]) {
+ return nums[0];
+ }
+
+ int left = 0, right = nums.length - 1;
+ while (left < right) {
+ int mid = (left + right + 1) / 2;
+ if (nums[left] < nums[mid]) {
+ left = mid;
+ } else {
+ right = mid - 1;
+ }
+ }
+
+ return nums[left + 1];
+ }
+}
diff --git a/longest-common-subsequence/minji-go.java b/longest-common-subsequence/minji-go.java
new file mode 100644
index 000000000..529fb65a0
--- /dev/null
+++ b/longest-common-subsequence/minji-go.java
@@ -0,0 +1,29 @@
+/**
+ * week8-4.longest-common-subsequence
+ * Description: Given two strings text1 and text2, return the length of their longest common subsequence
+ * Topics: String, Dynamic Programming
+ * Time Complexity: O(M×N), Runtime 11ms
+ * Space Complexity: O(M×N), Memory 50.97MB
+ */
+
+class Solution {
+ public int longestCommonSubsequence(String text1, String text2) {
+ int m = text1.length();
+ int n = text2.length();
+ int[][] dp = new int[m + 1][n + 1];
+ for (int i = 1; i <= m; i++) {
+ char c1 = text1.charAt(i - 1);
+ for (int j = 1; j <= n; j++) {
+ char c2 = text2.charAt(j - 1);
+
+ if (c1 == c2) {
+ dp[i][j] = dp[i - 1][j - 1] + 1;
+ } else {
+ dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
+ }
+ }
+ }
+
+ return dp[m][n];
+ }
+}
diff --git a/longest-palindromic-substring/minji-go.java b/longest-palindromic-substring/minji-go.java
new file mode 100644
index 000000000..923aeb2ca
--- /dev/null
+++ b/longest-palindromic-substring/minji-go.java
@@ -0,0 +1,36 @@
+/**
+ * week15-3. longest-palindromic-substring
+ * Description: Given a string s, return the longest palindromic substring in s
+ * Topics: Two Pointers, String, Dynamic Programming
+ * Time Complexity: O(N), Runtime 15ms
+ * Space Complexity: O(N), Memory 42.13MB
+ */
+
+class Solution {
+ public String longestPalindrome(String s) {
+ int start = 0;
+ int maxLength = 1;
+
+ for (int i = 0; i < s.length(); i++) {
+ int len1 = findPalindrome(s, i, i);
+ int len2 = findPalindrome(s, i, i + 1);
+
+ int len = Math.max(len1, len2);
+ if (len > maxLength) {
+ maxLength = len;
+ start = i - (len - 1) / 2;
+ }
+ }
+
+ return s.substring(start, start + maxLength);
+ }
+
+ public int findPalindrome(String s, int left, int right) {
+ while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
+ left--;
+ right++;
+ }
+
+ return right - left - 1;
+ }
+}
diff --git a/longest-repeating-character-replacement/minji-go.java b/longest-repeating-character-replacement/minji-go.java
new file mode 100644
index 000000000..e328b7a73
--- /dev/null
+++ b/longest-repeating-character-replacement/minji-go.java
@@ -0,0 +1,30 @@
+/**
+ * week8-2.longest-repeating-character-replacement
+ * Description: Return the length of the longest substring containing the same letter you can get after changing the character to any other uppercase English character
+ * Topics: Hash Table, String, Sliding Window
+ * Time Complexity: O(K), Runtime 7ms
+ * Space Complexity: O(1), Memory 44.85MB
+ */
+
+class Solution {
+ public int characterReplacement(String s, int k) {
+ int left = 0;
+ int maxCount = 0;
+ int[] count = new int[26];
+ int maxLength = 0;
+
+ for (int right = 0; right < s.length(); right++) {
+ char c = s.charAt(right);
+ count[c - 'A']++;
+ maxCount = Math.max(maxCount, count[c - 'A']);
+
+ while (right - left + 1 > maxCount + k) {
+ count[s.charAt(left) - 'A']--;
+ left++;
+ }
+ maxLength = Math.max(maxLength, right - left + 1);
+ }
+
+ return maxLength;
+ }
+}
diff --git a/maximum-depth-of-binary-tree/minji-go.java b/maximum-depth-of-binary-tree/minji-go.java
new file mode 100644
index 000000000..e37fe7de7
--- /dev/null
+++ b/maximum-depth-of-binary-tree/minji-go.java
@@ -0,0 +1,28 @@
+/**
+ * week4-2.maximum-depth-of-binary-tree
+ * Description: Given the root of a binary tree, return its maximum depth.
+ * Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
+ * Time Complexity: O(N), Runtime 1ms
+ * Space Complexity: O(W), Memory 43.21MB
+ */
+
+class Solution {
+ public int maxDepth(TreeNode root) {
+ if (root == null) return 0;
+
+ Queue queue = new LinkedList<>();
+ queue.offer(root);
+
+ int depth = 0;
+ while (!queue.isEmpty()) {
+ depth++;
+ int size = queue.size();
+ for (int i = 0; i < size; i++) {
+ TreeNode node = queue.poll();
+ if (node.left != null) queue.offer(node.left);
+ if (node.right != null) queue.offer(node.right);
+ }
+ }
+ return depth;
+ }
+}
diff --git a/number-of-1-bits/minji-go.java b/number-of-1-bits/minji-go.java
index 0adb4a526..76275049c 100644
--- a/number-of-1-bits/minji-go.java
+++ b/number-of-1-bits/minji-go.java
@@ -1,15 +1,16 @@
/**
- * week03-2.number-of-1-bits
+ * week8-1.number-of-1-bits
* Description: returns the number of set bits in its binary representation
* Topics: Divide and Conquer, Bit Manipulation
- * Time Complexity: O(logN), Runtime 0ms
- * Space Complexity: O(1), Memory 41.95MB
+ * Time Complexity: O(K), Runtime 0ms
+ * Space Complexity: O(1), Memory 40.57MB
*/
+
class Solution {
public int hammingWeight(int n) {
int count = 0;
- while(n != 0) {
- n &= (n-1);
+ while (n != 0) {
+ n = n & (n - 1);
count++;
}
return count;
diff --git a/rotate-image/minji-go.java b/rotate-image/minji-go.java
new file mode 100644
index 000000000..71e254247
--- /dev/null
+++ b/rotate-image/minji-go.java
@@ -0,0 +1,24 @@
+/**
+ * week15-4. rotate-image
+ * Description: Given an n x n 2D matrix representing an image, rotate the image by 90 degrees
+ * Topics: Array, Math, Matrix
+ * Time Complexity: O(N²), Runtime 0ms
+ * Space Complexity: O(1), Memory 42.11MB
+ */
+
+class Solution {
+ public void rotate(int[][] matrix) {
+ int n = matrix.length;
+
+ for (int i = 0; i < n / 2; i++) {
+ for (int j = i; j < n - 1 - i; j++) {
+ int temp = matrix[i][j];
+ matrix[i][j] = matrix[n - 1 - j][i];
+ matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j];
+ matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i];
+ matrix[j][n - 1 - i] = temp;
+ }
+ }
+ }
+}
+
diff --git a/set-matrix-zeroes/minji-go.java b/set-matrix-zeroes/minji-go.java
new file mode 100644
index 000000000..a043dab54
--- /dev/null
+++ b/set-matrix-zeroes/minji-go.java
@@ -0,0 +1,41 @@
+/**
+ * week7-5.set-matrix-zeroes
+ * Description: Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's
+ * Topics: Array, Hash Table, Matrix
+ * Time Complexity: O(MN), Runtime 0ms
+ * Space Complexity: O(M+N), Memory 45.58MB
+ */
+class Solution {
+ public void setZeroes(int[][] matrix) {
+ int m = matrix.length;
+ int n = matrix[0].length;
+
+ boolean[] rowZero = new boolean[m];
+ boolean[] colZero = new boolean[n];
+
+ for (int i = 0; i < m; i++) {
+ for (int j = 0; j < n; j++) {
+ if (matrix[i][j] == 0) {
+ rowZero[i] = true;
+ colZero[j] = true;
+ }
+ }
+ }
+
+ for (int i = 0; i < m; i++) {
+ if (rowZero[i]) {
+ for (int j = 0; j < n; j++) {
+ matrix[i][j] = 0;
+ }
+ }
+ }
+ for (int i = 0; i < n; i++) {
+ if (colZero[i]) {
+ for (int j = 0; j < m; j++) {
+ matrix[j][i] = 0;
+ }
+ }
+ }
+
+ }
+}
diff --git a/subtree-of-another-tree/minji-go.java b/subtree-of-another-tree/minji-go.java
new file mode 100644
index 000000000..59443e742
--- /dev/null
+++ b/subtree-of-another-tree/minji-go.java
@@ -0,0 +1,21 @@
+/**
+ * week15-1. subtree-of-another-tree
+ * Description: Given the roots of two binary trees root and subRoot, return true if there is a subtree of root with the same structure and node values of subRoot
+ * Topics: Tree, Depth-First Search, String Matching, Binary Tree, Hash Function
+ * Time Complexity: O(N*M), Runtime 2ms
+ * Space Complexity: O(H), Memory 43.98MB
+ */
+class Solution {
+ public boolean isSubtree(TreeNode root, TreeNode subRoot) {
+ if (root == null) return false;
+ if (isSameTree(root, subRoot)) return true;
+ return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
+ }
+
+ public boolean isSameTree(TreeNode root, TreeNode subRoot) {
+ if (root == null && subRoot == null) return true;
+ if (root == null || subRoot == null) return false;
+ if (root.val != subRoot.val) return false;
+ return isSameTree(root.left, subRoot.left) && isSameTree(root.right, subRoot.right);
+ }
+}