Skip to content

Commit b2521d0

Browse files
authored
Merge pull request #1657 from minji-go/week15
[minji-go] week 15 solutions
2 parents 8abf1b9 + 1acb347 commit b2521d0

File tree

11 files changed

+292
-63
lines changed

11 files changed

+292
-63
lines changed

clone-graph/minji-go.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/clone-graph/">week8-3.clone-graph</a>
3+
* <li>Description: Return a deep copy (clone) of the graph</li>
4+
* <li>Topics: Hash Table, Depth-First Search, Breadth-First Search, Graph</li>
5+
* <li>Time Complexity: O(N+E), Runtime 26ms</li>
6+
* <li>Space Complexity: O(N), Memory 42.77MB</li>
7+
*/
8+
9+
class Solution {
10+
private Map<Node, Node> map = new HashMap<>();
11+
12+
public Node cloneGraph(Node node) {
13+
if(node == null) return null;
14+
15+
if (map.containsKey(node)) {
16+
return map.get(node);
17+
}
18+
19+
Node clone = new Node(node.val);
20+
map.put(node, clone);
21+
22+
for(Node neighbor : node.neighbors) {
23+
clone.neighbors.add(cloneGraph(neighbor));
24+
}
25+
26+
return clone;
27+
}
28+
}
Lines changed: 22 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,31 @@
1-
/*
2-
Problem: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
3-
Description: Given two integer arrays preorder and inorder, construct and return the binary tree.
4-
Concept: Array, Hash Table, Divide and Conquer, Tree, Binary Tree
5-
Time Complexity: O(N²), Runtime 2ms
6-
Space Complexity: O(N), Memory 45.02MB
7-
*/
8-
import java.util.HashMap;
9-
import java.util.Map;
1+
/**
2+
* <a href="https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/">week15-2. construct-binary-tree-from-preorder-and-inorder-traversal</a>
3+
* <li>Description: Given two integer arrays preorder and inorder, construct and return the binary tree</li>
4+
* <li>Topics: Array, Hash Table, Divide and Conquer, Tree, Binary Tree</li>
5+
* <li>Time Complexity: O(N), Runtime 2ms </li>
6+
* <li>Space Complexity: O(N), Memory 44.41MB</li>
7+
*/
108

119
class Solution {
1210
public TreeNode buildTree(int[] preorder, int[] inorder) {
13-
boolean isLeft = true;
14-
Map<Integer, TreeNode> parents = new HashMap<>();
15-
TreeNode rootNode = null, parentNode = null;
11+
Map<Integer, Integer> inorderMap = new HashMap<>();
12+
for (int i = 0; i < inorder.length; i++) {
13+
inorderMap.put(inorder[i], i);
14+
}
15+
return buildTree(preorder, new AtomicInteger(0), inorderMap, 0, inorder.length - 1);
16+
}
1617

17-
for (int pidx=0, iidx=0; pidx<preorder.length; pidx++) {
18-
int pval = preorder[pidx];
19-
int ival = inorder[iidx];
18+
public TreeNode buildTree(int[] preorder, AtomicInteger index, Map<Integer, Integer> inorderMap, int start, int end) {
19+
if (start > end) return null;
2020

21-
if(pidx==0) {
22-
rootNode = parentNode = new TreeNode(pval);
23-
} else if (isLeft) {
24-
parents.put(pval, parentNode);
25-
parentNode = parentNode.left = new TreeNode(pval);
26-
} else {
27-
isLeft = true;
28-
parents.put(pval, parentNode);
29-
parentNode = parentNode.right = new TreeNode(pval);
30-
}
21+
int nodeValue = preorder[index.getAndIncrement()];
22+
TreeNode node = new TreeNode(nodeValue);
3123

32-
if(pval==ival) {
33-
isLeft = false;
34-
TreeNode targetNode = parentNode;
35-
while (iidx<inorder.length-1 && parents.get(parentNode.val)!=null) {
36-
if(parentNode.val == inorder[iidx+1]){
37-
iidx++;
38-
targetNode = parentNode;
39-
}
40-
parentNode = parents.get(parentNode.val);
41-
}
42-
if(iidx<inorder.length-1 && parentNode.val != inorder[iidx+1]){
43-
iidx++;
44-
parentNode = targetNode;
45-
} else iidx = iidx+2;
46-
}
47-
}
24+
int nodeIndex = inorderMap.get(nodeValue);
4825

49-
return rootNode;
26+
node.left = buildTree(preorder, index, inorderMap, start, nodeIndex - 1);
27+
node.right = buildTree(preorder, index, inorderMap, nodeIndex + 1, end);
28+
29+
return node;
5030
}
5131
}
52-
53-
/**
54-
* Definition for a binary tree node.
55-
* public class TreeNode {
56-
* int val;
57-
* TreeNode left;
58-
* TreeNode right;
59-
* TreeNode() {}
60-
* TreeNode(int val) { this.val = val; }
61-
* TreeNode(int val, TreeNode left, TreeNode right) {
62-
* this.val = val;
63-
* this.left = left;
64-
* this.right = right;
65-
* }
66-
* }
67-
*/
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/">find-minimum-in-rotated-sorted-array</a>
3+
* <li>Description: Given the sorted rotated array nums of unique elements, return the minimum element of this array</li>
4+
* <li>Topics: Array, Binary Search</li>
5+
* <li>Time Complexity: O(logN), Runtime 0ms</li>
6+
* <li>Space Complexity: O(1), Memory 41.78MB</li>
7+
*/
8+
9+
class Solution {
10+
public int findMin(int[] nums) {
11+
if (nums[0] <= nums[nums.length - 1]) {
12+
return nums[0];
13+
}
14+
15+
int left = 0, right = nums.length - 1;
16+
while (left < right) {
17+
int mid = (left + right + 1) / 2;
18+
if (nums[left] < nums[mid]) {
19+
left = mid;
20+
} else {
21+
right = mid - 1;
22+
}
23+
}
24+
25+
return nums[left + 1];
26+
}
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/longest-common-subsequence/">week8-4.longest-common-subsequence</a>
3+
* <li>Description: Given two strings text1 and text2, return the length of their longest common subsequence</li>
4+
* <li>Topics: String, Dynamic Programming</li>
5+
* <li>Time Complexity: O(M×N), Runtime 11ms</li>
6+
* <li>Space Complexity: O(M×N), Memory 50.97MB</li>
7+
*/
8+
9+
class Solution {
10+
public int longestCommonSubsequence(String text1, String text2) {
11+
int m = text1.length();
12+
int n = text2.length();
13+
int[][] dp = new int[m + 1][n + 1];
14+
for (int i = 1; i <= m; i++) {
15+
char c1 = text1.charAt(i - 1);
16+
for (int j = 1; j <= n; j++) {
17+
char c2 = text2.charAt(j - 1);
18+
19+
if (c1 == c2) {
20+
dp[i][j] = dp[i - 1][j - 1] + 1;
21+
} else {
22+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
23+
}
24+
}
25+
}
26+
27+
return dp[m][n];
28+
}
29+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/longest-palindromic-substring/">week15-3. longest-palindromic-substring</a>
3+
* <li>Description: Given a string s, return the longest palindromic substring in s</li>
4+
* <li>Topics: Two Pointers, String, Dynamic Programming</li>
5+
* <li>Time Complexity: O(N), Runtime 15ms </li>
6+
* <li>Space Complexity: O(N), Memory 42.13MB</li>
7+
*/
8+
9+
class Solution {
10+
public String longestPalindrome(String s) {
11+
int start = 0;
12+
int maxLength = 1;
13+
14+
for (int i = 0; i < s.length(); i++) {
15+
int len1 = findPalindrome(s, i, i);
16+
int len2 = findPalindrome(s, i, i + 1);
17+
18+
int len = Math.max(len1, len2);
19+
if (len > maxLength) {
20+
maxLength = len;
21+
start = i - (len - 1) / 2;
22+
}
23+
}
24+
25+
return s.substring(start, start + maxLength);
26+
}
27+
28+
public int findPalindrome(String s, int left, int right) {
29+
while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
30+
left--;
31+
right++;
32+
}
33+
34+
return right - left - 1;
35+
}
36+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/longest-repeating-character-replacement/">week8-2.longest-repeating-character-replacement</a>
3+
* <li>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</li>
4+
* <li>Topics: Hash Table, String, Sliding Window</li>
5+
* <li>Time Complexity: O(K), Runtime 7ms </li>
6+
* <li>Space Complexity: O(1), Memory 44.85MB </li>
7+
*/
8+
9+
class Solution {
10+
public int characterReplacement(String s, int k) {
11+
int left = 0;
12+
int maxCount = 0;
13+
int[] count = new int[26];
14+
int maxLength = 0;
15+
16+
for (int right = 0; right < s.length(); right++) {
17+
char c = s.charAt(right);
18+
count[c - 'A']++;
19+
maxCount = Math.max(maxCount, count[c - 'A']);
20+
21+
while (right - left + 1 > maxCount + k) {
22+
count[s.charAt(left) - 'A']--;
23+
left++;
24+
}
25+
maxLength = Math.max(maxLength, right - left + 1);
26+
}
27+
28+
return maxLength;
29+
}
30+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/maximum-depth-of-binary-tree/">week4-2.maximum-depth-of-binary-tree</a>
3+
* <li>Description: Given the root of a binary tree, return its maximum depth.</li>
4+
* <li>Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree</li>
5+
* <li>Time Complexity: O(N), Runtime 1ms</li>
6+
* <li>Space Complexity: O(W), Memory 43.21MB</li>
7+
*/
8+
9+
class Solution {
10+
public int maxDepth(TreeNode root) {
11+
if (root == null) return 0;
12+
13+
Queue<TreeNode> queue = new LinkedList<>();
14+
queue.offer(root);
15+
16+
int depth = 0;
17+
while (!queue.isEmpty()) {
18+
depth++;
19+
int size = queue.size();
20+
for (int i = 0; i < size; i++) {
21+
TreeNode node = queue.poll();
22+
if (node.left != null) queue.offer(node.left);
23+
if (node.right != null) queue.offer(node.right);
24+
}
25+
}
26+
return depth;
27+
}
28+
}

number-of-1-bits/minji-go.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
/**
2-
* <a href="https://leetcode.com/problems/number-of-1-bits/">week03-2.number-of-1-bits</a>
2+
* <a href="https://leetcode.com/problems/number-of-1-bits/">week8-1.number-of-1-bits</a>
33
* <li>Description: returns the number of set bits in its binary representation</li>
44
* <li>Topics: Divide and Conquer, Bit Manipulation </li>
5-
* <li>Time Complexity: O(logN), Runtime 0ms </li>
6-
* <li>Space Complexity: O(1), Memory 41.95MB </li>
5+
* <li>Time Complexity: O(K), Runtime 0ms </li>
6+
* <li>Space Complexity: O(1), Memory 40.57MB </li>
77
*/
8+
89
class Solution {
910
public int hammingWeight(int n) {
1011
int count = 0;
11-
while(n != 0) {
12-
n &= (n-1);
12+
while (n != 0) {
13+
n = n & (n - 1);
1314
count++;
1415
}
1516
return count;

rotate-image/minji-go.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/rotate-image/">week15-4. rotate-image</a>
3+
* <li>Description: Given an n x n 2D matrix representing an image, rotate the image by 90 degrees</li>
4+
* <li>Topics: Array, Math, Matrix</li>
5+
* <li>Time Complexity: O(N²), Runtime 0ms </li>
6+
* <li>Space Complexity: O(1), Memory 42.11MB </li>
7+
*/
8+
9+
class Solution {
10+
public void rotate(int[][] matrix) {
11+
int n = matrix.length;
12+
13+
for (int i = 0; i < n / 2; i++) {
14+
for (int j = i; j < n - 1 - i; j++) {
15+
int temp = matrix[i][j];
16+
matrix[i][j] = matrix[n - 1 - j][i];
17+
matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j];
18+
matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i];
19+
matrix[j][n - 1 - i] = temp;
20+
}
21+
}
22+
}
23+
}
24+

set-matrix-zeroes/minji-go.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/set-matrix-zeroes/">week7-5.set-matrix-zeroes</a>
3+
* <li>Description: Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's</li>
4+
* <li>Topics: Array, Hash Table, Matrix </li>
5+
* <li>Time Complexity: O(MN), Runtime 0ms </li>
6+
* <li>Space Complexity: O(M+N), Memory 45.58MB </li>
7+
*/
8+
class Solution {
9+
public void setZeroes(int[][] matrix) {
10+
int m = matrix.length;
11+
int n = matrix[0].length;
12+
13+
boolean[] rowZero = new boolean[m];
14+
boolean[] colZero = new boolean[n];
15+
16+
for (int i = 0; i < m; i++) {
17+
for (int j = 0; j < n; j++) {
18+
if (matrix[i][j] == 0) {
19+
rowZero[i] = true;
20+
colZero[j] = true;
21+
}
22+
}
23+
}
24+
25+
for (int i = 0; i < m; i++) {
26+
if (rowZero[i]) {
27+
for (int j = 0; j < n; j++) {
28+
matrix[i][j] = 0;
29+
}
30+
}
31+
}
32+
for (int i = 0; i < n; i++) {
33+
if (colZero[i]) {
34+
for (int j = 0; j < m; j++) {
35+
matrix[j][i] = 0;
36+
}
37+
}
38+
}
39+
40+
}
41+
}

0 commit comments

Comments
 (0)