From ac7a3f16eebf6bf859c4f0df06592035efb04be6 Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Sun, 19 Oct 2025 20:36:22 +0900 Subject: [PATCH 1/5] meeting rooms solution --- meeting-rooms/yhkee0404.kt | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 meeting-rooms/yhkee0404.kt 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} + } +} From 40872d8ee1f966d43b2446cce7d457165779af81 Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Sun, 19 Oct 2025 21:51:16 +0900 Subject: [PATCH 2/5] lowest common ancestor of a binary search tree solution --- .../yhkee0404.rs | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 lowest-common-ancestor-of-a-binary-search-tree/yhkee0404.rs 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) + } + }, + } + } +} From 1c3c75d87397800baac62dab4eecb0d1aa5a3117 Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Mon, 20 Oct 2025 00:36:51 +0900 Subject: [PATCH 3/5] kth smallest element in a bst solution --- kth-smallest-element-in-a-bst/yhkee0404.scala | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 kth-smallest-element-in-a-bst/yhkee0404.scala 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) + } +} From 1266ecaccaa309c0327a11d2ebf5b0e89d4538f0 Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Mon, 20 Oct 2025 01:03:13 +0900 Subject: [PATCH 4/5] insert interval solution --- insert-interval/yhkee0404.swift | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 insert-interval/yhkee0404.swift 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 + } +} From 367a86226f942fb85659cc409815e06e53f2c8d4 Mon Sep 17 00:00:00 2001 From: yhkee0404 Date: Mon, 20 Oct 2025 01:07:39 +0900 Subject: [PATCH 5/5] find median from data stream solution --- find-median-from-data-stream/yhkee0404.go | 52 +++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 find-median-from-data-stream/yhkee0404.go 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(); + */