-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
144 lines (125 loc) · 2.92 KB
/
main.go
File metadata and controls
144 lines (125 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Source: https://leetcode.com/problems/trapping-rain-water
// Title: Trapping Rain Water
// Difficulty: Hard
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Given `n` non-negative integers representing an elevation map where the width of each bar is `1`, compute how much water it can trap after raining.
//
// **Example 1:**
// https://assets.leetcode.com/uploads/2018/10/22/rainwatertrap.png
//
// ```
// Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
// Output: 6
// Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
// ```
//
// **Example 2:**
//
// ```
// Input: height = [4,2,0,3,2,5]
// Output: 9
// ```
//
// **Constraints:**
//
// - `n == height.length`
// - `1 <= n <= 2 * 10^4`
// - `0 <= height[i] <= 10^5`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
import (
"slices"
)
// First, move from left to right until the highest point
// Next, do this in reserve direction.
func trap(height []int) int {
n := len(height)
res := 0
// Left to right
// Here we use `val >= maxVal` since there might be multiple highest points
{
maxVal, water := 0, 0
for idx := 0; idx < n; idx++ {
val := height[idx]
if val >= maxVal {
res += water
maxVal, water = val, 0
} else {
water += maxVal - val
}
}
}
// Right to left
// Here we use `val > maxVal` since the `=` case is handled in previous loop
{
maxVal, water := 0, 0
for idx := n - 1; idx >= 0; idx-- {
val := height[idx]
if val > maxVal {
res += water
maxVal, water = val, 0
} else {
water += maxVal - val
}
}
}
return res
}
// Two pointers
func trap2(height []int) int {
n := len(height)
res := 0
left, right := 0, n-1
leftMax, rightMax := 0, 0
for left < right {
if height[left] < height[right] {
if height[left] >= leftMax {
leftMax = height[left]
} else {
res += leftMax - height[left]
}
left++
} else {
if height[right] >= rightMax {
rightMax = height[right]
} else {
res += rightMax - height[right]
}
right--
}
}
return res
}
// Use array
func trap3(height []int) int {
n := len(height)
// Find heightest point
maxVal := slices.Max(height)
// Fill water with max val
water := make([]int, n)
for i := range n {
water[i] = maxVal
}
// Flow out water from left
water[0] = height[0]
for i := 1; i < n; i++ {
if water[i] > water[i-1] {
water[i] = max(water[i-1], height[i])
}
}
// Flow out water from right
water[n-1] = height[n-1]
for i := n - 2; i >= 0; i-- {
if water[i] > water[i+1] {
water[i] = max(water[i+1], height[i])
}
}
// Count water
res := 0
for i := range n {
res += water[i] - height[i]
}
return res
}