Skip to content

Commit 8500301

Browse files
authored
Merge pull request #1860 from yhkee0404/main
[yhkee0404] WEEK 06 solutions
2 parents 66de80b + 1b81fc7 commit 8500301

File tree

5 files changed

+172
-0
lines changed

5 files changed

+172
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func maxArea(height []int) int {
2+
ans := 0
3+
i, j := 0, len(height) - 1
4+
for i < j {
5+
dh := height[i] - height[j]
6+
dx := j - i
7+
k := j
8+
if dh == 0 {
9+
i++
10+
j--
11+
} else if dh > 0 {
12+
j--
13+
} else {
14+
k = i
15+
i++
16+
}
17+
ans = max(ans, dx * height[k])
18+
}
19+
return ans
20+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
use std::collections::HashMap;
2+
3+
struct WordDictionary {
4+
trie: Box<Trie>,
5+
}
6+
7+
struct Trie {
8+
ended: bool,
9+
children: HashMap<char, Box<Trie>>,
10+
}
11+
12+
13+
/**
14+
* `&self` means the method takes an immutable reference.
15+
* If you need a mutable reference, change it to `&mut self` instead.
16+
*/
17+
impl WordDictionary {
18+
19+
fn new() -> Self {
20+
WordDictionary {
21+
trie: Box::new(Trie::new()),
22+
}
23+
}
24+
25+
// T(n) = S(n) = O(n)
26+
fn add_word(&mut self, word: String) {
27+
let mut t = &mut self.trie;
28+
for c in word.chars() {
29+
t = t.children
30+
.entry(c)
31+
.or_insert(Box::new(Trie::new()))
32+
}
33+
t.ended = true
34+
}
35+
36+
fn search(&self, word: String) -> bool {
37+
return Self::search_from_index(&self.trie, &word)
38+
}
39+
40+
// T(n) = S(n) = O((n - 2) * 26^2) = O(n)
41+
fn search_from_index(trie: &Trie, word: &str) -> bool {
42+
return match word.chars()
43+
.next() {
44+
None => trie.ended,
45+
Some(c) => if c == '.' {
46+
trie.children
47+
.values()
48+
.any(|x| Self::search_from_index(&x, &word[1..])) // Slice O(1)
49+
} else {
50+
match trie.children
51+
.get(&c) {
52+
None => false,
53+
Some(x) => Self::search_from_index(&x, &word[1..]), // Slice O(1)
54+
}
55+
},
56+
}
57+
}
58+
}
59+
60+
/**
61+
* Your WordDictionary object will be instantiated and called as such:
62+
* let obj = WordDictionary::new();
63+
* obj.add_word(word);
64+
* let ret_2: bool = obj.search(word);
65+
*/
66+
67+
impl Trie {
68+
fn new() -> Self {
69+
Trie {
70+
ended: false,
71+
children: HashMap::new(),
72+
}
73+
}
74+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
int lengthOfLIS(List<int> nums) {
3+
final stack = []; // S(n) = O(n)
4+
nums.forEach((x) { // T(n) = O(nlgn)
5+
int i = -1, j = stack.length - 1;
6+
while (i != j) { // O(lgn)
7+
final int mid = j - ((j - i) >> 1);
8+
if (stack[mid] < x) {
9+
i = mid;
10+
} else {
11+
j = mid - 1;
12+
}
13+
}
14+
if (++i == stack.length) {
15+
stack.add(x);
16+
} else {
17+
stack[i] = x;
18+
}
19+
});
20+
return stack.length;
21+
}
22+
}

spiral-matrix/yhkee0404.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
fun spiralOrder(matrix: Array<IntArray>): List<Int> {
3+
val visited = List(matrix.size) {MutableList(matrix.first().size) {false}} // T(n) = S(n) = O(n)
4+
val drcs = listOf(
5+
listOf(0, 1,),
6+
listOf(1, 0,),
7+
listOf(0, -1,),
8+
listOf(-1, 0,),
9+
)
10+
val ans = mutableListOf<Int>()
11+
search(ans, matrix, visited, drcs, 0, 0, 0, false)
12+
return ans
13+
}
14+
}
15+
16+
fun search(ans: MutableList<Int>, matrix: Array<IntArray>, visited: List<MutableList<Boolean>>, drcs: List<List<Int>>, r: Int, c: Int, dir: Int, turned: Boolean) {
17+
val drc = drcs[dir]
18+
if (r == -1 || r == matrix.size || c == -1 || c == matrix.first().size || visited[r][c]) {
19+
if (turned) {
20+
return
21+
}
22+
val nDir = (dir + 1) % drcs.size
23+
val nDrc = drcs[nDir]
24+
search(ans, matrix, visited, drcs, r - drc[0] + nDrc[0], c - drc[1] + nDrc[1], nDir, true)
25+
return
26+
}
27+
visited[r][c] = true
28+
ans.add(matrix[r][c])
29+
search(ans, matrix, visited, drcs, r + drc[0], c + drc[1], dir, false)
30+
}

valid-parentheses/yhkee0404.scala

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import scala.collection.mutable.ArrayBuffer
2+
3+
object Solution {
4+
var encoder = "(){}[]".zipWithIndex
5+
.toMap
6+
def isValid(s: String): Boolean = {
7+
val arr = ArrayBuffer[Int]()
8+
s.forall { c =>
9+
val x = encoder(c)
10+
if ((x & 1) == 0) {
11+
arr += x
12+
true
13+
} else {
14+
arr.lastOption match {
15+
case Some(y) => {
16+
if (y == x - 1) {
17+
arr.dropRightInPlace (1)
18+
true
19+
} else false
20+
}
21+
case None => false
22+
}
23+
}
24+
} && arr.isEmpty
25+
}
26+
}

0 commit comments

Comments
 (0)