Skip to content

Commit e16f4cf

Browse files
authored
Merge pull request #1965 from yhkee0404/main
[yhkee0404] WEEK 15 solutions
2 parents 2f5427e + 38e4359 commit e16f4cf

File tree

5 files changed

+208
-0
lines changed

5 files changed

+208
-0
lines changed

alien-dictionary/yhkee0404.kt

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
class Solution {
2+
/**
3+
* @param words: a list of words
4+
* @return: a string which is correct order
5+
*/
6+
fun alienOrder(words: Array<String>): String {
7+
// Write your code here
8+
val adj = List<MutableList<Int>>('z'.code - 'a'.code + 1) {mutableListOf()}
9+
for (i in 1 until words.size) {
10+
var j = 0
11+
while (j != words[i - 1].length && j != words[i].length && words[i - 1][j] == words[i][j]) {
12+
j++
13+
}
14+
if (j == words[i - 1].length) {
15+
continue
16+
}
17+
if (j == words[i].length) {
18+
return ""
19+
}
20+
adj[words[i - 1][j].code - 'a'.code].add(words[i][j].code - 'a'.code)
21+
}
22+
val visited = MutableList(adj.size) {false}
23+
words.forEach {
24+
it.map { it.code - 'a'.code }
25+
.forEach { visited[it] = true }
26+
}
27+
val inDegrees = MutableList(adj.size) {0} // Kahn T(V, E) = S(V, E) = O(V + E)
28+
adj.forEach {
29+
it.forEach { inDegrees[it]++ }
30+
}
31+
val ans = mutableListOf<Int>()
32+
val stack = mutableListOf<Int>()
33+
(0 until adj.size).filter { visited[it] && inDegrees[it] == 0 }
34+
.forEach {
35+
stack.add(it)
36+
ans.add(it)
37+
}
38+
while (! stack.isEmpty()) {
39+
val u = stack.removeLast()
40+
adj[u].forEach {
41+
if (--inDegrees[it] == 0) {
42+
stack.add(it)
43+
ans.add(it)
44+
}
45+
}
46+
}
47+
if ((0 until adj.size).any { visited[it] && inDegrees[it] != 0 }) {
48+
return ""
49+
}
50+
return ans.map { (it + 'a'.code).toChar() }
51+
.joinToString("")
52+
/*val decoder = MutableList(adj.size) {0}
53+
(0 until ans.size).forEach { decoder[ans[it]] = it }
54+
val decoded = words.map {
55+
it.map { (decoder[it.code - 'a'.code] + 'a'.code).toChar() }
56+
.joinToString("")
57+
}
58+
val sortedWords = decoded.sorted()
59+
return if (decoded == sortedWords) ans.map { (it + 'a'.code).toChar() }
60+
.joinToString("")
61+
else ""*/
62+
}
63+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* int val;
5+
* TreeNode? left;
6+
* TreeNode? right;
7+
* TreeNode([this.val = 0, this.left, this.right]);
8+
* }
9+
*/
10+
class Solution {
11+
TreeNode? buildTree(List<int> preorder, List<int> inorder) {
12+
int i = 1, j = 0;
13+
final root = TreeNode(preorder[0]);
14+
final stack = [root]; // T(n) = S(n) = O(n)
15+
while (i != preorder.length) {
16+
var u = stack.last;
17+
if (u.val != inorder[j]) {
18+
u.left = TreeNode(preorder[i++]);
19+
stack.add(u.left!);
20+
continue;
21+
}
22+
while (! stack.isEmpty && stack.last.val == inorder[j]) {
23+
u = stack.removeLast();
24+
j++;
25+
}
26+
u.right = TreeNode(preorder[i++]);
27+
stack.add(u.right!);
28+
}
29+
return root;
30+
}
31+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import scala.collection.mutable.ArrayBuffer
2+
3+
object Solution {
4+
def longestPalindrome(s: String): String = {
5+
val list = ArrayBuffer[Char]()
6+
for (c <- s) {
7+
list += c
8+
list += '\u0000'
9+
}
10+
val dp = Array.fill(list.size - 1)(0)
11+
var lastR = -1
12+
var lastMid = -1
13+
var maxD = 0
14+
var ansL = 0
15+
var ansR = 0
16+
var i = 1
17+
for (i <- 0 until dp.size) { // Manacher T(n) = S(n) = O(n)
18+
val diff = lastR - i
19+
var d = if (diff <= 0) 0 else diff min dp((lastMid << 1) - i)
20+
var l = i - d
21+
var r = i + d
22+
while (l != 0 && r != list.size - 1 && list(l - 1) == list(r + 1)) {
23+
d += 1
24+
l -= 1
25+
r += 1
26+
}
27+
if (maxD < d) {
28+
maxD = d
29+
ansL = l
30+
ansR = r
31+
}
32+
dp(i) = d
33+
if (lastR < r) {
34+
lastR = r
35+
lastMid = i
36+
}
37+
}
38+
val sb = StringBuilder()
39+
for (i <- ansL to ansR if list(i) != '\u0000') {
40+
sb.append(list(i))
41+
}
42+
sb.toString
43+
}
44+
}

rotate-image/yhkee0404.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func rotate(matrix [][]int) {
2+
for i := range len(matrix) {
3+
for j := i + 1; j != len(matrix[i]); j++ { // T(n) = O(n)
4+
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
5+
}
6+
}
7+
for _, row := range matrix {
8+
for i, j := 0, len(row) - 1; i < j; {
9+
row[i], row[j] = row[j], row[i]
10+
i++
11+
j--
12+
}
13+
}
14+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Definition for a binary tree node.
2+
// #[derive(Debug, PartialEq, Eq)]
3+
// pub struct TreeNode {
4+
// pub val: i32,
5+
// pub left: Option<Rc<RefCell<TreeNode>>>,
6+
// pub right: Option<Rc<RefCell<TreeNode>>>,
7+
// }
8+
//
9+
// impl TreeNode {
10+
// #[inline]
11+
// pub fn new(val: i32) -> Self {
12+
// TreeNode {
13+
// val,
14+
// left: None,
15+
// right: None
16+
// }
17+
// }
18+
// }
19+
use std::rc::Rc;
20+
use std::cell::RefCell;
21+
impl Solution {
22+
pub fn is_subtree(root: Option<Rc<RefCell<TreeNode>>>, sub_root: Option<Rc<RefCell<TreeNode>>>) -> bool {
23+
match &root {
24+
Some(u) => {
25+
let u = u.borrow();
26+
match &sub_root {
27+
Some(v) => {
28+
let v = v.borrow();
29+
Self::solve(root.clone(), sub_root.clone())
30+
|| Self::is_subtree(u.left.clone(), sub_root.clone())
31+
|| Self::is_subtree(u.right.clone(), sub_root.clone())
32+
},
33+
None => false,
34+
}
35+
},
36+
None => sub_root.is_none(),
37+
}
38+
}
39+
fn solve(root: Option<Rc<RefCell<TreeNode>>>, sub_root: Option<Rc<RefCell<TreeNode>>>) -> bool {
40+
match root {
41+
Some(u) => {
42+
let u = u.borrow();
43+
match &sub_root {
44+
Some(v) => {
45+
let v = v.borrow();
46+
u.val == v.val
47+
&& Self::solve(u.left.clone(), v.left.clone())
48+
&& Self::solve(u.right.clone(), v.right.clone())
49+
},
50+
None => false,
51+
}
52+
},
53+
None => sub_root.is_none(),
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)