Skip to content

Commit 7dd5fe5

Browse files
authored
Add Largest And Smallest Elements (#140)
1 parent 6ffac90 commit 7dd5fe5

File tree

8 files changed

+229
-7
lines changed

8 files changed

+229
-7
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ multiple times and be persistent over time.
8383
- [Pyramid generator](src/test/kotlin/com/igorwojda/integer/pyramidgenerator)
8484
- [Steps generator](src/test/kotlin/com/igorwojda/integer/stepsgenerator)
8585
- [Fizz Buzz](src/test/kotlin/com/igorwojda/integer/fizzbuzz)
86-
- [Caesar Cipher](src/test/kotlin/com/igorwojda/string/caesarcipher)
86+
- [Caesar cipher](src/test/kotlin/com/igorwojda/string/caesarcipher)
8787
- [Has repeated char](src/test/kotlin/com/igorwojda/string/hasrepeatedcharacter)
88+
- [Smallest elements](src/test/kotlin/com/igorwojda/list/smallestelements)
89+
- [Largest elements](src/test/kotlin/com/igorwojda/list/largestelements)
8890

8991
**Advanced**
9092

@@ -114,7 +116,7 @@ multiple times and be persistent over time.
114116
- [Radix sort](src/test/kotlin/com/igorwojda/list/sort/radixsort)
115117
- [Doubly linked list](src/test/kotlin/com/igorwojda/linkedlist/doubly/base)
116118
- [Max binary heap](src/test/kotlin/com/igorwojda/tree/heap/maxbinaryheap)
117-
- [Add Numbers](src/test/kotlin/com/igorwojda/linkedlist/singly/addnumbers)
119+
- [Add numbers](src/test/kotlin/com/igorwojda/linkedlist/singly/addnumbers)
118120

119121
**Expert**
120122

@@ -131,7 +133,7 @@ multiple times and be persistent over time.
131133
- [Min sub list length](src/test/kotlin/com/igorwojda/list/minsublistlength)
132134
- [Subtract](src/test/kotlin/com/igorwojda/list/subtract)
133135
- [Coins](src/test/kotlin/com/igorwojda/list/coins)
134-
- [Medan Of Sorted Lists](src/test/kotlin/com/igorwojda/list/medianoftwosorted)
136+
- [Medan of sorted lists](src/test/kotlin/com/igorwojda/list/medianoftwosorted)
135137
- [LRU Cache](src/test/kotlin/com/igorwojda/cache/lru)
136138
- [Advanced Cache](src/test/kotlin/com/igorwojda/cache/advancedlru)
137139

misc/ChallengeGroups.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ loops which decreases complexity from `O(n^2)` to `O(n)`.
4242
- [Get duplicated arguments](../src/test/kotlin/com/igorwojda/string/getduplicatedarguments)
4343
- [Midpoint](../src/test/kotlin/com/igorwojda/linkedlist/singly/midpoint)
4444
- [Circular check](../src/test/kotlin/com/igorwojda/linkedlist/singly/circularcheck)
45-
- [Medan Of Sorted Lists](../src/test/kotlin/com/igorwojda/list/medianoftwosorted)
45+
- [Medan of sorted lists](../src/test/kotlin/com/igorwojda/list/medianoftwosorted)
4646

4747
## Frequency counter
4848

@@ -71,7 +71,7 @@ We use sliding window instead of nested loops which decreases complexity from `O
7171

7272
- [Linear search](../src/test/kotlin/com/igorwojda/list/search/linearsearch)
7373
- [Binary search](../src/test/kotlin/com/igorwojda/list/search/binarysearch)
74-
- [Medan Of Sorted Lists](../src/test/kotlin/com/igorwojda/list/medianoftwosorted)
74+
- [Medan of sorted lists](../src/test/kotlin/com/igorwojda/list/medianoftwosorted)
7575

7676
### Sort algorithms
7777

@@ -95,14 +95,16 @@ We use sliding window instead of nested loops which decreases complexity from `O
9595
- [List subtract](../src/test/kotlin/com/igorwojda/list/subtract)
9696
- [Sum zero](../src/test/kotlin/com/igorwojda/list/sumzero)
9797
- [Coins](../src/test/kotlin/com/igorwojda/list/coins)
98+
- [Smallest elements](../src/test/kotlin/com/igorwojda/list/smallestelements)
99+
- [Largest elements](../src/test/kotlin/com/igorwojda/list/largestelements)
98100

99101
## Singly Linked List
100102

101103
- [Singly linked list](../src/test/kotlin/com/igorwojda/linkedlist/singly/base)
102104
- [Circular linked list?](../src/test/kotlin/com/igorwojda/linkedlist/singly/circularcheck)
103105
- [Take n-th element from last](../src/test/kotlin/com/igorwojda/linkedlist/singly/fromlast)
104106
- [Midpoint](../src/test/kotlin/com/igorwojda/linkedlist/singly/midpoint)
105-
- [Add Numbers](../src/test/kotlin/com/igorwojda/linkedlist/singly/addnumbers)
107+
- [Add numbers](../src/test/kotlin/com/igorwojda/linkedlist/singly/addnumbers)
106108

107109
## Doubly Linked List
108110

@@ -134,7 +136,7 @@ We use sliding window instead of nested loops which decreases complexity from `O
134136

135137
## String
136138

137-
- [Caesar Cipher](../src/test/kotlin/com/igorwojda/string/caesarcipher)
139+
- [Caesar cipher](../src/test/kotlin/com/igorwojda/string/caesarcipher)
138140
- [Capitalize sentence](../src/test/kotlin/com/igorwojda/string/capitalizesentence)
139141
- [Decapitalize const](../src/test/kotlin/com/igorwojda/string/decapitalizeconst)
140142
- [Get duplicated arguments](../src/test/kotlin/com/igorwojda/string/getduplicatedarguments)
@@ -185,3 +187,8 @@ We use sliding window instead of nested loops which decreases complexity from `O
185187

186188
- [LRU Cache](../src/test/kotlin/com/igorwojda/cache/lru)
187189
- [Advanced Cache](../src/test/kotlin/com/igorwojda/cache/advancedlru)
190+
191+
## Priority Queue
192+
193+
- [Smallest elements](../src/test/kotlin/com/igorwojda/list/smallestelements)
194+
- [Largest elements](../src/test/kotlin/com/igorwojda/list/largestelements)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.igorwojda.list.largestelements
2+
3+
import org.amshove.kluent.shouldBeEqualTo
4+
import org.junit.jupiter.api.Test
5+
import java.util.*
6+
7+
private fun largestElements(list: List<Int>, count: Int): List<Int> {
8+
if (list.size <= count) {
9+
return list
10+
}
11+
12+
val priorityQueue = PriorityQueue<Int>()
13+
14+
repeat(count) {
15+
priorityQueue.add(list[it])
16+
}
17+
18+
list
19+
.drop(count)
20+
.forEach {
21+
if (it > priorityQueue.peek()) {
22+
priorityQueue.poll()
23+
priorityQueue.add(it)
24+
}
25+
}
26+
27+
return priorityQueue.toList()
28+
}
29+
30+
private class Test {
31+
@Test
32+
fun `3 largest elements`() {
33+
val list = listOf(5, 1, 3)
34+
35+
largestElements(list, 3) shouldBeEqualTo listOf(5, 1, 3)
36+
}
37+
38+
@Test
39+
fun `2 largest elements`() {
40+
val list = listOf(5, 1, 3)
41+
42+
largestElements(list, 2) shouldBeEqualTo listOf(3, 5)
43+
}
44+
45+
@Test
46+
fun `1 largest element`() {
47+
val list = listOf(5, 1, 3)
48+
49+
largestElements(list, 1) shouldBeEqualTo listOf(5)
50+
}
51+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Largest Elements
2+
3+
Implement a function that takes a list of integers and an integer `count` as input parameters. The purpose of the
4+
function is to find the largest `count` numbers from the provided list.
5+
6+
If the size of the list is less than or equal to 'count', the function should return the original list.
7+
8+
[Challenge](Challenge.kt) | [Solution](Solution.kt)
9+
10+
## Examples
11+
12+
Example 1
13+
14+
```kotlin
15+
val list = listOf(5, 1, 3)
16+
largestElements(list, 2) shouldBeEqualTo listOf(3, 5)
17+
```
18+
19+
Example 2
20+
21+
```kotlin
22+
val list = listOf(5, 1, 3)
23+
largestElements(list, 3) shouldBeEqualTo listOf(5, 1, 3)
24+
```
25+
26+
## Hints
27+
28+
<details>
29+
<summary>Hint 1</summary>
30+
Use `PriorityQueue` to store the largest elements.
31+
</details>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.igorwojda.list.largestelements
2+
3+
import java.util.*
4+
5+
// Time complexity: O(n log k)
6+
private object Solution1 {
7+
private fun largestElements(list: List<Int>, count: Int): List<Int> {
8+
if (list.size <= count) {
9+
return list
10+
}
11+
12+
val priorityQueue = PriorityQueue<Int>()
13+
14+
repeat(count) {
15+
priorityQueue.add(list[it])
16+
}
17+
18+
list
19+
.drop(count)
20+
.forEach {
21+
if (it > priorityQueue.peek()) {
22+
priorityQueue.poll()
23+
priorityQueue.add(it)
24+
}
25+
}
26+
27+
return priorityQueue.toList()
28+
}
29+
}
30+
31+
private object KtLintWillNotComplain
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.igorwojda.list.smallestelements
2+
3+
import org.amshove.kluent.shouldBeEqualTo
4+
import org.junit.jupiter.api.Test
5+
6+
private fun smallestElements(list: List<Int>, count: Int): List<Int> {
7+
TODO("Add your solution here")
8+
}
9+
10+
private class Test {
11+
@Test
12+
fun `4 smallest elements`() {
13+
val list = listOf(5, 1, 3)
14+
15+
smallestElements(list, 3) shouldBeEqualTo listOf(5, 1, 3)
16+
}
17+
18+
@Test
19+
fun `3 smallest elements`() {
20+
val list = listOf(5, 1, 3)
21+
22+
smallestElements(list, 3) shouldBeEqualTo listOf(5, 1, 3)
23+
}
24+
25+
@Test
26+
fun `2 smallest elements`() {
27+
val list = listOf(5, 1, 3)
28+
29+
smallestElements(list, 2) shouldBeEqualTo listOf(3, 1)
30+
}
31+
32+
@Test
33+
fun `1 smallest element`() {
34+
val list = listOf(5, 1, 3)
35+
36+
smallestElements(list, 1) shouldBeEqualTo listOf(1)
37+
}
38+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Smallest Elements
2+
3+
Implement a function that takes a list of integers and an integer `count` as input parameters. The purpose of the
4+
function is to find the smallest `count` numbers from the provided list.
5+
6+
If the size of the list is less than or equal to 'count', the function should return the original list.
7+
8+
[Challenge](Challenge.kt) | [Solution](Solution.kt)
9+
10+
## Examples
11+
12+
Example 1
13+
14+
```kotlin
15+
val list = listOf(5, 1, 3)
16+
smallestElements(list, 2) shouldBeEqualTo listOf(3, 1)
17+
```
18+
19+
Example 2
20+
21+
```kotlin
22+
val list = listOf(5, 1, 3)
23+
smallestElements(list, 3) shouldBeEqualTo listOf(5, 1, 3)
24+
```
25+
26+
## Hints
27+
28+
<details>
29+
<summary>Hint 1</summary>
30+
Use `PriorityQueue` to store the smallest elements.
31+
</details>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.igorwojda.list.smallestelements
2+
3+
import java.util.*
4+
5+
// Time complexity: O(n log k)
6+
private object Solution1 {
7+
private fun smallestElements(list: List<Int>, count: Int): List<Int> {
8+
if (list.size <= count) {
9+
return list
10+
}
11+
12+
val priorityQueue = PriorityQueue<Int>(Collections.reverseOrder())
13+
14+
repeat(count) {
15+
priorityQueue.add(list[it])
16+
}
17+
18+
list
19+
.drop(count)
20+
.forEach {
21+
if (it < priorityQueue.peek()) {
22+
priorityQueue.poll()
23+
priorityQueue.add(it)
24+
}
25+
}
26+
27+
return priorityQueue.toList()
28+
}
29+
}
30+
31+
private object KtLintWillNotComplain

0 commit comments

Comments
 (0)