File tree Expand file tree Collapse file tree 6 files changed +176
-0
lines changed
find-median-from-data-stream
kth-smallest-element-in-a-bst
lowest-common-ancestor-of-a-binary-search-tree Expand file tree Collapse file tree 6 files changed +176
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ /*
3+ 0 -> 0
4+ 1 -> 1
5+ 2 -> 1
6+ 3 -> 2
7+ 4 -> 1
8+ 5 -> 2
9+ 6 -> 2
10+ 7 -> 3
11+ 8 -> 1
12+ 9 -> 2
13+ 10 -> 2
14+ 11 -> 3
15+ 12 -> 2
16+ 2,4,8,16,.. -> 1
17+ 짝수: i/2의 1의 개수
18+ 홀수: i/2의 1의 개수 + 1
19+ */
20+ // TC: O(n)
21+ // SC: O(n)
22+ fun countBits (n : Int ): IntArray {
23+ val result = IntArray (n + 1 )
24+ for (i in 0 .. n) {
25+ result[i] = result[i / 2 ] + (i % 2 )
26+ }
27+ return result
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ import java.util.PriorityQueue
2+ class MedianFinder () {
3+ // 1 2 3 4 5
4+ // 중간값을 기준으로
5+ // 3
6+ // 2 4
7+ // 1 5
8+ // 1 2 : maxHeap
9+ // 4 5 : minHeap
10+ private val maxHeap = PriorityQueue <Int > { a, b -> b - a }
11+ private val minHeap = PriorityQueue <Int >()
12+
13+ // TC: O(log n)
14+ // SC: O(n)
15+ fun addNum (num : Int ) {
16+ if (maxHeap.isEmpty() || maxHeap.peek() >= num) {
17+ maxHeap.add(num)
18+ } else {
19+ minHeap.add(num)
20+ }
21+
22+ if (maxHeap.size > minHeap.size + 1 ) {
23+ minHeap.add(maxHeap.poll())
24+ } else if (minHeap.size > maxHeap.size) {
25+ maxHeap.add(minHeap.poll())
26+ }
27+ }
28+
29+ fun findMedian (): Double {
30+ if (maxHeap.size == minHeap.size) {
31+ return (maxHeap.peek() + minHeap.peek()) / 2.0
32+ } else {
33+ return maxHeap.peek().toDouble()
34+ }
35+ }
36+
37+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ fun insert (intervals : Array <IntArray >, newInterval : IntArray ): Array <IntArray > {
3+ val result = mutableListOf<IntArray >()
4+ var currentStart = newInterval[0 ]
5+ var currentEnd = newInterval[1 ]
6+ var isAdded = false
7+
8+ // TC: O(n)
9+ // SC: O(n)
10+ for (interval in intervals){
11+ if (isAdded) {
12+ result.add(interval)
13+ } else {
14+ if (interval[1 ] < currentStart){ // new interval is after the current interval
15+ result.add(interval) // just add the interval without any changes
16+ }
17+ else if (interval[0 ] > currentEnd){ // new interval is before the current interval
18+ result.add(intArrayOf(currentStart, currentEnd))
19+ result.add(interval)
20+ isAdded = true
21+ }
22+ else { // intervals overlap, merge them
23+ currentStart = minOf(interval[0 ], currentStart)
24+ currentEnd = maxOf(interval[1 ], currentEnd)
25+ }
26+ }
27+ }
28+
29+ // If new interval wasn't added yet, add it now
30+ if (! isAdded) {
31+ result.add(intArrayOf(currentStart, currentEnd))
32+ }
33+
34+ return result.toTypedArray()
35+ }
36+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ // TC : O(H+K)
3+ // SC: O(H)
4+ private var K : Int = 0
5+ fun kthSmallest (root : TreeNode ? , k : Int ): Int {
6+ K = k
7+ return traverse(root)!! .`val `
8+ }
9+
10+ fun traverse (cur : TreeNode ? ): TreeNode ? {
11+ if (cur == null ) return cur
12+
13+ val result = traverse(cur.left)
14+ K --
15+ if (K == 0 ) return cur
16+ return result ? : traverse(cur.right)
17+ }
18+ }
Original file line number Diff line number Diff line change 1+ class TreeNode (var `val `: Int = 0 ) {
2+ var left: TreeNode ? = null
3+ var right: TreeNode ? = null
4+ }
5+
6+
7+ class Solution {
8+
9+ // TC: O(H) - H: height of BST
10+ // SC: O(1) - while loop: X stack
11+ fun lowestCommonAncestor (root : TreeNode ? , p : TreeNode ? , q : TreeNode ? ): TreeNode ? {
12+
13+ if (root == null || p == null || q == null ) return null
14+
15+ // BST: left < parent < right
16+ var current = root
17+ while (current != null ){
18+ if (current.`val ` < p.`val ` && current.`val ` < q.`val `){
19+ current = current.right
20+ }
21+ else if (current.`val ` > p.`val ` && current.`val ` > q.`val `){
22+ current = current.left
23+ }
24+ else return current
25+ }
26+
27+ return null
28+ }
29+
30+ }
Original file line number Diff line number Diff line change 1+
2+ // TC: O(N)
3+ // SC: O(1)
4+ class Solution {
5+ /**
6+ * @param intervals: an array of meeting time intervals
7+ * @return: if a person could attend all meetings
8+ */
9+ public boolean canAttendMeetings (List <Interval > intervals ) {
10+ // Edge case: empty list or single meeting
11+ if (intervals == null || intervals .size () <= 1 ) {
12+ return true ;
13+ }
14+
15+ intervals .sort (Comparator .comparing (v ->v .start ));
16+
17+ int prevEndTime = intervals .get (0 ).end ;
18+ for (int i = 1 ; i < intervals .size (); i ++){
19+ Interval cur = intervals .get (i );
20+ if (cur .start < prevEndTime ) return false ;
21+ prevEndTime = cur .end ;
22+ }
23+ return true ;
24+ }
25+ }
26+
You can’t perform that action at this time.
0 commit comments