diff --git a/counting-bits/eunhwa99.kt b/counting-bits/eunhwa99.kt new file mode 100644 index 000000000..2b846e3b5 --- /dev/null +++ b/counting-bits/eunhwa99.kt @@ -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 + } +} diff --git a/find-median-from-data-stream/eunhwa99.kt b/find-median-from-data-stream/eunhwa99.kt new file mode 100644 index 000000000..324d46b62 --- /dev/null +++ b/find-median-from-data-stream/eunhwa99.kt @@ -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 { a, b -> b - a } + private val minHeap = PriorityQueue() + + // 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() + } + } + +} diff --git a/insert-interval/eunhwa99.kt b/insert-interval/eunhwa99.kt new file mode 100644 index 000000000..8796c09a9 --- /dev/null +++ b/insert-interval/eunhwa99.kt @@ -0,0 +1,36 @@ +class Solution { + fun insert(intervals: Array, newInterval: IntArray): Array { + val result = mutableListOf() + 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() + } +} diff --git a/kth-smallest-element-in-a-bst/eunhwa99.kt b/kth-smallest-element-in-a-bst/eunhwa99.kt new file mode 100644 index 000000000..6a04ff129 --- /dev/null +++ b/kth-smallest-element-in-a-bst/eunhwa99.kt @@ -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) + } +} diff --git a/lowest-common-ancestor-of-a-binary-search-tree/eunhwa99.kt b/lowest-common-ancestor-of-a-binary-search-tree/eunhwa99.kt new file mode 100644 index 000000000..15c6b583a --- /dev/null +++ b/lowest-common-ancestor-of-a-binary-search-tree/eunhwa99.kt @@ -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 + } + +} diff --git a/meeting-rooms/eunhwa99.java b/meeting-rooms/eunhwa99.java new file mode 100644 index 000000000..6023edaf9 --- /dev/null +++ b/meeting-rooms/eunhwa99.java @@ -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 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; + } +} +