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
52 changes: 52 additions & 0 deletions find-median-from-data-stream/yhkee0404.go
Original file line number Diff line number Diff line change
@@ -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();
*/
29 changes: 29 additions & 0 deletions insert-interval/yhkee0404.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
26 changes: 26 additions & 0 deletions kth-smallest-element-in-a-bst/yhkee0404.scala
Original file line number Diff line number Diff line change
@@ -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)
}
}
39 changes: 39 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/yhkee0404.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
// pub val: i32,
// pub left: Option<Rc<RefCell<TreeNode>>>,
// pub right: Option<Rc<RefCell<TreeNode>>>,
// }
//
// 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<Rc<RefCell<TreeNode>>>, p: Option<Rc<RefCell<TreeNode>>>, q: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
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)
}
},
}
}
}
24 changes: 24 additions & 0 deletions meeting-rooms/yhkee0404.kt
Original file line number Diff line number Diff line change
@@ -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<Interval?>): 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}
}
}