반응형
Heli, 헬리
시행착오를 줄이는 방법 - 진태양
Heli, 헬리
  • 분류 전체보기 (82)
    • General (28)
      • Essay (22)
      • Craftsmanship (2)
      • IT Meet & Hack (4)
    • Finance (1)
      • Portfolio (1)
      • Activity (0)
    • Infrastructure (1)
      • Kubernetes (0)
      • AWS (1)
    • Development (45)
      • News (4)
      • Architecture (4)
      • Web (1)
      • Spring Framework (7)
      • JVM (12)
      • MongoDB (0)
      • Git (2)
      • Algorithm (14)
      • Python (1)
    • Computer Science (1)
      • Network (1)
    • Civic Hacking (3)
      • Code for Korea (3)
    • Know-how (2)
      • IT Service (1)
      • Career (1)
    • English (1)
      • Translation (1)

인기 글

  • 서버 개발자, 커뮤니티 빌더의 이야기가 궁금하신분!
    2023.03.28
    서버 개발자, 커뮤니티 빌더의 이야기가 궁금하신분!
  • Why DDD, Clean Architecture and ⋯
    2022.03.10
    Why DDD, Clean Architecture and ⋯
  • [번역] 개발자가 잠자는 동안 돈을버는 5가지 방법 | 사⋯
    2022.04.17
    [번역] 개발자가 잠자는 동안 돈을버는 5가지 방법 | 사⋯
  • M1 칩에서 pyqt5 설치하기 - qmake 패스 설정
    2022.07.30
  • [Java & Kotlin] enum class가 완벽한 ⋯
    2021.12.13
    [Java & Kotlin] enum class가 완벽한 ⋯

블로그 메뉴

  • 홈
  • 관리
  • 방명록
hELLO · Designed By 정상우.
Heli, 헬리

시행착오를 줄이는 방법 - 진태양

[LeetCode] 11. Container With Most Water (Kotlin)
Development/Algorithm

[LeetCode] 11. Container With Most Water (Kotlin)

2021. 12. 10. 12:46
반응형

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

    'Development/Algorithm' 카테고리의 다른 글
    • [LeetCode] 10. Regular Expression Matching (Kotlin)
    • [LeetCode] 8. String to Integer (atoi) (Kotlin)
    • [LeetCode] 7. Reverse Integer (Kotlin)
    • [LeetCode] 6. Zigzag Conversion (Kotlin)
    Leetcode, 릿코드, 알고리즘, 코테, 코틀린
    Heli, 헬리
    Heli, 헬리
    Java/Kotlin, Spring 백엔드 관련 기술을 익히고 공유합니다.
    [LeetCode] 10. Regular Expression Matching (Kotlin)
    이전 글
    [LeetCode] 10. Regular Expression Matching (Kotlin)

    티스토리툴바