Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions alien-dictionary/yhkee0404.kt
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()}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'z'.code 이러면 아스키 코드 넘버가 나올까요?

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 ""*/
}
}
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;
}
}
44 changes: 44 additions & 0 deletions longest-palindromic-substring/yhkee0404.scala
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
}
}
14 changes: 14 additions & 0 deletions rotate-image/yhkee0404.go
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--
}
}
}
56 changes: 56 additions & 0 deletions subtree-of-another-tree/yhkee0404.rs
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(),
}
}
}