File tree Expand file tree Collapse file tree 5 files changed +144
-0
lines changed
non-overlapping-intervals
number-of-connected-components-in-an-undirected-graph
remove-nth-node-from-end-of-list
serialize-and-deserialize-binary-tree Expand file tree Collapse file tree 5 files changed +144
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ // TC: O(n log n) - intervals๋ฅผ ์ ๋ ฌํ๋๋ฐ O(n log n)
3
+ // SC: O(1) - ์ถ๊ฐ์ ์ธ ๊ณต๊ฐ์ ์ฌ์ฉํ์ง ์์
4
+ fun eraseOverlapIntervals (intervals : Array <IntArray >): Int {
5
+ intervals.sortBy { it[1 ] } // intervals๋ฅผ ๋๋๋ ์๊ฐ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ
6
+
7
+ var prevEnd = intervals[0 ][1 ]
8
+ var count = 0
9
+ for (i in 1 until intervals.size) {
10
+ val curStart = intervals[i][0 ]
11
+ if (curStart < prevEnd) { // ๋ ๊ตฌ๊ฐ์ด ๊ฒน์น๋ค.
12
+ count++
13
+ } else {
14
+ prevEnd = intervals[i][1 ] // ๊ฒน์น์ง ์์ผ๋ฉด prevEnd๋ฅผ ๊ฐฑ์
15
+ }
16
+
17
+ }
18
+ return count
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ // Time Complexity: O(n + e)
3
+ // Space Complexity: O(n + e)
4
+ // n : number of nodes
5
+ // e : number of edges
6
+ fun countComponents (n : Int , edges : Array <IntArray >): Int {
7
+ val graph = Array (n) { mutableListOf<Int >() }
8
+ for (edge in edges) { // make graph
9
+ graph[edge[0 ]].add(edge[1 ])
10
+ graph[edge[1 ]].add(edge[0 ])
11
+ }
12
+ val visited = BooleanArray (n) // visited array
13
+ var count = 0 // number of connected components
14
+
15
+ for (i in 0 until n) {
16
+ if (! visited[i]) {
17
+ dfs(i, graph, visited)
18
+ count++
19
+ }
20
+ }
21
+ return count
22
+ }
23
+
24
+ fun dfs (node : Int , graph : Array <MutableList <Int >>, visited : BooleanArray ) {
25
+ visited[node] = true
26
+ for (neighbor in graph[node]) {
27
+ if (! visited[neighbor]) {
28
+ dfs(neighbor, graph, visited)
29
+ }
30
+ }
31
+ }
32
+ }
Original file line number Diff line number Diff line change
1
+ package leetcode_study
2
+
3
+ class ListNode (var `val `: Int ) {
4
+ var next: ListNode ? = null
5
+ }
6
+
7
+ // ํํธ ์ฐธ๊ณ
8
+ // Time Complexity: O(L) where L is the length of the linked list
9
+ // Space Complexity: O(1) since we are using only a constant amount of space
10
+ class Solution {
11
+ fun removeNthFromEnd (head : ListNode ? , n : Int ): ListNode ? {
12
+ var left = head
13
+ var right = head
14
+ var step = n
15
+ while (step-- > 0 ) {
16
+ right = right?.next
17
+ }
18
+ if (right == null ) {
19
+ return head?.next // n์ด ๋ฆฌ์คํธ์ ๊ธธ์ด์ ๊ฐ์ ๋, ์ฆ ์ฒซ ๋ฒ์งธ ๋
ธ๋๋ฅผ ์ ๊ฑฐํด์ผ ํ๋ ๊ฒฝ์ฐ
20
+ }
21
+ while (right?.next != null ) {
22
+ left = left?.next
23
+ right = right.next
24
+ }
25
+
26
+ left?.next = left?.next?.next
27
+ return head
28
+ }
29
+ }
30
+
31
+
Original file line number Diff line number Diff line change
1
+ package leetcode_study
2
+
3
+ class TreeNode (var `val `: Int ) {
4
+ var left: TreeNode ? = null
5
+ var right: TreeNode ? = null
6
+ }
7
+
8
+ // Time Complexity: O(n)
9
+ // Space Complexity: O(h) where h is the height of the tree (์ฌ๊ทํจ์๋ก ์ธํ ๊ณต๊ฐ ๋ณต์ก๋)
10
+ class Solution {
11
+ fun isSameTree (p : TreeNode ? , q : TreeNode ? ): Boolean {
12
+ return if (p == null && q == null ) {
13
+ true
14
+ } else if (p == null || q == null || p.`val ` != q.`val `) {
15
+ false
16
+ } else {
17
+ isSameTree(p.left, q.left) && isSameTree(p.right, q.right)
18
+ }
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ package leetcode_study
2
+
3
+ class Codec {
4
+ private var index = 0
5
+ // Time Complexity: O(n)
6
+ // Space Complexity: O(n)
7
+ fun serialize (root : TreeNode ? ): String {
8
+ return buildString {
9
+ traverse(root, this )
10
+ }
11
+ }
12
+
13
+ fun traverse (node : TreeNode ? , sb : StringBuilder ) {
14
+ if (node == null ) {
15
+ sb.append(" null," )
16
+ return
17
+ }
18
+ sb.append(" ${node.`val `} ," )
19
+ traverse(node.left, sb)
20
+ traverse(node.right, sb)
21
+ }
22
+
23
+ // Time Complexity: O(n)
24
+ // Space Complexity: O(n)
25
+ fun deserialize (data : String ): TreeNode ? {
26
+ val values = data.split(" ," ).filter { it.isNotEmpty() }.toList()
27
+ index = 0
28
+ return buildTree(values)
29
+ }
30
+
31
+ fun buildTree (values : List <String >): TreeNode ? {
32
+ if (index >= values.size) return null
33
+ val value = values[index++ ] // index ๋ ๋งค๊ฐ๋ณ์๊ฐ ์๋๋ผ ์ ์ญ์ผ๋ก ๋์ด์ผ ํ๋ค!!
34
+ if (value == " null" ) return null
35
+ val node = TreeNode (value.toInt())
36
+ node.left = buildTree(values)
37
+ node.right = buildTree(values)
38
+ return node
39
+ }
40
+ }
41
+
You canโt perform that action at this time.
0 commit comments