Skip to content

Commit 20dc2e3

Browse files
authored
Add time and space complexity comments to solutions (#247)
1 parent 5c4301e commit 20dc2e3

81 files changed

Lines changed: 432 additions & 148 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎misc/template/challenge/Solution.kt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.igorwojda.challenge
22

33
// Time complexity: O(?)
4+
// Space complexity: O(?)
45
private object Solution1 {
56
private fun challenge(str: String): String {
67
TODO()
78
}
89
}
910

1011
// Time complexity: O(?)
12+
// Space complexity: O(?)
1113
private object Solution2 {
1214
private fun challenge(str: String): String {
1315
TODO()

‎src/test/kotlin/com/igorwojda/cache/advancedlru/Solution.kt‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import java.time.Duration
55
import java.util.PriorityQueue
66

77
// Implementation is using combination of HashMap and PriorityQueue.
8-
// Time Complexity: O(N) (JVM priority queue is O(log(n)) on offer/poll methods and O(N) on remove(item) method)
8+
// Time complexity:
9+
// put: O(n) - plus O(n) per expired item evicted; PriorityQueue.remove(item) is O(n), offer/poll O(log n)
10+
// get: O(n) - PriorityQueue.remove(item) is O(n)
11+
// Space complexity: O(n) - n is number of cached items
912
internal object Solution1 {
1013
class AdvancedLRUCache<K : Any, V : Any>(private val capacity: Int, private val clock: Clock = Clock.systemDefaultZone()) : LRUCache<K, V> {
1114
private val map: MutableMap<K, CacheItem<K, V>> = mutableMapOf()

‎src/test/kotlin/com/igorwojda/cache/lru/Solution.kt‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.igorwojda.cache.lru
22

33
// Implementation is using combination of HashMap and LinkedList.
4-
// Time Complexity: O(1)
4+
// Time complexity:
5+
// put: O(1)
6+
// get: O(1)
7+
// Space complexity: O(n) - n is capacity
58
object Solution1 {
69
class LRUCacheImpl<K : Any, V : Any>(
710
private val capacity: Int,
@@ -111,7 +114,10 @@ object Solution1 {
111114
}
112115

113116
// Implementation using LinkedHashMap
114-
// Time Complexity: O(1)
117+
// Time complexity:
118+
// put: O(1)
119+
// get: O(1)
120+
// Space complexity: O(n) - n is capacity
115121
object Solution2 {
116122
class LRUCacheImpl<K : Any, V : Any>(
117123
private val capacity: Int,
@@ -139,7 +145,10 @@ object Solution2 {
139145
}
140146

141147
// Implementation using List
142-
// Time Complexity: O(n)
148+
// Time complexity:
149+
// put: O(n) - removeIf and removeFirst on ArrayList
150+
// get: O(n) - linear search and remove
151+
// Space complexity: O(n) - n is capacity
143152
object Solution3 {
144153
class LRUCacheImpl<K : Any, V : Any>(
145154
private val capacity: Int,

‎src/test/kotlin/com/igorwojda/common/anycallback/Solution.kt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.igorwojda.common.anycallback
22

3+
// Time complexity: O(n^2) - drop(1) copies the list at each level
4+
// Space complexity: O(n^2) - each stack frame holds its own dropped copy
35
internal object Solution1 {
46
fun <T : Any> anyCallback(
57
list: List<T>,
@@ -17,6 +19,8 @@ internal object Solution1 {
1719
}
1820
}
1921

22+
// Time complexity: O(n) - subList is an O(1) view
23+
// Space complexity: O(n) - recursion call stack
2024
internal object Solution2 {
2125
fun <T : Any> anyCallback(
2226
list: List<T>,
@@ -27,6 +31,8 @@ internal object Solution2 {
2731
}
2832
}
2933

34+
// Time complexity: O(n) - O(n^2) for non-RandomAccess lists (drop copies)
35+
// Space complexity: O(n) - recursion call stack; O(n^2) for non-RandomAccess lists
3036
internal object Solution3 {
3137
fun <T : Any> anyCallback(
3238
list: List<T>,

‎src/test/kotlin/com/igorwojda/integer/addupto/Solution.kt‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
package com.igorwojda.integer.addupto
22

33
// Kotlin idiomatic solution
4+
// Time complexity: O(n)
5+
// Space complexity: O(1)
46
private object Solution1 {
57
private fun addUpTo(n: Int): Int = (1..n).sum()
68
}
79

810
// Kotlin idiomatic solution
11+
// Time complexity: O(n)
12+
// Space complexity: O(1)
913
private object Solution2 {
1014
private fun addUpTo(n: Int): Int = (0..n).fold(0) { accumulated, current -> accumulated + current }
1115
}
1216

1317
// Recursive solution
18+
// Time complexity: O(n)
19+
// Space complexity: O(n) - recursion call stack
1420
private object Solution3 {
1521
private fun addUpTo(n: Int): Int {
1622
if (n == 1) {
@@ -21,14 +27,16 @@ private object Solution3 {
2127
}
2228
}
2329

24-
// Time Complexity: O(1)
2530
// Mathematical formula
31+
// Time complexity: O(1)
32+
// Space complexity: O(1)
2633
private object Solution4 {
2734
private fun addUpTo(n: Int): Int = n * (n + 1) / 2
2835
}
2936

30-
// Time Complexity: O(n)
3137
// Iterative solution
38+
// Time complexity: O(n)
39+
// Space complexity: O(1)
3240
private object Solution5 {
3341
private fun addUpTo(n: Int): Int {
3442
var total = 0
@@ -40,6 +48,8 @@ private object Solution5 {
4048
}
4149

4250
// Iterative solution
51+
// Time complexity: O(n)
52+
// Space complexity: O(1)
4353
private object Solution6 {
4454
private fun addUpTo(n: Int): Int {
4555
var total = 0

‎src/test/kotlin/com/igorwojda/integer/countdown/Solution.kt‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.igorwojda.integer.countdown
22

33
// Kotlin idiomatic solution
4+
// Time complexity: O(n)
5+
// Space complexity: O(n)
46
private object Solution1 {
57
private fun countDown(n: Int): List<Int> {
68
// Create a range and convert it to a list
@@ -9,6 +11,8 @@ private object Solution1 {
911
}
1012

1113
// Recursive solution
14+
// Time complexity: O(n^2) - each level copies the result of the recursive call
15+
// Space complexity: O(n) - result list and recursion call stack
1216
private object Solution2 {
1317
private fun countDown(n: Int): List<Int> {
1418
if (n == 0) {
@@ -20,6 +24,8 @@ private object Solution2 {
2024
}
2125

2226
// Recursive solution with helper function
27+
// Time complexity: O(n^2) - each level copies the result of the recursive call
28+
// Space complexity: O(n) - result list and recursion call stack
2329
private object Solution3 {
2430
private fun countDown(n: Int): List<Int> {
2531
// We want to keep return type unchanged while implementing recursive solution, so we will
@@ -37,6 +43,8 @@ private object Solution3 {
3743
}
3844

3945
// Kotlin idiomatic solution
46+
// Time complexity: O(n)
47+
// Space complexity: O(n)
4048
private object Solution4 {
4149
private fun countDown(n: Int): List<Int> = List(n + 1) { n - it }
4250
}

‎src/test/kotlin/com/igorwojda/integer/countupanddown/Solution.kt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.igorwojda.integer.countupanddown
22

33
// Calculate element value from element position
4+
// Time complexity: O(n)
5+
// Space complexity: O(n)
46
private object Solution1 {
57
private fun countUpAndDown(n: Int): List<Int> {
68
val size = (n * 2) + 1
@@ -15,6 +17,8 @@ private object Solution1 {
1517
}
1618

1719
// Returns a new list combining one collection of numbers from 0 to n-1 and another with numbers from n to 0
20+
// Time complexity: O(n)
21+
// Space complexity: O(n)
1822
private object Solution2 {
1923
private fun countUpAndDown(n: Int): List<Int> = (0 until n) + (n downTo 0)
2024
}

‎src/test/kotlin/com/igorwojda/integer/digitfrequency/Solution.kt‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.igorwojda.integer.digitfrequency
22

3-
// Time complexity: O(n)
43
// Generate digit frequency map for each integer and compare them
4+
// Time complexity: O(d) - d is number of digits
5+
// Space complexity: O(d)
56
private object Solution1 {
67
private fun equalDigitFrequency(
78
i1: Int,
@@ -20,9 +21,10 @@ private object Solution1 {
2021
}
2122
}
2223

23-
// Time complexity: O(n^2)
2424
// Loop through each character of first integer and look for this character in another integer. If character if found
2525
// remove it from second integer to make sure that character frequency match.
26+
// Time complexity: O(d^2) - d is number of digits; indexOf and removeAt are O(d)
27+
// Space complexity: O(d)
2628
private object Solution2 {
2729
private fun equalDigitFrequency(
2830
i1: Int,

‎src/test/kotlin/com/igorwojda/integer/factorial/Solution.kt‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.igorwojda.integer.factorial
22

33
// iterative solution
4+
// Time complexity: O(n)
5+
// Space complexity: O(1)
46
private object Solution1 {
57
private fun factorial(n: Int): Int {
68
var total = 1
@@ -14,6 +16,8 @@ private object Solution1 {
1416
}
1517

1618
// another iterative solution
19+
// Time complexity: O(n)
20+
// Space complexity: O(1)
1721
private object Solution2 {
1822
private fun factorial(n: Int): Int = when (n) {
1923
0 -> 1
@@ -22,6 +26,8 @@ private object Solution2 {
2226
}
2327

2428
// recursive solution
29+
// Time complexity: O(n)
30+
// Space complexity: O(n) - recursion call stack
2531
private object Solution3 {
2632
private fun factorial(n: Int): Int = when (n) {
2733
0, 1 -> 1
@@ -30,6 +36,8 @@ private object Solution3 {
3036
}
3137

3238
// Tail-recursive solution
39+
// Time complexity: O(n)
40+
// Space complexity: O(n) - recursion call stack (no tailrec modifier, so not optimized)
3341
private object Solution4 {
3442
private fun factorial(n: Int): Int {
3543
fun fact(

‎src/test/kotlin/com/igorwojda/integer/fibonacci/basic/Solution.kt‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.igorwojda.integer.fibonacci.basic
22

33
// iterative solution
4+
// Time complexity: O(n)
5+
// Space complexity: O(1)
46
private object Solution1 {
57
private fun fibonacci(n: Int): Int {
68
if (n < 2) {
@@ -22,6 +24,8 @@ private object Solution1 {
2224
}
2325

2426
// iterative solution with temporary list (not efficient)
27+
// Time complexity: O(n)
28+
// Space complexity: O(n)
2529
private object Solution2 {
2630
private fun fibonacci(n: Int): Int {
2731
val list = mutableListOf(0, 1)
@@ -35,6 +39,8 @@ private object Solution2 {
3539
}
3640

3741
// recursive solution
42+
// Time complexity: O(2^n)
43+
// Space complexity: O(n) - recursion call stack
3844
private object Solution3 {
3945
private fun fibonacci(n: Int): Int = when (n) {
4046
0 -> 0
@@ -44,6 +50,8 @@ private object Solution3 {
4450
}
4551

4652
// recursive solution
53+
// Time complexity: O(2^n)
54+
// Space complexity: O(n) - recursion call stack
4755
private object Solution4 {
4856
private fun fibonacci(n: Int): Int {
4957
if (n < 2) {

0 commit comments

Comments
 (0)