Skip to content

Commit 871575e

Browse files
committed
[WEEK6](gmlwls96) Container With Most Water
1 parent ceda89c commit 871575e

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
/** 시간 : O(n), 공간 : O(1)*/
3+
fun maxArea(height: IntArray): Int {
4+
var maxDiff = 0
5+
var left = 0
6+
var right = height.lastIndex
7+
// left, right값을 순차적으로 조회해서 물높이를 구하고,
8+
// left < right값 보다 작으면 left증가시킨다. 반대는 right 감소
9+
while (left < right) {
10+
maxDiff = max(maxDiff, (right - left) * min(height[left], height[right]))
11+
// 너비 : right - left
12+
// 현재 높이 : min(height[left], height[right])
13+
// 너비 * 현재 높이가 maxDiff 비교하여 더 큰값이 maxDiff가 된다.
14+
if (height[left] < height[right]) {
15+
left++
16+
} else {
17+
right--
18+
}
19+
}
20+
return maxDiff
21+
}
22+
}

valid-parentheses/gmlwls96.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package leetcode_study
22

33
class Solution {
4+
/** 시간 : O(n), 공간 : O(n) */
45
fun isValid(s: String): Boolean {
56
val stack = Stack<Char>()
67
val openParentheses = "([{"

0 commit comments

Comments
 (0)