-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WEEK6](gmlwls96) Container With Most Water
- Loading branch information
Showing
2 changed files
with
23 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class Solution { | ||
/** 시간 : O(n), 공간 : O(1)*/ | ||
fun maxArea(height: IntArray): Int { | ||
var maxDiff = 0 | ||
var left = 0 | ||
var right = height.lastIndex | ||
// left, right값을 순차적으로 조회해서 물높이를 구하고, | ||
// left < right값 보다 작으면 left증가시킨다. 반대는 right 감소 | ||
while (left < right) { | ||
maxDiff = max(maxDiff, (right - left) * min(height[left], height[right])) | ||
// 너비 : right - left | ||
// 현재 높이 : min(height[left], height[right]) | ||
// 너비 * 현재 높이가 maxDiff 비교하여 더 큰값이 maxDiff가 된다. | ||
if (height[left] < height[right]) { | ||
left++ | ||
} else { | ||
right-- | ||
} | ||
} | ||
return maxDiff | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters