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
20 changes: 20 additions & 0 deletions container-with-most-water/yhkee0404.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
func maxArea(height []int) int {
ans := 0
i, j := 0, len(height) - 1
for i < j {
dh := height[i] - height[j]
dx := j - i
k := j
if dh == 0 {
i++
j--
} else if dh > 0 {
j--
} else {
k = i
i++
}
ans = max(ans, dx * height[k])
}
return ans
}
74 changes: 74 additions & 0 deletions design-add-and-search-words-data-structure/yhkee0404.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use std::collections::HashMap;

struct WordDictionary {
trie: Box<Trie>,
}

struct Trie {
ended: bool,
children: HashMap<char, Box<Trie>>,
}


/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl WordDictionary {

fn new() -> Self {
WordDictionary {
trie: Box::new(Trie::new()),
}
}

// T(n) = S(n) = O(n)
fn add_word(&mut self, word: String) {
let mut t = &mut self.trie;
for c in word.chars() {
t = t.children
.entry(c)
.or_insert(Box::new(Trie::new()))
}
t.ended = true
}

fn search(&self, word: String) -> bool {
return Self::search_from_index(&self.trie, &word)
}

// T(n) = S(n) = O((n - 2) * 26^2) = O(n)
fn search_from_index(trie: &Trie, word: &str) -> bool {
return match word.chars()
.next() {
None => trie.ended,
Some(c) => if c == '.' {
trie.children
.values()
.any(|x| Self::search_from_index(&x, &word[1..])) // Slice O(1)
} else {
match trie.children
.get(&c) {
None => false,
Some(x) => Self::search_from_index(&x, &word[1..]), // Slice O(1)
}
},
}
}
}

/**
* Your WordDictionary object will be instantiated and called as such:
* let obj = WordDictionary::new();
* obj.add_word(word);
* let ret_2: bool = obj.search(word);
*/

impl Trie {
fn new() -> Self {
Trie {
ended: false,
children: HashMap::new(),
}
}
}
22 changes: 22 additions & 0 deletions longest-increasing-subsequence/yhkee0404.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
int lengthOfLIS(List<int> nums) {
final stack = []; // S(n) = O(n)
nums.forEach((x) { // T(n) = O(nlgn)
int i = -1, j = stack.length - 1;
while (i != j) { // O(lgn)
final int mid = j - ((j - i) >> 1);
if (stack[mid] < x) {
i = mid;
} else {
j = mid - 1;
}
}
if (++i == stack.length) {
stack.add(x);
} else {
stack[i] = x;
}
});
return stack.length;
}
}
30 changes: 30 additions & 0 deletions spiral-matrix/yhkee0404.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
fun spiralOrder(matrix: Array<IntArray>): List<Int> {
val visited = List(matrix.size) {MutableList(matrix.first().size) {false}} // T(n) = S(n) = O(n)
val drcs = listOf(
listOf(0, 1,),
listOf(1, 0,),
listOf(0, -1,),
listOf(-1, 0,),
)
val ans = mutableListOf<Int>()
search(ans, matrix, visited, drcs, 0, 0, 0, false)
return ans
}
}

fun search(ans: MutableList<Int>, matrix: Array<IntArray>, visited: List<MutableList<Boolean>>, drcs: List<List<Int>>, r: Int, c: Int, dir: Int, turned: Boolean) {
val drc = drcs[dir]
if (r == -1 || r == matrix.size || c == -1 || c == matrix.first().size || visited[r][c]) {
if (turned) {
return
}
val nDir = (dir + 1) % drcs.size
val nDrc = drcs[nDir]
search(ans, matrix, visited, drcs, r - drc[0] + nDrc[0], c - drc[1] + nDrc[1], nDir, true)
return
}
visited[r][c] = true
ans.add(matrix[r][c])
search(ans, matrix, visited, drcs, r + drc[0], c + drc[1], dir, false)
}
26 changes: 26 additions & 0 deletions valid-parentheses/yhkee0404.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import scala.collection.mutable.ArrayBuffer

object Solution {
var encoder = "(){}[]".zipWithIndex
.toMap
def isValid(s: String): Boolean = {
val arr = ArrayBuffer[Int]()
s.forall { c =>
val x = encoder(c)
if ((x & 1) == 0) {
arr += x
true
} else {
arr.lastOption match {
case Some(y) => {
if (y == x - 1) {
arr.dropRightInPlace (1)
true
} else false
}
case None => false
}
}
} && arr.isEmpty
}
}