Skip to content

Commit 81b8183

Browse files
42: Trapping Rain Water
1 parent c7df7bd commit 81b8183

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ My solutions to LeetCode problems in Kotlin.
1010
| [5](https://leetcode.com/problems/longest-palindromic-substring/) | [Longest Palindromic Substring](src/main/kotlin/com/schmoczer/leetcode/_0005/LongestPalindromicSubstring.kt) | Medium |
1111
| [8](https://leetcode.com/problems/string-to-integer-atoi/) | [String to Integer (atoi)](src/main/kotlin/com/schmoczer/leetcode/_0008/StringToInteger.kt) | Medium |
1212
| [20](https://leetcode.com/problems/valid-parentheses/) | [Valid Parentheses](src/main/kotlin/com/schmoczer/leetcode/_0020/ValidParentheses.kt) | Easy |
13+
| [42](https://leetcode.com/problems/trapping-rain-water/) | [Trapping Rain Water](src/main/kotlin/com/schmoczer/leetcode/_0042/TrappingRainWater.kt) | Hard |
1314
| [49](https://leetcode.com/problems/group-anagrams/) | [Group Anagrams](src/main/kotlin/com/schmoczer/leetcode/_0049/GroupAnagrams.kt) | Medium |
1415
| [125](https://leetcode.com/problems/valid-palindrome/) | [Valid Palindrome](src/main/kotlin/com/schmoczer/leetcode/_0125/ValidPalindrome.kt) | Easy |
1516
| [151](https://leetcode.com/problems/reverse-words-in-a-string/) | [Reverse Words in a String](src/main/kotlin/com/schmoczer/leetcode/_0151/ReverseWordsInString.kt) | Medium |
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Trapping Rain Water
2+
3+
Given `n` non-negative integers representing an elevation map where the width of each bar is `1`, compute how much water
4+
it can trap after raining.
5+
6+
Example 1:
7+
8+
> Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
9+
>
10+
> Output: 6
11+
12+
Example 2:
13+
14+
> Input: height = [4,2,0,3,2,5]
15+
>
16+
> Output: 9
17+
18+
Constraints:
19+
20+
- `n == height.length`
21+
- `1 <= n <= 2 * 10^4`
22+
- `0 <= height[i] <= 10^5`
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.schmoczer.leetcode._0042
2+
3+
import kotlin.math.max
4+
import kotlin.math.min
5+
6+
class TrappingRainWater {
7+
// Approach 1: bruteforce
8+
fun trap1(height: IntArray): Int {
9+
if (height.size < 3) {
10+
return 0
11+
}
12+
var water = 0
13+
var leftHeight = height.first()
14+
var rightHeight = height.copyOfRange(2, height.size).max()
15+
for (i in 1 until height.size - 1) {
16+
val currentHeight = height[i]
17+
height[i - 1].let {
18+
if (it > leftHeight) {
19+
leftHeight = it
20+
}
21+
}
22+
if (currentHeight == rightHeight) {
23+
rightHeight = height.copyOfRange(i + 1, height.size).max()
24+
}
25+
val lowest = min(leftHeight, rightHeight)
26+
if (lowest > currentHeight) {
27+
water += lowest - currentHeight
28+
}
29+
}
30+
return water
31+
}
32+
33+
// Approach 2: 2 pointers, Runtime 1ms Beats 100.00%
34+
fun trap(height: IntArray): Int {
35+
if (height.size < 3) {
36+
return 0
37+
}
38+
var water = 0
39+
var leftIndex = 0
40+
var rightIndex = height.size - 1
41+
var leftHeight = 0
42+
var rightHeight = 0
43+
while (leftIndex < rightIndex) {
44+
if (height[leftIndex] < height[rightIndex]) {
45+
leftHeight = max(leftHeight, height[leftIndex])
46+
water += leftHeight - height[leftIndex]
47+
leftIndex++
48+
} else {
49+
rightHeight = max(rightHeight, height[rightIndex])
50+
water += rightHeight - height[rightIndex]
51+
rightIndex--
52+
}
53+
}
54+
return water
55+
}
56+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.schmoczer.leetcode._0042
2+
3+
import org.junit.jupiter.api.BeforeEach
4+
import kotlin.test.Test
5+
import kotlin.test.assertEquals
6+
7+
class TrappingRainWaterTests {
8+
private lateinit var sut: TrappingRainWater
9+
10+
@BeforeEach
11+
fun setUp() {
12+
sut = TrappingRainWater()
13+
}
14+
15+
@Test
16+
fun `0,1,0,2,1,0,1,3,2,1,2,1 traps 6 water`() {
17+
val input = intArrayOf(0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1)
18+
19+
assertEquals(6, sut.trap(input))
20+
}
21+
22+
@Test
23+
fun `4,2,0,3,2,5 traps 9 water`() {
24+
val input = intArrayOf(4, 2, 0, 3, 2, 5)
25+
26+
assertEquals(9, sut.trap(input))
27+
}
28+
}

0 commit comments

Comments
 (0)