반응형
https://leetcode.com/problems/container-with-most-water/
Container With Most Water - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
import kotlin.math.max
import kotlin.math.min
class Solution {
fun maxArea(height: IntArray): Int {
var answer = 0
var left = 0
var right = height.size - 1
while (right - left > 0) {
val calcHeight = min(height[left], height[right])
val calcWidth = right - left
answer = max(answer, calcHeight * calcWidth)
if (height[left] > height[right]) {
right -= 1
} else {
left += 1
}
}
return answer
}
}
import kotlin.math.max
import kotlin.math.min
/**
* Time Limit Exceeded
**/
class Solution {
fun maxArea(height: IntArray): Int {
var answer = 0
height.forEachIndexed { index, left ->
for (j: Int in index + 1 until height.size) {
val right = height[j]
val calcHeight = min(right, left)
val calcWidth = j - index
answer = max(answer, calcHeight * calcWidth)
}
}
return answer
}
}
https://github.com/960813/leetcode-problems/commit/33f9795d8d59f2f4085fd295b358f0cc02243ce6
[11] Kotlin · 960813/leetcode-problems@33f9795
println(maxArea(intArrayOf(1, 8, 6, 2, 5, 4, 8, 3, 7)) == 49)
github.com
반응형
☕️ Networking
기술 직군의 기술적인 교류, 커리어 이야기, 직군 무관 네트워킹 모두 환영합니다!
위클리 아카데미 오픈 채팅방(비밀번호: 9323)
kakaotalk: https://open.kakao.com/o/gyvuT5Yd