File tree Expand file tree Collapse file tree 2 files changed +6
-9
lines changed
main/kotlin/common/twopointers/largestcontainer
test/kotlin/common/twopointers/largestcontainer Expand file tree Collapse file tree 2 files changed +6
-9
lines changed Original file line number Diff line number Diff line change @@ -3,18 +3,15 @@ package common.twopointers.largestcontainer
3
3
import kotlin.math.max
4
4
import kotlin.math.min
5
5
6
- class LargestContainerNaive {
6
+ class LargestContainerBruteForce {
7
7
8
8
fun solution (heights : List <Int >): Int {
9
+ val n = heights.size
9
10
var maxWater = 0
10
11
// Find the maximum amount of water stored between all pairs of lines.
11
- for (i in heights.indices ) {
12
+ for (i in 0 until n ) {
12
13
for (j in i + 1 until heights.size) {
13
- // Select minimum among them. Water can be contained depending on lower height.
14
- val width = min(heights[i], heights[j])
15
- val length = j - i
16
- // Rectangle formula.
17
- val water = width * length
14
+ val water = min(heights[i], heights[j]) * (j - i)
18
15
maxWater = max(maxWater, water)
19
16
}
20
17
}
Original file line number Diff line number Diff line change @@ -3,9 +3,9 @@ package common.twopointers.largestcontainer
3
3
import org.junit.jupiter.api.Test
4
4
import kotlin.test.assertEquals
5
5
6
- class LargestContainerNaiveTest {
6
+ class LargestContainerBruteForceTest {
7
7
8
- private val question = LargestContainerNaive ()
8
+ private val question = LargestContainerBruteForce ()
9
9
10
10
@Test
11
11
fun `Verify solution` () {
You can’t perform that action at this time.
0 commit comments