diff --git a/find-median-from-data-stream/yhkee0404.go b/find-median-from-data-stream/yhkee0404.go new file mode 100644 index 000000000..f36687b65 --- /dev/null +++ b/find-median-from-data-stream/yhkee0404.go @@ -0,0 +1,52 @@ +type MedianFinder struct { + lq Heap + rq Heap +} + +type Heap []int // S(n) = O(n) + +func (pq Heap) Len() int {return len(pq)} +func (pq Heap) Less(i, j int) bool {return pq[i] < pq[j]} +func (pq Heap) Swap(i, j int) {pq[i], pq[j] = pq[j], pq[i]} +func (pq *Heap) Push(x any) {*pq = append(*pq, x.(int))} +func (pq *Heap) Pop() any { + x := (*pq)[len(*pq) - 1] + *pq = (*pq)[: len(*pq) - 1] + return x +} + + +func Constructor() MedianFinder { + return MedianFinder{} +} + + +func (this *MedianFinder) AddNum(num int) { // T(n) = O(logn) + if len(this.rq) != 0 && this.rq[0] <= num { + heap.Push(&this.rq, num) + if len(this.lq) == len(this.rq) - 1 { + heap.Push(&this.lq, - heap.Pop(&this.rq).(int)) + } + return + } + heap.Push(&this.lq, - num) + if len(this.lq) == len(this.rq) + 2 { + heap.Push(&this.rq, - heap.Pop(&this.lq).(int)) + } +} + + +func (this *MedianFinder) FindMedian() float64 { // T(n) = O(1) + if len(this.lq) > len(this.rq) { + return float64(- this.lq[0]) + } + return float64(- this.lq[0] + this.rq[0]) / 2 +} + + +/** + * Your MedianFinder object will be instantiated and called as such: + * obj := Constructor(); + * obj.AddNum(num); + * param_2 := obj.FindMedian(); + */ diff --git a/insert-interval/yhkee0404.swift b/insert-interval/yhkee0404.swift new file mode 100644 index 000000000..349bf65f6 --- /dev/null +++ b/insert-interval/yhkee0404.swift @@ -0,0 +1,29 @@ +class Solution { + func insert(_ intervals: [[Int]], _ newInterval: [Int]) -> [[Int]] { + var ans: [[Int]] = [] + var i = 0 + while i != intervals.count && intervals[i][1] < newInterval[0] { + ans.append(intervals[i]) + i += 1 + } + var newInterval = newInterval + if i != intervals.count && intervals[i][0] <= newInterval[0] { + newInterval[0] = intervals[i][0] + newInterval[1] = max(newInterval[1], intervals[i][1]) + i += 1 + } + while i != intervals.count && intervals[i][1] <= newInterval[1] { + i += 1 + } + if i != intervals.count && intervals[i][0] <= newInterval[1] { + newInterval[1] = intervals[i][1] + i += 1 + } + ans.append(newInterval) + while i != intervals.count { + ans.append(intervals[i]) + i += 1 + } + return ans + } +} diff --git a/kth-smallest-element-in-a-bst/yhkee0404.scala b/kth-smallest-element-in-a-bst/yhkee0404.scala new file mode 100644 index 000000000..8c079267f --- /dev/null +++ b/kth-smallest-element-in-a-bst/yhkee0404.scala @@ -0,0 +1,26 @@ +/** + * Definition for a binary tree node. + * class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) { + * var value: Int = _value + * var left: TreeNode = _left + * var right: TreeNode = _right + * } + */ +object Solution { + def kthSmallest(root: TreeNode, k: Int): Int = { + count(root, k, -1) + } + def count(root: TreeNode, k: Int, n: Int): Int = { + if (root == null) { + return n + } + val u = count(root.left, k, n) + if (u >= 0) { + return u + } + if (- u == k) { + return root.value + } + count(root.right, k, u - 1) + } +} diff --git a/lowest-common-ancestor-of-a-binary-search-tree/yhkee0404.rs b/lowest-common-ancestor-of-a-binary-search-tree/yhkee0404.rs new file mode 100644 index 000000000..17ca6bedd --- /dev/null +++ b/lowest-common-ancestor-of-a-binary-search-tree/yhkee0404.rs @@ -0,0 +1,39 @@ +// Definition for a binary tree node. +// #[derive(Debug, PartialEq, Eq)] +// pub struct TreeNode { +// pub val: i32, +// pub left: Option>>, +// pub right: Option>>, +// } +// +// 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 lowest_common_ancestor(root: Option>>, p: Option>>, q: Option>>) -> Option>> { + match root.as_ref() { + None => None, + Some(u) if p.as_ref().map_or(false, |pp| Rc::ptr_eq(u, pp)) => p, + Some(u) if q.as_ref().map_or(false, |qq| Rc::ptr_eq(u, qq)) => q, + Some(u) => { + let u = u.borrow(); + let left = Self::lowest_common_ancestor(u.left.clone(), p.clone(), q.clone()); + let right = Self::lowest_common_ancestor(u.right.clone(), p.clone(), q.clone()); + if left.is_some() && right.is_some() { + root.clone() + } else { + left.or(right) + } + }, + } + } +} diff --git a/meeting-rooms/yhkee0404.kt b/meeting-rooms/yhkee0404.kt new file mode 100644 index 000000000..eec365c45 --- /dev/null +++ b/meeting-rooms/yhkee0404.kt @@ -0,0 +1,24 @@ +/** + * Definition of Interval: + * class Interval { + * var start: Int = 0 + * var end: Int = 0 + * constructor(start: Int, end: Int) { + * this.start = start + * this.end = end + * } + * } + */ + +class Solution { + /** + * @param intervals: an array of meeting time intervals + * @return: if a person could attend all meetings + */ + fun canAttendMeetings(intervals: List): Boolean { + // Write your code here + val sorted = intervals.filterNotNull() // T(n) = O(nlogn) + .sortedBy {it!!.end} + return (1 until intervals.size).all {sorted[it].start >= sorted[it - 1].end} + } +}