https://leetcode.com/problems/container-with-most-water/
func maxArea(_ height: [Int]) -> Int {
var result = 0
for i in 0..<height.count {
for j in (i+1)..<height.count {
let subject = min(height[i], height[j])
let tempResult = subject * abs(i-j)
if result < tempResult {
result = tempResult
}
}
}
return result
}
같은 방법인데 2중 포문만 날려서 해보기로 함. (index 때문에 2중 포문 썼는 데 index 외부에서 주고 while문으로 처리하기)
근데 어차피 같은 방법인데 아래 같이 쓰다가 막힘.
var result = 0
var index1 = 0
let index2 = height.count - 1
while index1 < index2 {
let subject = min(height[index1], height[index2])
let width = abs(index1 - index2)
let water = subject * width
result = max(result, water)
index1 += 1
}
return result
if height[index1] < height[index2] {
index1 += 1
}
else {
index2 -= 1
}
var result = 0
var index1 = 0
var index2 = height.count - 1
while index1 < index2 {
let subject = min(height[index1], height[index2])
let width = abs(index1 - index2)
let water = subject * width
result = max(result, water)
if height[index1] < height[index2] {
index1 += 1
}
else {
index2 -= 1
}
}
return result