-
-
Notifications
You must be signed in to change notification settings - Fork 250
[yhkee0404] WEEK 15 solutions #1965
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
82cfccf
subtree of another tree solution
yhkee0404 e4afccc
construct binary tree from preorder and inorder traversal solution
yhkee0404 bd3c898
longest palindromic substring solution
yhkee0404 fe439ec
rotate image solution
yhkee0404 38e4359
alien dictionary solution
yhkee0404 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| class Solution { | ||
| /** | ||
| * @param words: a list of words | ||
| * @return: a string which is correct order | ||
| */ | ||
| fun alienOrder(words: Array<String>): String { | ||
| // Write your code here | ||
| val adj = List<MutableList<Int>>('z'.code - 'a'.code + 1) {mutableListOf()} | ||
| for (i in 1 until words.size) { | ||
| var j = 0 | ||
| while (j != words[i - 1].length && j != words[i].length && words[i - 1][j] == words[i][j]) { | ||
| j++ | ||
| } | ||
| if (j == words[i - 1].length) { | ||
| continue | ||
| } | ||
| if (j == words[i].length) { | ||
| return "" | ||
| } | ||
| adj[words[i - 1][j].code - 'a'.code].add(words[i][j].code - 'a'.code) | ||
| } | ||
| val visited = MutableList(adj.size) {false} | ||
| words.forEach { | ||
| it.map { it.code - 'a'.code } | ||
| .forEach { visited[it] = true } | ||
| } | ||
| val inDegrees = MutableList(adj.size) {0} // Kahn T(V, E) = S(V, E) = O(V + E) | ||
| adj.forEach { | ||
| it.forEach { inDegrees[it]++ } | ||
| } | ||
| val ans = mutableListOf<Int>() | ||
| val stack = mutableListOf<Int>() | ||
| (0 until adj.size).filter { visited[it] && inDegrees[it] == 0 } | ||
| .forEach { | ||
| stack.add(it) | ||
| ans.add(it) | ||
| } | ||
| while (! stack.isEmpty()) { | ||
| val u = stack.removeLast() | ||
| adj[u].forEach { | ||
| if (--inDegrees[it] == 0) { | ||
| stack.add(it) | ||
| ans.add(it) | ||
| } | ||
| } | ||
| } | ||
| if ((0 until adj.size).any { visited[it] && inDegrees[it] != 0 }) { | ||
| return "" | ||
| } | ||
| return ans.map { (it + 'a'.code).toChar() } | ||
| .joinToString("") | ||
| /*val decoder = MutableList(adj.size) {0} | ||
| (0 until ans.size).forEach { decoder[ans[it]] = it } | ||
| val decoded = words.map { | ||
| it.map { (decoder[it.code - 'a'.code] + 'a'.code).toChar() } | ||
| .joinToString("") | ||
| } | ||
| val sortedWords = decoded.sorted() | ||
| return if (decoded == sortedWords) ans.map { (it + 'a'.code).toChar() } | ||
| .joinToString("") | ||
| else ""*/ | ||
| } | ||
| } | ||
31 changes: 31 additions & 0 deletions
31
construct-binary-tree-from-preorder-and-inorder-traversal/yhkee0404.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * class TreeNode { | ||
| * int val; | ||
| * TreeNode? left; | ||
| * TreeNode? right; | ||
| * TreeNode([this.val = 0, this.left, this.right]); | ||
| * } | ||
| */ | ||
| class Solution { | ||
| TreeNode? buildTree(List<int> preorder, List<int> inorder) { | ||
| int i = 1, j = 0; | ||
| final root = TreeNode(preorder[0]); | ||
| final stack = [root]; // T(n) = S(n) = O(n) | ||
| while (i != preorder.length) { | ||
| var u = stack.last; | ||
| if (u.val != inorder[j]) { | ||
| u.left = TreeNode(preorder[i++]); | ||
| stack.add(u.left!); | ||
| continue; | ||
| } | ||
| while (! stack.isEmpty && stack.last.val == inorder[j]) { | ||
| u = stack.removeLast(); | ||
| j++; | ||
| } | ||
| u.right = TreeNode(preorder[i++]); | ||
| stack.add(u.right!); | ||
| } | ||
| return root; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import scala.collection.mutable.ArrayBuffer | ||
|
|
||
| object Solution { | ||
| def longestPalindrome(s: String): String = { | ||
| val list = ArrayBuffer[Char]() | ||
| for (c <- s) { | ||
| list += c | ||
| list += '\u0000' | ||
| } | ||
| val dp = Array.fill(list.size - 1)(0) | ||
| var lastR = -1 | ||
| var lastMid = -1 | ||
| var maxD = 0 | ||
| var ansL = 0 | ||
| var ansR = 0 | ||
| var i = 1 | ||
| for (i <- 0 until dp.size) { // Manacher T(n) = S(n) = O(n) | ||
| val diff = lastR - i | ||
| var d = if (diff <= 0) 0 else diff min dp((lastMid << 1) - i) | ||
| var l = i - d | ||
| var r = i + d | ||
| while (l != 0 && r != list.size - 1 && list(l - 1) == list(r + 1)) { | ||
| d += 1 | ||
| l -= 1 | ||
| r += 1 | ||
| } | ||
| if (maxD < d) { | ||
| maxD = d | ||
| ansL = l | ||
| ansR = r | ||
| } | ||
| dp(i) = d | ||
| if (lastR < r) { | ||
| lastR = r | ||
| lastMid = i | ||
| } | ||
| } | ||
| val sb = StringBuilder() | ||
| for (i <- ansL to ansR if list(i) != '\u0000') { | ||
| sb.append(list(i)) | ||
| } | ||
| sb.toString | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| func rotate(matrix [][]int) { | ||
| for i := range len(matrix) { | ||
| for j := i + 1; j != len(matrix[i]); j++ { // T(n) = O(n) | ||
| matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] | ||
| } | ||
| } | ||
| for _, row := range matrix { | ||
| for i, j := 0, len(row) - 1; i < j; { | ||
| row[i], row[j] = row[j], row[i] | ||
| i++ | ||
| j-- | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // Definition for a binary tree node. | ||
| // #[derive(Debug, PartialEq, Eq)] | ||
| // pub struct TreeNode { | ||
| // pub val: i32, | ||
| // pub left: Option<Rc<RefCell<TreeNode>>>, | ||
| // pub right: Option<Rc<RefCell<TreeNode>>>, | ||
| // } | ||
| // | ||
| // impl TreeNode { | ||
| // #[inline] | ||
| // pub fn new(val: i32) -> Self { | ||
| // TreeNode { | ||
| // val, | ||
| // left: None, | ||
| // right: None | ||
| // } | ||
| // } | ||
| // } | ||
| use std::rc::Rc; | ||
| use std::cell::RefCell; | ||
| impl Solution { | ||
| pub fn is_subtree(root: Option<Rc<RefCell<TreeNode>>>, sub_root: Option<Rc<RefCell<TreeNode>>>) -> bool { | ||
| match &root { | ||
| Some(u) => { | ||
| let u = u.borrow(); | ||
| match &sub_root { | ||
| Some(v) => { | ||
| let v = v.borrow(); | ||
| Self::solve(root.clone(), sub_root.clone()) | ||
| || Self::is_subtree(u.left.clone(), sub_root.clone()) | ||
| || Self::is_subtree(u.right.clone(), sub_root.clone()) | ||
| }, | ||
| None => false, | ||
| } | ||
| }, | ||
| None => sub_root.is_none(), | ||
| } | ||
| } | ||
| fn solve(root: Option<Rc<RefCell<TreeNode>>>, sub_root: Option<Rc<RefCell<TreeNode>>>) -> bool { | ||
| match root { | ||
| Some(u) => { | ||
| let u = u.borrow(); | ||
| match &sub_root { | ||
| Some(v) => { | ||
| let v = v.borrow(); | ||
| u.val == v.val | ||
| && Self::solve(u.left.clone(), v.left.clone()) | ||
| && Self::solve(u.right.clone(), v.right.clone()) | ||
| }, | ||
| None => false, | ||
| } | ||
| }, | ||
| None => sub_root.is_none(), | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
'z'.code 이러면 아스키 코드 넘버가 나올까요?