Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Median Of Sorted Lists #126

Merged
merged 6 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Keep in mind that each challenge will usually have more than one solution. Even
[10 different ways](https://eddmann.com/posts/ten-ways-to-reverse-a-string-in-javascript/). Usually, we compare various
solutions using
([Big O notation](https://medium.com/karuna-sehgal/a-simplified-explanation-of-the-big-o-notation-82523585e835)) to
determine space/time complexity and we look at code readability.
determine space/time complexity, and we look at code readability.

## How do I start?

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

**Good luck!**
**Good luck! 🤞**

**New in Town**

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

# Useful links

Expand Down
1 change: 1 addition & 0 deletions misc/ChallengeGroups.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ loops which decreases complexity from `O(n^2)` to `O(n)`.
- [Get duplicated arguments](../src/test/kotlin/com/igorwojda/string/getduplicatedarguments/README.md)
- [Midpoint](../src/test/kotlin/com/igorwojda/linkedlist/singly/midpoint/README.md)
- [Circular check](../src/test/kotlin/com/igorwojda/linkedlist/singly/circularcheck/README.md)
- [Medan Of Sorted Lists](../src/test/kotlin/com/igorwojda/list/medianoftwosorted/README.md)

## Frequency counter

Expand Down
45 changes: 45 additions & 0 deletions src/test/kotlin/com/igorwojda/list/medianoftwosorted/Challenge.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.igorwojda.list.medianoftwosorted

import org.amshove.kluent.shouldBeEqualTo
import org.junit.jupiter.api.Test

fun medianOfSortedLists(list1: List<Int>, list2: List<Int>): Double {
val mergedList = list1
.plus(list2)
.sorted()

val median = if (mergedList.size % 2 != 0) {
mergedList[mergedList.size / 2].toDouble()
} else {
(mergedList[mergedList.size / 2].toDouble() + mergedList[mergedList.size / 2 - 1].toDouble()) / 2
}

return median
}

private class Test {
@Test
fun `median of sorted lists 1, 3 and 2 returns 2,0`() {
medianOfSortedLists(listOf(1, 3), listOf(2)) shouldBeEqualTo 2.0
}

@Test
fun `median of sorted lists 1, 3 and 2 returns 2,5`() {
medianOfSortedLists(listOf(1, 2), listOf(3, 4)) shouldBeEqualTo 2.5
}

@Test
fun `median of sorted lists 2 and 1, 3 returns 2,0`() {
medianOfSortedLists(listOf(2), listOf(1, 3)) shouldBeEqualTo 2.0
}

@Test
fun `median of sorted lists 1, 3, 4 and 2 returns 3,5`() {
medianOfSortedLists(listOf(1, 5, 7), listOf(2)) shouldBeEqualTo 3.5
}

@Test
fun `median of sorted lists 2 and 1, 3, 4 returns 4,0`() {
medianOfSortedLists(listOf(2), listOf(1, 6, 7)) shouldBeEqualTo 4.0
}
}
19 changes: 19 additions & 0 deletions src/test/kotlin/com/igorwojda/list/medianoftwosorted/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Median Of Two Sorted Lists

## Instructions

Given two sorted lists `list1` and `list2` of integers return the median of the two sorted lists.

The overall run time complexity should be `O(log (m+n))`.

[Challenge](Challenge.kt) | [Solution](Solution.kt)

## Examples

```kotlin
medianOfSortedLists(listOf(1, 3), listOf(2)) // 2.0 (merged list = [1, 2, 3] and median is 2)

medianOfSortedLists(listOf(1, 2), listOf(3, 4)) // 2.5 (merged list = [1, 2, 3, 4] and median is (2 + 3) / 2 = 2.5)

medianOfSortedLists(listOf(1, 5, 7), listOf(2)) // 3.5 (merged list = [1, 2, 5, 7] and median is (2 + 5) / 2 = 3.5)
```
77 changes: 77 additions & 0 deletions src/test/kotlin/com/igorwojda/list/medianoftwosorted/Solution.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.igorwojda.list.medianoftwosorted

// Time complexity: O(log (m+n))
private object Solution1 {
fun medianOfSortedLists(list1: List<Int>, list2: List<Int>): Double {
val totalSize = list1.size + list2.size

val lastIndex = (totalSize / 2)
var prevValue: Int? = null

var pointer1Index = 0
var pointer2Index = 0

(0..lastIndex).forEach {
val value1 = list1.getOrNull(pointer1Index)
val value2 = list2.getOrNull(pointer2Index)

val minValue = when {
value1 != null && value2 == null -> {
pointer1Index++
value1
}
value2 != null && value1 == null -> {
pointer2Index++
value2
}
value1!! < value2!! -> {
pointer1Index++
value1
}
else -> {
pointer2Index++
value2
}
}

if (it == lastIndex) {
val totalSizeIsOdd = totalSize % 2 != 0

return if (totalSizeIsOdd) {
return minValue.toDouble()
} else {
val localPrevValue = prevValue

if (localPrevValue == null) {
minValue.toDouble()
} else {
(localPrevValue + minValue) / 2.0
}
}
}

prevValue = minValue
println("-----------")
}

return 0.0
}
}

// Time complexity: O(n)
// Space complexity O(n)
private object Solution2 {
fun medianOfSortedLists(list1: List<Int>, list2: List<Int>): Double {
val mergedList = list1
.plus(list2)
.sorted()

val median = if (mergedList.size % 2 != 0) {
mergedList[mergedList.size / 2].toDouble()
} else {
(mergedList[mergedList.size / 2].toDouble() + mergedList[mergedList.size / 2 - 1].toDouble()) / 2
}

return median
}
}
2 changes: 1 addition & 1 deletion src/test/kotlin/com/igorwojda/list/pairaverage/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Find the pair with average
# The pair with average

## Instructions

Expand Down