-
-
Notifications
You must be signed in to change notification settings - Fork 245
[minji-go] week 15 solutions #1657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b69aa8a
0307571
174fec9
7067abc
8476fc0
700dca6
649490e
cd799c5
94a952d
3c4e032
12e25a1
1acb347
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* <a href="https://leetcode.com/problems/clone-graph/">week8-3.clone-graph</a> | ||
* <li>Description: Return a deep copy (clone) of the graph</li> | ||
* <li>Topics: Hash Table, Depth-First Search, Breadth-First Search, Graph</li> | ||
* <li>Time Complexity: O(N+E), Runtime 26ms</li> | ||
* <li>Space Complexity: O(N), Memory 42.77MB</li> | ||
*/ | ||
|
||
class Solution { | ||
private Map<Node, Node> 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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
/** | ||
* <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> | ||
* <li>Description: Given two integer arrays preorder and inorder, construct and return the binary tree</li> | ||
* <li>Topics: Array, Hash Table, Divide and Conquer, Tree, Binary Tree</li> | ||
* <li>Time Complexity: O(N), Runtime 2ms </li> | ||
* <li>Space Complexity: O(N), Memory 44.41MB</li> | ||
*/ | ||
|
||
class Solution { | ||
public TreeNode buildTree(int[] preorder, int[] inorder) { | ||
boolean isLeft = true; | ||
Map<Integer, TreeNode> parents = new HashMap<>(); | ||
TreeNode rootNode = null, parentNode = null; | ||
Map<Integer, Integer> 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<preorder.length; pidx++) { | ||
int pval = preorder[pidx]; | ||
int ival = inorder[iidx]; | ||
public TreeNode buildTree(int[] preorder, AtomicInteger index, Map<Integer, Integer> 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 (iidx<inorder.length-1 && parents.get(parentNode.val)!=null) { | ||
if(parentNode.val == inorder[iidx+1]){ | ||
iidx++; | ||
targetNode = parentNode; | ||
} | ||
parentNode = parents.get(parentNode.val); | ||
} | ||
if(iidx<inorder.length-1 && parentNode.val != inorder[iidx+1]){ | ||
iidx++; | ||
parentNode = targetNode; | ||
} else iidx = iidx+2; | ||
} | ||
} | ||
int nodeIndex = inorderMap.get(nodeValue); | ||
|
||
return rootNode; | ||
node.left = buildTree(preorder, index, inorderMap, start, nodeIndex - 1); | ||
node.right = buildTree(preorder, index, inorderMap, nodeIndex + 1, end); | ||
|
||
return node; | ||
} | ||
} | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* <a href="https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/">find-minimum-in-rotated-sorted-array</a> | ||
* <li>Description: Given the sorted rotated array nums of unique elements, return the minimum element of this array</li> | ||
* <li>Topics: Array, Binary Search</li> | ||
* <li>Time Complexity: O(logN), Runtime 0ms</li> | ||
* <li>Space Complexity: O(1), Memory 41.78MB</li> | ||
*/ | ||
|
||
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]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* <a href="https://leetcode.com/problems/longest-common-subsequence/">week8-4.longest-common-subsequence</a> | ||
* <li>Description: Given two strings text1 and text2, return the length of their longest common subsequence</li> | ||
* <li>Topics: String, Dynamic Programming</li> | ||
* <li>Time Complexity: O(M×N), Runtime 11ms</li> | ||
* <li>Space Complexity: O(M×N), Memory 50.97MB</li> | ||
*/ | ||
|
||
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]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* <a href="https://leetcode.com/problems/longest-palindromic-substring/">week15-3. longest-palindromic-substring</a> | ||
* <li>Description: Given a string s, return the longest palindromic substring in s</li> | ||
* <li>Topics: Two Pointers, String, Dynamic Programming</li> | ||
* <li>Time Complexity: O(N), Runtime 15ms </li> | ||
* <li>Space Complexity: O(N), Memory 42.13MB</li> | ||
*/ | ||
|
||
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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* <a href="https://leetcode.com/problems/longest-repeating-character-replacement/">week8-2.longest-repeating-character-replacement</a> | ||
* <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> | ||
* <li>Topics: Hash Table, String, Sliding Window</li> | ||
* <li>Time Complexity: O(K), Runtime 7ms </li> | ||
* <li>Space Complexity: O(1), Memory 44.85MB </li> | ||
*/ | ||
|
||
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']++; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Java는 'A' - '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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* <a href="https://leetcode.com/problems/maximum-depth-of-binary-tree/">week4-2.maximum-depth-of-binary-tree</a> | ||
* <li>Description: Given the root of a binary tree, return its maximum depth.</li> | ||
* <li>Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree</li> | ||
* <li>Time Complexity: O(N), Runtime 1ms</li> | ||
* <li>Space Complexity: O(W), Memory 43.21MB</li> | ||
*/ | ||
|
||
class Solution { | ||
public int maxDepth(TreeNode root) { | ||
if (root == null) return 0; | ||
|
||
Queue<TreeNode> 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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* <a href="https://leetcode.com/problems/rotate-image/">week15-4. rotate-image</a> | ||
* <li>Description: Given an n x n 2D matrix representing an image, rotate the image by 90 degrees</li> | ||
* <li>Topics: Array, Math, Matrix</li> | ||
* <li>Time Complexity: O(N²), Runtime 0ms </li> | ||
* <li>Space Complexity: O(1), Memory 42.11MB </li> | ||
*/ | ||
|
||
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; | ||
} | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* <a href="https://leetcode.com/problems/set-matrix-zeroes/">week7-5.set-matrix-zeroes</a> | ||
* <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> | ||
* <li>Topics: Array, Hash Table, Matrix </li> | ||
* <li>Time Complexity: O(MN), Runtime 0ms </li> | ||
* <li>Space Complexity: O(M+N), Memory 45.58MB </li> | ||
*/ | ||
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; | ||
} | ||
} | ||
} | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* <a href="https://leetcode.com/problems/subtree-of-another-tree/">week15-1. subtree-of-another-tree</a> | ||
* <li>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 </li> | ||
* <li>Topics: Tree, Depth-First Search, String Matching, Binary Tree, Hash Function</li> | ||
* <li>Time Complexity: O(N*M), Runtime 2ms </li> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. serialization 방식을 사용하면 시간 복잡도를 O(M + N)으로 줄일 수 있습니다. 한번 시도해 보시는 걸 추천드립니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 시간 복잡도 줄일 생각을 못했네요 :) 감사합니다! |
||
* <li>Space Complexity: O(H), Memory 43.98MB </li> | ||
*/ | ||
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); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Java에서 int 나눗셈은 소수점 이하를 그냥 잘라버리는 거였군요. JavaScript의 Math.floor()랑은 다른 개념이라 흥미롭네요. 배워갑니다!