Skip to content

Commit bb65a21

Browse files
authored
Add Median Of Sorted Lists (#126)
1 parent 5e485a3 commit bb65a21

File tree

6 files changed

+146
-3
lines changed

6 files changed

+146
-3
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Keep in mind that each challenge will usually have more than one solution. Even
2424
[10 different ways](https://eddmann.com/posts/ten-ways-to-reverse-a-string-in-javascript/). Usually, we compare various
2525
solutions using
2626
([Big O notation](https://medium.com/karuna-sehgal/a-simplified-explanation-of-the-big-o-notation-82523585e835)) to
27-
determine space/time complexity and we look at code readability.
27+
determine space/time complexity, and we look at code readability.
2828

2929
## How do I start?
3030

@@ -47,7 +47,7 @@ look at [problem-solving strategy](https://github.com/igorwojda/kotlin-coding-ch
4747
Take your time before you view the presented solution. To succeed you need to practice often, repeat the same challenges
4848
multiple times and be persistent over time.
4949

50-
**Good luck!**
50+
**Good luck! 🤞**
5151

5252
**New in Town**
5353

@@ -130,6 +130,7 @@ multiple times and be persistent over time.
130130
- [Min sub list length](src/test/kotlin/com/igorwojda/list/minsublistlength)
131131
- [Subtract](src/test/kotlin/com/igorwojda/list/subtract)
132132
- [Coins](src/test/kotlin/com/igorwojda/list/coins)
133+
- [Medan Of Sorted Lists](src/test/kotlin/com/igorwojda/list/medianoftwosorted/README.md)
133134

134135
# Useful links
135136

misc/ChallengeGroups.md

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ loops which decreases complexity from `O(n^2)` to `O(n)`.
4242
- [Get duplicated arguments](../src/test/kotlin/com/igorwojda/string/getduplicatedarguments/README.md)
4343
- [Midpoint](../src/test/kotlin/com/igorwojda/linkedlist/singly/midpoint/README.md)
4444
- [Circular check](../src/test/kotlin/com/igorwojda/linkedlist/singly/circularcheck/README.md)
45+
- [Medan Of Sorted Lists](../src/test/kotlin/com/igorwojda/list/medianoftwosorted/README.md)
4546

4647
## Frequency counter
4748

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.igorwojda.list.medianoftwosorted
2+
3+
import org.amshove.kluent.shouldBeEqualTo
4+
import org.junit.jupiter.api.Test
5+
6+
fun medianOfSortedLists(list1: List<Int>, list2: List<Int>): Double {
7+
val mergedList = list1
8+
.plus(list2)
9+
.sorted()
10+
11+
val median = if (mergedList.size % 2 != 0) {
12+
mergedList[mergedList.size / 2].toDouble()
13+
} else {
14+
(mergedList[mergedList.size / 2].toDouble() + mergedList[mergedList.size / 2 - 1].toDouble()) / 2
15+
}
16+
17+
return median
18+
}
19+
20+
private class Test {
21+
@Test
22+
fun `median of sorted lists 1, 3 and 2 returns 2,0`() {
23+
medianOfSortedLists(listOf(1, 3), listOf(2)) shouldBeEqualTo 2.0
24+
}
25+
26+
@Test
27+
fun `median of sorted lists 1, 3 and 2 returns 2,5`() {
28+
medianOfSortedLists(listOf(1, 2), listOf(3, 4)) shouldBeEqualTo 2.5
29+
}
30+
31+
@Test
32+
fun `median of sorted lists 2 and 1, 3 returns 2,0`() {
33+
medianOfSortedLists(listOf(2), listOf(1, 3)) shouldBeEqualTo 2.0
34+
}
35+
36+
@Test
37+
fun `median of sorted lists 1, 3, 4 and 2 returns 3,5`() {
38+
medianOfSortedLists(listOf(1, 5, 7), listOf(2)) shouldBeEqualTo 3.5
39+
}
40+
41+
@Test
42+
fun `median of sorted lists 2 and 1, 3, 4 returns 4,0`() {
43+
medianOfSortedLists(listOf(2), listOf(1, 6, 7)) shouldBeEqualTo 4.0
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Median Of Two Sorted Lists
2+
3+
## Instructions
4+
5+
Given two sorted lists `list1` and `list2` of integers return the median of the two sorted lists.
6+
7+
The overall run time complexity should be `O(log (m+n))`.
8+
9+
[Challenge](Challenge.kt) | [Solution](Solution.kt)
10+
11+
## Examples
12+
13+
```kotlin
14+
medianOfSortedLists(listOf(1, 3), listOf(2)) // 2.0 (merged list = [1, 2, 3] and median is 2)
15+
16+
medianOfSortedLists(listOf(1, 2), listOf(3, 4)) // 2.5 (merged list = [1, 2, 3, 4] and median is (2 + 3) / 2 = 2.5)
17+
18+
medianOfSortedLists(listOf(1, 5, 7), listOf(2)) // 3.5 (merged list = [1, 2, 5, 7] and median is (2 + 5) / 2 = 3.5)
19+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.igorwojda.list.medianoftwosorted
2+
3+
// Time complexity: O(log (m+n))
4+
private object Solution1 {
5+
fun medianOfSortedLists(list1: List<Int>, list2: List<Int>): Double {
6+
val totalSize = list1.size + list2.size
7+
8+
val lastIndex = (totalSize / 2)
9+
var prevValue: Int? = null
10+
11+
var pointer1Index = 0
12+
var pointer2Index = 0
13+
14+
(0..lastIndex).forEach {
15+
val value1 = list1.getOrNull(pointer1Index)
16+
val value2 = list2.getOrNull(pointer2Index)
17+
18+
val minValue = when {
19+
value1 != null && value2 == null -> {
20+
pointer1Index++
21+
value1
22+
}
23+
value2 != null && value1 == null -> {
24+
pointer2Index++
25+
value2
26+
}
27+
value1!! < value2!! -> {
28+
pointer1Index++
29+
value1
30+
}
31+
else -> {
32+
pointer2Index++
33+
value2
34+
}
35+
}
36+
37+
if (it == lastIndex) {
38+
val totalSizeIsOdd = totalSize % 2 != 0
39+
40+
return if (totalSizeIsOdd) {
41+
return minValue.toDouble()
42+
} else {
43+
val localPrevValue = prevValue
44+
45+
if (localPrevValue == null) {
46+
minValue.toDouble()
47+
} else {
48+
(localPrevValue + minValue) / 2.0
49+
}
50+
}
51+
}
52+
53+
prevValue = minValue
54+
println("-----------")
55+
}
56+
57+
return 0.0
58+
}
59+
}
60+
61+
// Time complexity: O(n)
62+
// Space complexity O(n)
63+
private object Solution2 {
64+
fun medianOfSortedLists(list1: List<Int>, list2: List<Int>): Double {
65+
val mergedList = list1
66+
.plus(list2)
67+
.sorted()
68+
69+
val median = if (mergedList.size % 2 != 0) {
70+
mergedList[mergedList.size / 2].toDouble()
71+
} else {
72+
(mergedList[mergedList.size / 2].toDouble() + mergedList[mergedList.size / 2 - 1].toDouble()) / 2
73+
}
74+
75+
return median
76+
}
77+
}

src/test/kotlin/com/igorwojda/list/pairaverage/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Find the pair with average
1+
# The pair with average
22

33
## Instructions
44

0 commit comments

Comments
 (0)