-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.ContainerWithMostWater.kt
40 lines (35 loc) · 1022 Bytes
/
11.ContainerWithMostWater.kt
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
//Two-pointer: time complexity O(n)
fun maxArea(height: IntArray): Int {
var left = 0
var right = height.size - 1
var maxArea = 0
while (left < right) {
val minHeight = Math.min(height[left], height[right])
val width = right - left
val currentArea = minHeight * width
maxArea = Math.max(maxArea, currentArea)
if (height[left] < height[right]) {
left++
} else {
right--
}
}
return maxArea
}
//Brute force: time complexity O(n²)
fun maxArea1(height: IntArray): Int {
var maxArea = 0
for (i in height.indices) {
for (j in i + 1 until height.size) {
// Calculate the area for the pair (i, j)
val width = j - i
val minHeight = minOf(height[i], height[j])
val area = width * minHeight
// Update maxArea if this area is larger
if (area > maxArea) {
maxArea = area
}
}
}
return maxArea
}