Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Algorithm] 최소직사각형 #96

Closed
hwangJi-dev opened this issue Jan 27, 2023 · 0 comments
Closed

[Algorithm] 최소직사각형 #96

hwangJi-dev opened this issue Jan 27, 2023 · 0 comments

Comments

@hwangJi-dev
Copy link
Owner

hwangJi-dev commented Jan 27, 2023

💬 문제

[코딩테스트 연습 - 최소직사각형](https://school.programmers.co.kr/learn/courses/30/lessons/86491)


💬 Idea

  • sizes를 돌며 각 size를 정렬하여 w를 각 size의 최소값 중 최대가 되도록 만들고, h를 최대값 중 최대가 되게 만들어준다.

💬 풀이

func solution(sizes:[[Int]]) -> Int {
    var result = sizes[0].sorted()
    
    for i in 1..<sizes.count {
        if result[0] < sizes[i].sorted()[0] {
            result[0] = sizes[i].sorted()[0]
        }
        if result[1] < sizes[i].sorted()[1] {
            result[1] = sizes[i].sorted()[1]
        }
    }
    
    return result.reduce(1, *)
}

💬 더 나은 방법

  • max, min을 사용하여 최소값 중 최대값을 찾아준다..!!! wow
func solution(sizes:[[Int]]) -> Int {
    var MAX = 0
    var MIN = 0

    for size in sizes {
        MAX = max(size.max()!, MAX)
        MIN = max(size.min()!, MIN)
    }

    return MAX * MIN
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant