Skip to content

[eunhwa99] Week13 Solutions #1608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 2, 2025
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
29 changes: 29 additions & 0 deletions counting-bits/eunhwa99.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
/*
0 -> 0
1 -> 1
2 -> 1
3 -> 2
4 -> 1
5 -> 2
6 -> 2
7 -> 3
8 -> 1
9 -> 2
10 -> 2
11 -> 3
12 -> 2
2,4,8,16,.. -> 1
짝수: i/2의 1의 개수
홀수: i/2의 1의 개수 + 1
*/
// TC: O(n)
// SC: O(n)
fun countBits(n: Int): IntArray {
val result = IntArray(n + 1)
for(i in 0..n) {
result[i] = result[i / 2] + (i % 2)
}
return result
}
}
37 changes: 37 additions & 0 deletions find-median-from-data-stream/eunhwa99.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.util.PriorityQueue
class MedianFinder() {
// 1 2 3 4 5
// 중간값을 기준으로
// 3
// 2 4
// 1 5
// 1 2 : maxHeap
// 4 5 : minHeap
private val maxHeap = PriorityQueue<Int> { a, b -> b - a }
private val minHeap = PriorityQueue<Int>()

// TC: O(log n)
// SC: O(n)
fun addNum(num: Int) {
if(maxHeap.isEmpty() || maxHeap.peek() >= num) {
maxHeap.add(num)
} else {
minHeap.add(num)
}

if(maxHeap.size > minHeap.size + 1) {
minHeap.add(maxHeap.poll())
} else if(minHeap.size > maxHeap.size) {
maxHeap.add(minHeap.poll())
}
}

fun findMedian(): Double {
if(maxHeap.size == minHeap.size) {
return (maxHeap.peek() + minHeap.peek()) / 2.0
} else {
return maxHeap.peek().toDouble()
}
}

}
36 changes: 36 additions & 0 deletions insert-interval/eunhwa99.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Solution {
fun insert(intervals: Array<IntArray>, newInterval: IntArray): Array<IntArray> {
val result = mutableListOf<IntArray>()
var currentStart = newInterval[0]
var currentEnd = newInterval[1]
var isAdded = false

// TC: O(n)
// SC: O(n)
for(interval in intervals){
if(isAdded) {
result.add(interval)
} else {
if(interval[1] < currentStart){ // new interval is after the current interval
result.add(interval) // just add the interval without any changes
}
else if(interval[0] > currentEnd){ // new interval is before the current interval
result.add(intArrayOf(currentStart, currentEnd))
result.add(interval)
isAdded = true
}
else{ // intervals overlap, merge them
currentStart = minOf(interval[0], currentStart)
currentEnd = maxOf(interval[1], currentEnd)
}
}
}

// If new interval wasn't added yet, add it now
if(!isAdded) {
result.add(intArrayOf(currentStart, currentEnd))
}

return result.toTypedArray()
}
}
18 changes: 18 additions & 0 deletions kth-smallest-element-in-a-bst/eunhwa99.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
// TC : O(H+K)
// SC: O(H)
private var K: Int = 0
fun kthSmallest(root: TreeNode?, k: Int): Int {
K = k
return traverse(root)!!.`val`
}

fun traverse(cur: TreeNode?): TreeNode?{
if(cur == null) return cur

val result = traverse(cur.left)
K--
if(K == 0) return cur
return result ?: traverse(cur.right)
}
}
30 changes: 30 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/eunhwa99.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class TreeNode(var `val`: Int = 0) {
var left: TreeNode? = null
var right: TreeNode? = null
}


class Solution {

// TC: O(H) - H: height of BST
// SC: O(1) - while loop: X stack
fun lowestCommonAncestor(root: TreeNode?, p: TreeNode?, q: TreeNode?): TreeNode? {

if (root == null || p == null || q == null) return null

// BST: left < parent < right
var current = root
while(current != null){
if(current.`val` < p.`val` && current.`val` < q.`val`){
current = current.right
}
else if(current.`val` > p.`val` && current.`val` > q.`val`){
current = current.left
}
else return current
}

return null
}

}
26 changes: 26 additions & 0 deletions meeting-rooms/eunhwa99.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

// TC: O(N)
// SC: O(1)
class Solution {
/**
* @param intervals: an array of meeting time intervals
* @return: if a person could attend all meetings
*/
public boolean canAttendMeetings(List<Interval> intervals) {
// Edge case: empty list or single meeting
if (intervals == null || intervals.size() <= 1) {
return true;
}

intervals.sort(Comparator.comparing(v->v.start));

int prevEndTime = intervals.get(0).end;
for(int i = 1; i < intervals.size(); i++){
Interval cur = intervals.get(i);
if(cur.start < prevEndTime) return false;
prevEndTime = cur.end;
}
return true;
}
}