Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions container-with-most-water/sonjh1217.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
func maxArea(_ height: [Int]) -> Int {
var heights = height
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코드가 깔끔하고 좋네요 👍 height를 안쓰고, heights를 새로 만들어서 쓰시는 이유가 있을까요?

Copy link
Contributor Author

@sonjh1217 sonjh1217 Aug 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func maxArea(_ height: [Int]) -> Int { 라인은 leetCode에서 자동생성된건데.. 제가 습관이 되어서 Collection 타입들은 s가 붙어야 Collection으로 읽혀서요ㅎㅎ Swift에서 권장되는 방식입니답!

var start = 0
var end = heights.count - 1
var maxAmount = 0

while start < end {
let startHeight = heights[start]
let endHeight = heights[end]
let amount = min(startHeight, endHeight) * (end - start)
maxAmount = max(amount, maxAmount)

if startHeight < endHeight {
start += 1
} else {
end -= 1
}
}

return maxAmount

//시간 O(n)
//공간 O(1)
}
}