Skip to content

Commit 459ae30

Browse files
authored
Merge pull request #1948 from yhkee0404/main
[yhkee0404] WEEK 13 solutions
2 parents 4234f4f + 367a862 commit 459ae30

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
type MedianFinder struct {
2+
lq Heap
3+
rq Heap
4+
}
5+
6+
type Heap []int // S(n) = O(n)
7+
8+
func (pq Heap) Len() int {return len(pq)}
9+
func (pq Heap) Less(i, j int) bool {return pq[i] < pq[j]}
10+
func (pq Heap) Swap(i, j int) {pq[i], pq[j] = pq[j], pq[i]}
11+
func (pq *Heap) Push(x any) {*pq = append(*pq, x.(int))}
12+
func (pq *Heap) Pop() any {
13+
x := (*pq)[len(*pq) - 1]
14+
*pq = (*pq)[: len(*pq) - 1]
15+
return x
16+
}
17+
18+
19+
func Constructor() MedianFinder {
20+
return MedianFinder{}
21+
}
22+
23+
24+
func (this *MedianFinder) AddNum(num int) { // T(n) = O(logn)
25+
if len(this.rq) != 0 && this.rq[0] <= num {
26+
heap.Push(&this.rq, num)
27+
if len(this.lq) == len(this.rq) - 1 {
28+
heap.Push(&this.lq, - heap.Pop(&this.rq).(int))
29+
}
30+
return
31+
}
32+
heap.Push(&this.lq, - num)
33+
if len(this.lq) == len(this.rq) + 2 {
34+
heap.Push(&this.rq, - heap.Pop(&this.lq).(int))
35+
}
36+
}
37+
38+
39+
func (this *MedianFinder) FindMedian() float64 { // T(n) = O(1)
40+
if len(this.lq) > len(this.rq) {
41+
return float64(- this.lq[0])
42+
}
43+
return float64(- this.lq[0] + this.rq[0]) / 2
44+
}
45+
46+
47+
/**
48+
* Your MedianFinder object will be instantiated and called as such:
49+
* obj := Constructor();
50+
* obj.AddNum(num);
51+
* param_2 := obj.FindMedian();
52+
*/

insert-interval/yhkee0404.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
func insert(_ intervals: [[Int]], _ newInterval: [Int]) -> [[Int]] {
3+
var ans: [[Int]] = []
4+
var i = 0
5+
while i != intervals.count && intervals[i][1] < newInterval[0] {
6+
ans.append(intervals[i])
7+
i += 1
8+
}
9+
var newInterval = newInterval
10+
if i != intervals.count && intervals[i][0] <= newInterval[0] {
11+
newInterval[0] = intervals[i][0]
12+
newInterval[1] = max(newInterval[1], intervals[i][1])
13+
i += 1
14+
}
15+
while i != intervals.count && intervals[i][1] <= newInterval[1] {
16+
i += 1
17+
}
18+
if i != intervals.count && intervals[i][0] <= newInterval[1] {
19+
newInterval[1] = intervals[i][1]
20+
i += 1
21+
}
22+
ans.append(newInterval)
23+
while i != intervals.count {
24+
ans.append(intervals[i])
25+
i += 1
26+
}
27+
return ans
28+
}
29+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) {
4+
* var value: Int = _value
5+
* var left: TreeNode = _left
6+
* var right: TreeNode = _right
7+
* }
8+
*/
9+
object Solution {
10+
def kthSmallest(root: TreeNode, k: Int): Int = {
11+
count(root, k, -1)
12+
}
13+
def count(root: TreeNode, k: Int, n: Int): Int = {
14+
if (root == null) {
15+
return n
16+
}
17+
val u = count(root.left, k, n)
18+
if (u >= 0) {
19+
return u
20+
}
21+
if (- u == k) {
22+
return root.value
23+
}
24+
count(root.right, k, u - 1)
25+
}
26+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Definition for a binary tree node.
2+
// #[derive(Debug, PartialEq, Eq)]
3+
// pub struct TreeNode {
4+
// pub val: i32,
5+
// pub left: Option<Rc<RefCell<TreeNode>>>,
6+
// pub right: Option<Rc<RefCell<TreeNode>>>,
7+
// }
8+
//
9+
// impl TreeNode {
10+
// #[inline]
11+
// pub fn new(val: i32) -> Self {
12+
// TreeNode {
13+
// val,
14+
// left: None,
15+
// right: None
16+
// }
17+
// }
18+
// }
19+
use std::rc::Rc;
20+
use std::cell::RefCell;
21+
impl Solution {
22+
pub fn lowest_common_ancestor(root: Option<Rc<RefCell<TreeNode>>>, p: Option<Rc<RefCell<TreeNode>>>, q: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
23+
match root.as_ref() {
24+
None => None,
25+
Some(u) if p.as_ref().map_or(false, |pp| Rc::ptr_eq(u, pp)) => p,
26+
Some(u) if q.as_ref().map_or(false, |qq| Rc::ptr_eq(u, qq)) => q,
27+
Some(u) => {
28+
let u = u.borrow();
29+
let left = Self::lowest_common_ancestor(u.left.clone(), p.clone(), q.clone());
30+
let right = Self::lowest_common_ancestor(u.right.clone(), p.clone(), q.clone());
31+
if left.is_some() && right.is_some() {
32+
root.clone()
33+
} else {
34+
left.or(right)
35+
}
36+
},
37+
}
38+
}
39+
}

meeting-rooms/yhkee0404.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition of Interval:
3+
* class Interval {
4+
* var start: Int = 0
5+
* var end: Int = 0
6+
* constructor(start: Int, end: Int) {
7+
* this.start = start
8+
* this.end = end
9+
* }
10+
* }
11+
*/
12+
13+
class Solution {
14+
/**
15+
* @param intervals: an array of meeting time intervals
16+
* @return: if a person could attend all meetings
17+
*/
18+
fun canAttendMeetings(intervals: List<Interval?>): Boolean {
19+
// Write your code here
20+
val sorted = intervals.filterNotNull() // T(n) = O(nlogn)
21+
.sortedBy {it!!.end}
22+
return (1 until intervals.size).all {sorted[it].start >= sorted[it - 1].end}
23+
}
24+
}

0 commit comments

Comments
 (0)